One way to achieve this is via the WindowProc
property.
Simply supply your own window procedure by assigning to WindowProc
on the instance you want to hook. You'll need to take a copy to the previous value of WindowProc
so that you can make sure that the original handling is carried out.
Roughly it goes like this:
type
TMyClass = class
....
FOldWindowProc: TWndMethod;
procedure NewWindowProc(var Message: TMessage);
....
end;
To redirect the window procedure you do this:
FOldWindowProc := SomeControl.WindowProc;
SomeControl.WindowProc := NewWindowProc;
Then implement the new window procedure like this:
procedure TMyClass.NewWindowProc(var Message: TMessage);
begin
FOldWindowProc(Message);
if Message.Msg = WM_GROUPUNGROUP then
....
end;
When you are done with the control, put the old window procedure back in place:
SomeControl.WindowProc := FOldWindowProc;
Another way to do it is to take advantage of the fact that the message is queued. You can add an Application.OnMessage
handler, most likely by using a TApplicationEvents
object. This will get a look at all queued messages. However, OnMessage
fires before the message is dispatched to the control which sounds like it may be the wrong way round for you.