Home > Articles > Programming > General Programming/Other Languages

Handling Messages in Delphi 6

  • Print
  • + Share This
Ease the pain of debugging Delphi code with these simple message-handling techniques.
From the author of


Handling or processing a message means that your application responds in some manner to a Windows message. In a standard Windows application, message handling is performed in each window procedure. By internalizing the window procedure, however, Delphi makes it much easier to handle individual messages; instead of having one procedure that handles all messages, each message has its own procedure. Three requirements must be met for a procedure to be a message-handling procedure:

  • The procedure must be a method of an object.

  • The procedure must take one var parameter of a TMessage or other message-specific record type.

  • The procedure must use the message directive followed by the constant value of the message you want to process.

Here's an example of a procedure that handles WM_PAINT messages:

procedure WMPaint(var Msg: TWMPaint); message WM_PAINT;

As another example, let's write a simple message-handling procedure for WM_PAINT that processes the message simply by beeping.

Start by creating a new, blank project. Then access the Code Editor window for this project and add the header for the WMPaint function to the private section of the TForm1 object:

procedure WMPaint(var Msg: TWMPaint); message WM_PAINT;

Now add the function definition to the implementation part of this unit. Remember to use the dot operator to scope this procedure as a method of TForm1. Don't use the message directive as part of the function implementation:

procedure TForm1.WMPaint(var Msg: TWMPaint);

Notice the use of the inherited keyword here. Call inherited when you want to pass the message to the ancestor object's handler. By calling inherited in this example, you pass on the message to TForm's WM_PAINT handler.

The main unit in Listing 1 provides a simple example of a form that processes the WM_PAINT message. Creating this project is easy: Just create a new project and add the code for the WMPaint procedure to the TForm object.

Listing 1—GetMess—A Message-Handling Example

unit GMMain;


 SysUtils, Windows, Messages, Classes, Graphics, Controls,
 Forms, Dialogs;

 TForm1 = class(TForm)
  procedure WMPaint(var Msg: TWMPaint); message WM_PAINT;

 Form1: TForm1;


{$R *.DFM}

procedure TForm1.WMPaint(var Msg: TWMPaint);


Whenever a WM_PAINT message comes down the pike, it's passed to the WMPaint procedure. The WMPaint procedure simply informs you of the WM_PAINT message by making some noise with the MessageBeep() procedure and then passes the message to the inherited handler.

MessageBeep(): The Poor Man's Debugger

While we're on the topic of beeping, now is a good time for a slight digression. The MessageBeep() procedure is one of the most straightforward and useful elements in the Win32 API. Its use is simple: Call MessageBeep(), pass a predefined constant, and Windows beeps the PC's speaker. (If you have a sound card, it plays a WAV file.) Big deal, you say? On the surface it might not seem like much, but MessageBeep() really shines as an aid in debugging your programs.

If you're looking for a quick-and-dirty way to tell whether your program is reaching a certain place in your code—without having to bother with the debugger and breakpoints—MessageBeep() is for you. Because it doesn't require a handle or some other Windows resource, you can use it practically anywhere in your code, and as a wise man once said, "MessageBeep() is for the itch you can't scratch with the debugger." If you have a sound card, you can pass MessageBeep() one of several predefined constants to have it play a wider variety of sounds—these constants are defined under MessageBeep() in the Win32 API help file.

If you're like the authors and are too lazy to type out that whole big, long function name and parameter, you can use the Beep() procedure found in the SysUtils unit. The implementation of Beep() is simply a call to MessageBeep() with the parameter 0.

  • + Share This
  • 🔖 Save To Your Account