Enumerate child windows and buttons for the specified parent window to control its operations.

Source: Internet
Author: User

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:

  1. Bool enumchildwindows (
  2. Hwnd hwndparent, // handle to parent window // parent window handle
  3. Wndenumproc lpenumfunc, // callback function address
  4. Lparam // application-defined value // your own defined parameter
  5. );

In this simple way, Let's define another callback function, as shown below:

  1. Bool callback enumchildproc (
  2. Hwnd, // handle to Child Window
  3. Lparam // application-defined value
  4. );

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:

  1. Function findwindow (lpclassname, lpwindowname: pchar): hwnd; stdcall;
  2. 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:

  1. VaR wndhwnd: hwnd;
  2. Wndhwnd: = findwindow (nil, 'window title ');
  3. If wndhwnd <> 0 then file: // find the handle of this window.
  4. Begin
  5. XXXXX
  6. End
  7. Else begin
  8. MessageBox (self. Handle, 'The handle to this Windows' is not found, 'hs', 0 );
  9. 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:

  1. Function enumchildwindows (hwndparent: hwnd;
  2. Lpenumfunc: tfnwndenumproc;
  3. 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.

  1. Function enumchildwndproc (ahwnd: longint;
  2. Alparam: lparam): Boolean; stdcall;
  3. VaR
  4. Wndclassname: array [0 .. 254] of char;
  5. Wndcaption: array [0 .. 254] of char;
  6. Begin
  7. Getclassname (ahwnd, wndclassname, 254 );
  8. Getwindowtext (ahwndnd, wndcaption, 254 );
  9. With form1.memo1 do
  10. Begin
  11. Lines. Add (string (wndclassname ));
  12. Lines. Add (string (wndcaption ));
  13. Lines. Add ('-------');
  14. End;
  15. Result: = true;
  16. End;

Then, call the enumchildwindows function in an event.

  1. Procedure tform1.button1click (Sender: tobject );
  2. VaR
  3. Hwnd: longint;
  4. Begin
  5. Memo1.lines. Clear;
  6. Memo1.lines. Add (edit1.text + 'controls with the following class name ');
  7. Hwnd: = findwindow (nil, pchar (edit1.text ));
  8. If hwnd <> 0 then
  9. Begin
  10. Enumchildwindows (hwnd, @ enumchildwndproc, 0 );
  11. End
  12. Else MessageBox (self. Handle, 'The handle to this Windows' is not found, 'hs', 0 );
  13. End;

The program list is as follows:

  1. Unit unit1;
  2. Interface
  3. Uses
  4. Windows, messages, sysutils, variants, classes, graphics, controls, forms,
  5. Dialogs, stdctrls;
  6. Type
  7. Tform1 = Class (tform)
  8. Memo1: tmemo; // display the found Control
  9. Label1: tlabel;
  10. Edit1: tedit; // enter the title.
  11. Button1: tbutton;
  12. Procedure button1click (Sender: tobject );
  13. Private
  14. {Private Declarations}
  15. Public
  16. {Public declarations}
  17. End;
  18. VaR
  19. Form1: tform1;
  20. Function enumchildwndproc (ahwnd: longint;
  21. Alparam: lparam): Boolean; stdcall;
  22. Implementation
  23. {$ R *. DFM}
  24. Function enumchildwndproc (ahwnd: longint;
  25. Alparam: lparam): Boolean; stdcall;
  26. VaR
  27. Wndclassname: array [0 .. 254] of char;
  28. Wndcaption: array [0 .. 254] of char;
  29. Begin
  30. Getclassname (ahwnd, wndclassname, 254 );
  31. Getwindowtext (ahwndnd, wndcaption, 254 );
  32. With form1.memo1 do
  33. Begin
  34. Lines. Add (string (wndclassname ));
  35. Lines. Add (string (wndcaption ));
  36. Lines. Add ('-------');
  37. End;
  38. Result: = true;
  39. End;
  40. Procedure tform1.button1click (Sender: tobject );
  41. VaR
  42. Hwnd: longint;
  43. Begin
  44. Memo1.lines. Clear;
  45. Memo1.lines. Add (edit1.text + 'controls with the following class name ');
  46. Hwnd: = findwindow (nil, pchar (edit1.text ));
  47. If hwnd <> 0 then
  48. Begin
  49. Enumchildwindows (hwnd, @ enumchildwndproc, 0 );
  50. End
  51. Else MessageBox (self. Handle, 'The handle to this Windows' is not found, 'hs', 0 );
  52. End;
  53. End.

With the control handle, we can certainly do what we like. For example:

  1. 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

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.