RAD Studio
ContentsIndex
PreviousUpNext
Handling messages
Name 
Description 
When your component changes global settings that affect all of the controls in a form or other container, you may want to send a message to those controls so that they can update themselves appropriately. Not every control may need to respond to the notification, but by broadcasting the message, you can inform all controls that know how to respond and allow the other controls to ignore the message.
To broadcast a message to all the controls in another control, use the Broadcast method. Before you broadcast a message, you fill out a message record with the information you want... more 
Sometimes there is only a single control that needs to respond to your message. If you know the control that should receive your message, the simplest and most straightforward way to send the message is to call the control's Perform method.
There are two main reasons why you call a control's Perform method:
  • You want to trigger the same response that the control makes to a standard Windows (or other) message. For example, when a grid control receives a keystroke message, it creates an inline edit control and then sends the keystroke message on to the edit control.
  • You may... more 
When the widget layer receives an event notification from the operating system, it generates a special event object (QEvent or one of its descendants) to represent the event. The event object contains read-only information about the event that occurred. The type of the event object indicates the type of event that occurred.
The widget layer notifies your CLX component of system events using a special signal of type event. It passes the QEvent object to the signal handler for the event. The processing of the event signal is a bit more complicated than processing other signals because it goes... more 
There are times you may want to send a message but you do not know whether it is safe for the target of the message to execute right away. For example, if the code that sends a message is called from an event handler on the target control, you may want to make sure that the event handler has finished executing before the control executes your message. You can handle this situation as long as you do not need to know the message result.
Use the Windows API call, PostMessage, to send a message to a control but allow... more 
In a multithreaded application, you can't just call the Perform method because the target control is in a different thread than the one that is executing. However, by using the Windows message queue, you can safely communicate with other threads. Message handling always occurs in the main VCL thread, but you can send a message using the Windows message queue from any thread in the application. A call to SendMessage is synchronous. That is, SendMessage does not return until the target control has handled the message, even if it is in another thread.
Use the Windows API call, SendMessage,... more 
Typically, an application sends message to send notifications of state changes or to broadcast information. Your component can broadcast messages to all the controls in a form, send messages to a particular control (or to the application itself), or even send messages to itself.
There are several different ways to send a Windows message. Which method you use depends on why you are sending the message. The following topics describe the different ways to send Windows messages:
Before changing the message handling of your components, make sure that is what you really want to do. The VCL translates most Windows messages into events that both the component writer and the component user can handle. Rather than changing the message-handling behavior, you should probably change the event-handling behavior.
To change message handling in VCL components, you override the message-handling method. You can also prevent a component from handling a message under certain circumstances by trapping the message. 
Because the VCL provides handlers for most common messages, the time you will most likely need to create new message handlers is when you define your own messages. Working with user-defined messages has three aspects:  
A message identifier is an integer-sized constant. Windows reserves the messages below 1,024 for its own use, so when you declare your own messages you should start above that level.
The constant WM_APP represents the starting number for user-defined messages. When defining message identifiers, you should base them on WM_APP.
Be aware that some standard Windows controls use messages in the user-defined range. These include list boxes, combo boxes, edit boxes, and command buttons. If you derive a component from one of these and want to define a new message for it, be sure to check the Messages unit... more 
The following code shows two user-defined messages.  
For example, here is the message record for all mouse messages, TWMMouse, which uses a variant record to define two sets of names for the same parameters.  
If you want to give useful names to the parameters of your message, you need to declare a message-record type for that message. The message-record is the type of the parameter passed to the message-handling method. If you do not use the message's parameters, or if you want to use the old-style parameter notation (wParam, lParam, and so on), you can use the default message-record, TMessage. 
There are two sets of circumstances that require you to declare new message-handling methods:
  • Your component needs to handle a Windows message that is not already handled by the standard components.
  • You have defined your own message for use by your components.
 
Here is the declaration of a message handler for a user-defined message called CM_CHANGECOLOR.  
A number of the standard components define messages for internal use. The most common reasons for defining messages are broadcasting information not covered by standard messages and notification of state changes. You can define your own messages in the VCL.
Defining a message is a two-step process. The steps are:
  1. Declaring a message identifier.
  2. Declaring a message-record type.
 
When an application creates a window, it registers a window procedure with the Windows kernel. The window procedure is the routine that handles messages for the window. Traditionally, the window procedure contains a huge case statement with entries for each message the window has to handle. Keep in mind that "window" in this sense means just about anything on the screen: each window, each control, and so on. Every time you create a new type of window, you have to create a complete window procedure.
The VCL simplifies message dispatching in several ways:
  • Each component inherits a complete message-dispatching system.... more 
For example, to override a component's handling of the WM_PAINT message, you redeclare the WMPaint method:  
To change the way a component handles a particular message, you override the message-handling method for that message. If the component does not already handle the particular message, you need to declare a new message-handling method.
To override a message-handling method, you declare a new method in your component with the same message index as the method it overrides. Do not use the override directive; you must use the message directive and a matching message index.
Note that the name of the method and the type of the single var parameter do not have to match the overridden method. Only... more 
Components often need to respond to notifications from the underlying operating system. The operating system informs the application of occurrences such as what the user does with the mouse and keyboard. Some controls also generate notifications, such as the results from user actions such as selecting an item in a list box. The component library handles most of the common notifications already. It is possible, however, that you will need to write your own code for handling such notifications.
For VCL applications, notifications arrive in the form of messages. These messages can come from any source, including Windows, VCL... more 
Under some circumstances, you might want your components to ignore messages. That is, you want to keep the component from dispatching the message to its handler. To trap a message, you override the virtual method WndProc.
For VCL components, the WndProc method screens messages before passing them to the Dispatch method, which in turn determines which method gets to handle the message. By overriding WndProc, your component gets a chance to filter out messages before dispatching them. An override of WndProc for a control derived from TWinControl looks like this:  
Note: This information is applicable when writing VCL components only.
Here is part of the WndProc method for TControl, for example:  
All VCL classes have a built-in mechanism for handling messages, called message-handling methods or message handlers. The basic idea of message handlers is that the class receives messages of some sort and dispatches them, calling one of a set of specified methods depending on the message received. If no specific method exists for a particular message, there is a default handler.
The following diagram shows the message-dispatch system:

The Visual Component Library defines a message-dispatching system that translates all Windows messages (including user-defined messages) directed to a particular class into method calls. You should never need to alter this... more 
Once inside a message-handling method, your component has access to all the parameters of the message structure. Because the parameter passed to the message handler is a var parameter, the handler can change the values of the parameters if necessary. The only parameter that changes frequently is the Result field for the message: the value returned by the SendMessage call that sends the message.
Because the type of the Message parameter in the message-handling method varies with the message being handled, you should refer to the documentation on Windows messages for the names and meanings of individual parameters. If for... more 
A Windows message is a data record that contains several fields. The most important of these is an integer-size value that identifies the message. Windows defines many messages, and the Messages unit declares identifiers for all of them. Other useful information in a message comes in two parameter fields and a result field.
One parameter contains 16 bits, the other 32 bits. You often see Windows code that refers to those values as wParam and lParam, for word parameter and long parameter. Often, each parameter will contain more than one piece of information, and you see references to names... more 
Copyright(C) 2008 CodeGear(TM). All Rights Reserved.
What do you think about this topic? Send feedback!