In a VCL Forms program, I have a Form that implements a method for handling windows messages and updating some controls on the Form, something like:
procedure OnMsgTest (var Msg: TMessage); message WM_CUSTOMTEST;
I use PostMessage
with a custom message to this Form, using a code like this:
h := FindWindow('TFrmTest', nil);
if IsWindow(h) then begin
PostMessage(h, WM_CUSTOMTEST, 0, 0);
end;
When the Form is instantiated several times, using the above code to send the message, only one Form instance updates the information on the screen. I would like all open and instantiated Forms to receive the message.
An important note: PostMessage
can occur within the Form process itself, but also from another process. So, I believe a loop through the Forms would not work.
What would be the best approach to reach my goal?
EnumerateWindows
to find all top level windows. Check if they have a class name that matches. Hope that nobody else in the world uses the same form name as you do. – NephrosisRegisterWindowMessage()
withPostMessage(HWND_BROADCAST)
. Let the OS do the enumeration for you, and only interested windows will react to the message, other windows will ignore it. Then the class name doesn't matter. However, sinceRegisterWindowMessage
is dynamic, you can't use amessage
handler, so have the Form override the virtualWndProc
method instead. – Coates