PostMessage for all instances of a specific form (ClassName):

In the VCL Forms program, I have a Form that implements a method for processing Windows messages and updating some form controls, for example:

procedure OnMsgTest (var Msg: TMessage); message WM_CUSTOMTEST;

I use PostMessagewith a custom post to this form using this code:

  h := FindWindow('TFrmTest', nil);    
  if IsWindow(h) then begin    
    PostMessage(h, WM_CUSTOMTEST, 0, 0);    
  end;

When a Form instance is created several times using the above code to send a message, only one Form instance updates the information on the screen. I would like all open and created instances of forms to receive a message.

An important note: it PostMessagemay arise in the Form process itself, but also from another process. So, I believe that the cycle through Forms will not work.

What will be the best approach to achieve my goal?

+4
1

, . EnumWindows() FindWindow/Ex(), PostMessage(HWND_BROADCAST) , RegisterWindowMessage(). , , . :

type
  TMyForm = class(TForm)
  protected
    procedure WndProc(var Msg: TMessage); override;
  end;

...

var
  WM_CUSTOMTEST: UINT = 0;

procedure TMyForm.WndProc(var Msg: TMessage);
begin
  if (Msg.Msg = WM_CUSTOMTEST) and (WM_CUSTOMTEST <> 0) then
  begin
    ...
  end else
    inherited;
end;

initialization
  WM_CUSTOMTEST := RegisterWindowMessage('SomeUniqueNameHere');

:

if WM_CUSTOMTEST <> 0 then
  PostMessage(HWND_BROADCAST, WM_CUSTOMTEST, 0, 0);    
+8

Source: https://habr.com/ru/post/1691928/


All Articles