Posted by maxwell on April 16, 2006
In Reply to How to use SetWindowLong outside my aplication? posted by haivan lovrevic on
April 05, 2006
If you have the handle of a control, even in another running application, you can do several things with it, such as send a WM_GETTEXT message to get its text.
But to obtain the handle, you need to know the exact order of the controls that are parents to the desired control.
Example: using Delphi to get the current URL (web address) from an Internet Explorer window:
procedure TForm1.Button1Click(Sender: TObject); var hIEMain, hIEToolbar, hIEAddressBar, hIEComboBox: THandle; Buffer: array[0..255] of Char; begin // 1. Class name of main IE window is called 'IEFrame' hIEMain := FindWindow('IEFrame', Buffer); // 2. Use FindWindowEx several times for getting // handle of the combobox with the URL if hIEMain > 0 then begin hIEToolbar := FindWindowEx(hIE, 0, 'WorkerW', nil); hIEAddressBar := FindWindowEx(hIEToolbar, 0, 'ReBarWindow32', nil); hIEComboBox := FindWindowEx(hIEAddressBar, 0, 'ComboBoxEx32', nil); // 3. Read text of the combobox into buffer SendMessage(hIEComboBox, WM_GETTEXT, 255, integer(@Buffer)); // 4. Do something with text, for example: Label1.Caption := Buffer; end else ShowMesage('Error: Internet Explorer window not found'); end;
Careful: FindWindow, as used in step 1, retrieves the handle to the top-level window. In the example, you will get the handle of the last activated IE window. What if you want the URLs of all the IE windows?
That's a bit more complicated:
1. Get the handle of each IE window ("enumerate" the IE windows), with the function EnumWindows in combination with a "callback" function.
Note: a very good free tool for finding the hierarchy of controls Windows application, is called "WinDowse". Run the tool, place the mouse cursor on any window, and WinDowse shows all parameters of that window.
Related Articles and Replies