Enumerate child windows and buttons for the specified parent window
I have been writing something like spy ++ and viewing the password window for a long time. I always wanted to add something special to this little thing. Some time ago, I was interested in software installation and registration. If you do not enter the correct registration code for some software, the "Next" button will always be disable. This time I asked spy ++ to thoroughly check the spy, set the Registration button to light up, and let me easily go to the next step ....
My idea is to move the cursor over the specified window and check the number of buttons in the window. If there are buttons, enable them all. I don't want to discuss how to implement this function here, but what you need to know is that the handle of these Disable Windows (buttons) cannot be obtained through the windowfrompoint API function at all, do not think about getwindow. After checking msdn, we can see that enumchildwindows is a good thing. You can enumerate all the child windows in a parent window:
- Bool enumchildwindows (
- Hwnd hwndparent, // handle to parent window // parent window handle
- Wndenumproc lpenumfunc, // callback function address
- Lparam // application-defined value // your own defined parameter
- );
In this simple way, Let's define another callback function, as shown below:
- Bool callback enumchildproc (
- Hwnd, // handle to Child Window
- Lparam // application-defined value
- );
Note: This callback function is either a class static function or a global function.
--------------------------------
When you call the enumchildwindows function, the function is always enumerated until the maximum subwindow is called or the callback function returns a false value. Otherwise, the function is always enumerated. With the above knowledge, I think you should know how to do it. With the callback function concept and the above example, we can continue. In fact, if you want to find a window handle with a known title, you can use an API function: findwindow. Its original function is:
- Function findwindow (lpclassname, lpwindowname: pchar): hwnd; stdcall;
- Lpclassname: the name of the window class. If you only know the title, it can be blank. The window class name can be obtained using many tools, such as winsignt32.
Lpwindowname: window title.
Call method example:
- VaR wndhwnd: hwnd;
- Wndhwnd: = findwindow (nil, 'window title ');
- If wndhwnd <> 0 then file: // find the handle of this window.
- Begin
- XXXXX
- End
- Else begin
- MessageBox (self. Handle, 'The handle to this Windows' is not found, 'hs', 0 );
- End;
With this window handle, it is not far from our initial goal: to control the window controls on other forms. similarly, you must first obtain the handle of the window control on other forms. we use this API function: enumchildwindows. the original function is:
- Function enumchildwindows (hwndparent: hwnd;
- Lpenumfunc: tfnwndenumproc;
- Lparam: lparam): bool; stdcall;
This function and the enumwindow function are somewhat imaginary. its role is also very similar. its function is to list the handles of all window controls on a form with a window handle of hwndparent. it is also given in the form of callback function parameters. let's take another practical example to illustrate the usage of this function. the function of the program is to allow the user to enter a window title, and then call the findwindow function to find the window handle. with this handle, all the window controls in this window are displayed in a memo. write the callback function first.
- Function enumchildwndproc (ahwnd: longint;
- Alparam: lparam): Boolean; stdcall;
- VaR
- Wndclassname: array [0 .. 254] of char;
- Wndcaption: array [0 .. 254] of char;
- Begin
- Getclassname (ahwnd, wndclassname, 254 );
- Getwindowtext (ahwndnd, wndcaption, 254 );
- With form1.memo1 do
- Begin
- Lines. Add (string (wndclassname ));
- Lines. Add (string (wndcaption ));
- Lines. Add ('-------');
- End;
- Result: = true;
- End;
Then, call the enumchildwindows function in an event.
- Procedure tform1.button1click (Sender: tobject );
- VaR
- Hwnd: longint;
- Begin
- Memo1.lines. Clear;
- Memo1.lines. Add (edit1.text + 'controls with the following class name ');
- Hwnd: = findwindow (nil, pchar (edit1.text ));
- If hwnd <> 0 then
- Begin
- Enumchildwindows (hwnd, @ enumchildwndproc, 0 );
- End
- Else MessageBox (self. Handle, 'The handle to this Windows' is not found, 'hs', 0 );
- End;
The program list is as follows:
- Unit unit1;
- Interface
- Uses
- Windows, messages, sysutils, variants, classes, graphics, controls, forms,
- Dialogs, stdctrls;
- Type
- Tform1 = Class (tform)
- Memo1: tmemo; // display the found Control
- Label1: tlabel;
- Edit1: tedit; // enter the title.
- Button1: tbutton;
- Procedure button1click (Sender: tobject );
- Private
- {Private Declarations}
- Public
- {Public declarations}
- End;
- VaR
- Form1: tform1;
- Function enumchildwndproc (ahwnd: longint;
- Alparam: lparam): Boolean; stdcall;
- Implementation
- {$ R *. DFM}
- Function enumchildwndproc (ahwnd: longint;
- Alparam: lparam): Boolean; stdcall;
- VaR
- Wndclassname: array [0 .. 254] of char;
- Wndcaption: array [0 .. 254] of char;
- Begin
- Getclassname (ahwnd, wndclassname, 254 );
- Getwindowtext (ahwndnd, wndcaption, 254 );
- With form1.memo1 do
- Begin
- Lines. Add (string (wndclassname ));
- Lines. Add (string (wndcaption ));
- Lines. Add ('-------');
- End;
- Result: = true;
- End;
- Procedure tform1.button1click (Sender: tobject );
- VaR
- Hwnd: longint;
- Begin
- Memo1.lines. Clear;
- Memo1.lines. Add (edit1.text + 'controls with the following class name ');
- Hwnd: = findwindow (nil, pchar (edit1.text ));
- If hwnd <> 0 then
- Begin
- Enumchildwindows (hwnd, @ enumchildwndproc, 0 );
- End
- Else MessageBox (self. Handle, 'The handle to this Windows' is not found, 'hs', 0 );
- End;
- End.
With the control handle, we can certainly do what we like. For example:
- Sendmessage (hwnd, wm_settext, 0, longint (pchar ('sdafdsf ')));
You can send text to the control. others can also send different messages and do many things. however, there is a big problem: Assume that a form has many identical controls and there is no way to differentiate them, even if we can find all the control handles, we cannot tell which one is what we want. after thinking for a long time, I found the answer in the monopoly. I can solve the problem by using a small skill.
Trackback: http://tb.blog.csdn.net/TrackBack.aspx? Postid = 588250