Getting Delphi Window Windows

I am trying to get window handles for a Delphi application from an external application. I see that several windows have been created (TApplication, TFrmMain and some others), and I know that TApplication is a "controller", but it never displays. However, can I find out what the value is for a real window? I know this is TFrmMain (for this particular application), but is there any way to understand this? Is information stored in window properties or elsewhere? Thanks!

+6
winapi delphi
source share
3 answers

No, there is no documented way to know which window represents Application.MainForm from outside the application. In newer versions of Delphi, the main handle to the form window is not necessarily Application.MainForm.Handle ; applications can handle the OnGetMainFormHandle event to return whatever they want - which is used to select the parent window for modal dialogs.

You can guess by looking for windows with the β€œmain” in the class names, but even if you find it, there is no guarantee that there is only one instance of this. Applications can have several top-level windows, in which case it makes no sense to designate any of them as "main".

+11
source share

A class name of any form Delphi is also the registered class name of the window underlying the "Windows window". Therefore, you should use the FindWindow () call to the Windows API to get the TFrmMain window handle a little something like:

  hWnd := FindWindow('TFrmMain', NIL); 

If there are (potentially) multiple instances of a given form class name, you can distinguish them using the 2nd parameter (Window name, ie β€œsignature” or title). If this is not enough, you may need a little more complicated and look at the EnumWindows () function and check the properties of the windows to find interest.

To check the class name of the arbirary window handle (for example, in your callback function that you use with EnumWindows ()), use GetClassName () , for example:

 function GetWindowClassName(const aHWND: HWND): String; var buf: array[0..255] of Char; // Tip: Use a more appropriately sized array begin GetClassName(SomeHWND, @buf, Length(buf)); result := buf; end; ... if SameText(GetWindowClassName(hwnd), 'TFrmMain') then ... etc 

Without the specific details of your specific implementation task, it’s hard to say what will most likely work best for you, but hopefully this should be enough pointers to make you move in the right direction.

+4
source share

I usually use WinDowse to help me get started, but then you should use the API functions as described by Deltics.

+1
source share

All Articles