Win32 API window handle acquisition-my experience
For the first time I wrote a document, I hope you can correct me if I have a poor understanding of the document!
This article describes how to obtain the window handle and what functions are available for use! It can be applied to VC and BCB (I have not tried it, I guess it can be). I have tested it in the BCB environment.
First, I will list some Win32 API functions for obtaining the handle, and then briefly talk about their usage! Finally, let's talk about how I understand and apply it. Laugh!
Available Win32 API functions:
1. hwnd findwindow (maid, lpwindowname)
Hwnd find1_wex (hwnd hwndparent, hwnd hwndchildafter, lpctstr lpclassname, lptstr lpwindowname)
2. hwnd windowfrompoint (point & point)
3. bool callback enumchildproc (hwnd, lparam)
Bool callback enumchildwindows (hwnd hwndparent, wndenumproc lpenumfunc, lparam)
Bool callback enumwindows (wndenumproc lpenumfunc, lparam)
Bool callback enumwindowsproc (hwnd, lparam)
General purpose:
The first one is familiar to everyone. It is a conventional weapon for capturing handles. The findwindow brothers can accept the class name or window title of the object to be captured. As a parameter, an hwnd is returned. However, for general users, they do not necessarily know the class names of all windows (including the title bar, buttons, and so on! -- A simple example is provided. Do you know the class name of the window of the desktop icon? For the window title, the same title may appear. There are two windows-two processes of a program. This is a problem! Now, let's move on to the next group.
The second group uses the point structure defined by Win32 (typedef struct tagpoint {long X;
Long y;} Point) to obtain the Windows hwnd at the current cursor position, which is the most intuitive weapon! The general operation is as follows: first obtain the cursor's point (bool getcursorpos (lppoint) function) and then use windowfrompoint. In this way, we can obtain the hwnd of any function with window opened! Then, you can obtain the Win32 API function (INT getclassname (hwndHwnd, LptstrLpclassname, IntNmaxcount
) Get the class name -- here'sLpclassnameIt is best to use the character array address,NmaxcountIt is the size of the array. At the same time, this method solves the first problem! -- I can place the mouse anywhere! * ^_^ *
Group 3: these are super weapons used to list and process any window! By combining enumwindows and enumwindowsproc, enumchildwindows and enumchildproc, you can scan and process all windows on the desktop!
My understanding: (this part uses the task-driven teaching method-who makes the younger brother a teacher! Xi)
Task: Get the class names of all windows.
Solution 1: we first thought about the third group, which can start from the desktop window (which is the ancestor of all windows) and scan in sequence to get the coexistence of class names. It is a bit like visual stdio's spy ++ or Borland's winsight32. The specific method is as follows: (BCB)
In the main program, call enumwindows and input the youenumproc function address as the first parameter. Do not forget to convert it to the wndenumproc type. The second parameter can be null. : Enumwindows (reinterpret_cast <wndenumproc> youenumproc, null );
In the youenumproc function, if the first parameter hwnd = NULL, it will jump off (return false ;),It's over!
Then, prepare the class name array to get the class name and save it.
Returns the true value and continues the next scan.
It does not seem complicated. It is a function recursion. But I will explain it! Noodles! : P
Solution 2: simple and intuitive-self-developed and proud
First, prepare a clock, a memory class name method (I use tmemo)
In the timer processing function:
1. Obtain the point position of the current cursor.
2. Use windowfrompoint again,
3. Get the class name and put it in tmemo.
In this way, you can use the mouse to get the desired window (including buttons), as long as you move the mouse in the window for a while... Haha
Method 3: Actually, the findwindow and loop structure should also be available.
Summary: in fact, there are many methods to obtain hwnd. For example, if you know the window level, scan down in sequence... Let's talk about the third type! But I think my method is the most effective. What do you mean?
You are welcome to contact me and discuss this issue! I still have many questions about this issue, such as converting hwnd to ID, hwnd or ID of the Form Control on the IE page, or something else, in short, it is something that can recognize him... I am confused!
For details, refer to msdn! (Good stuff !)