Message in the Bottle

Handling Windows Messages the Delphi way

man programming

Przemyslaw Klos/EyeEm/Getty Images

Delphi, you've got message to handle!

One of the keys to traditional Windows programming is handling the messages sent by Windows to applications. Simply put, a message is some information sent from one place to another. For the most part, Delphi makes message handling easy through its use of events, an event is usually generated in response to a Windows message being sent to an application.

However, someday you may want to process some uncommon messages like: CM_MOUSEENTER which happens (is posted by Windows) when mouse cursor enters the client area of some component (or form).

Handling messages on their own requires a few extra programming techniques, this article is here to help us find the right way through the message river and grap needed information.

Strategies to Manipulate Windows Messages With Delphi

  • Drag a Window: No title bar! How can you drag such a window? It's easy and fun: let's make a Delphi form move by clicking (and dragging) in it's client area. The main idea is to get your hands on the wm_NCHitTest windows message.
  • How to send information (String, Image, Record) between two Delphi applications (WM_CopyData): Learn how to send the WM_CopyData message between two Delphi applications to exchange information and make two applications communicate. The accompanying source code demonstrates how to send a string, record (complex data type) and even graphics to another application.
  • Sticky Windows: This strategy allows you to dock your Delphi forms to the edges of your desktop screen.
  • Monitoring Registry Changes: Need to get notified about changes to the attributes or contents of a specified Registry key? Then you are ready for this your Delphi code toolkit.
  • Sending Messages to Non-Windowed Applications: This strategy is used to send messages (signals) to non-windowed applications by using AllocateHWND and DefWindowProc. You should understand what Delphi does in the background to intercept Windows messages, how can you write your own message handler for a windowed application and how to obtain a unique message identifier that you can safely use in your applications. There is also a small bug in the Delphi DeallocateHWND procedure that you can fix along the way.
  • Controlling the Number of Application Instances: In this article you'll learn how to "run-once enable" a Delphi application that can check for its previous (running) instance. Along the process, several techniques of implementing such a check will be discussed; as well as how to bring your already running application to the foreground, if a user tries to run it "one more time". By the end of the article you'll have a copy-to-go code to control the behavior of your application's multiple instances: with the option to limit the number of running instances.
  • How to Handle System Time Change Using Delphi Code: If you need to react when system date time has changed you can handle the WM_TimeChange Windows message.
  • How to Draw Custom Text on a Delphi Form's Caption Bar: If you want to add some custom text on the caption bar of a form, without changing the Caption property of the form you need to handle one special Windows message: WM_NCPAINT (along with WM_NCACTIVATE).
  • How to Display Menu Item Hints: By (Windows) design, in Delphi applications, hints assigned to menu items do not get displayed in the popup tooltip window (when the mouse hovers over a menu).
  • Get, Set, and Handle Display Device Modes (Screen Resolution and Color Depth): This strategy allows you to change the Windows display mode settings (resolution and color depth) from Delphi code. You can also handle the WM_DISPLAYCHANGE Windows message sent to all windows when the display resolution has changed.
  • Get Current URL From IE: There is a Delphi tactic to retrieve the full URL of all opened Internet Explorer instances.
  • Detecting and Preventing Windows Shut Down: You can use Delphi to programmatically cancel Windows shut down action.
  • Display a Password Dialog: Suppose you have a data-critical type of application where you would not want a non-authored user to work with the data. What if you need to display a password dialog *before* the application is restored to make sure an authorized user is accessing it.
  • Remove the Windows Constraint on Minimum Form Size: By Windows design, a form (window) has a size constraint that sets the minimum form height to the height of the caption bar and the width to 112 pixels (118 in XP theme).
  • How to Detect a TPopupMenu's OnClose (OnPopDown) Event: Unfortunately, the TPopupMenu does not expose an event you can handle that will fire when the menu gets closed - either after a user has selected an item from the menu or has activated some other UI element.
  • Trapping Messages Sent to an Application: "...Delphi surfaces the OnMessage event for the Application object. The OnMessage event handler is "supposed" to allow you trap every message sent to your application..."