I am writing a program and I cannot figure out how to do this: it is necessary, when a window appears, to perform some kind of action. I have already done all the actions with the window, but I cannot catch it. I just don’t know how to add a condition: "if a window appears, then ...".

  • in a separate thread: FindWindow, with a loop and synchronization with the main thread - vv2cc
  • Can I have a sample code or link that I would feel, twist it? I was recommended to dig in the direction of SetWindowsHookEx - oldzas
  • in general, it is not very good to intercept events from other applications ... But it is possible to check in the loop whether a specific window has appeared ... - Vladyslav Matviienko
  • A window in your program or someone else's? - kot-da-vinci

1 answer 1

In Delphi, this can be done in at least two ways :

  • Describe the handler function signature ** of the system message in the main class (in the public section):

    public procedure Catch(var msg:TMessage);message SW_SHOW; ... 

And, accordingly, the implementation is already what you are implementing it. Suppose my example is this:

  procedure TForm1.Catch(var msg: TMessage); begin showmessage('Caught!'); end; 

This method can handle any system message for any form of application.

  • The second method already consists in processing the message by means of Delphi for each individual form. But, however, not every system message can be processed this way, so decide for yourself. Example of a handler procedure:

      procedure TForm1.FormShow(Sender: TObject); begin showmessage('Caught!'); end;