RAD Studio |  |

Trapping Messages

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:

Copy Code

    procedure TMyControl.WndProc(var Message: TMessage);
    begin
      { tests to determine whether to continue processing }
      inherited WndProc(Message);
    end;

Copy Code

    void __fastcall TMyControl::WndProc(TMessage& Message)
    {
       // tests to determine whether to continue processing
    if(Message.Msg != WM_LBUTTONDOWN)

    }

The TControl component defines entire ranges of mouse messages that it filters when a user is dragging and dropping controls. Overriding WndProc helps this in two ways:

- It can filter ranges of messages instead of having to specify handlers for each one.
- It can preclude dispatching the message at all, so the handlers are never called.

Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
What do you think about this topic? Send feedback!