This article is a simple discussion on how to get window handles, and which functions are available! It can be applied to VC, BCB (other I didn't try, estimated), I tried in the BCB environment.
First, I will listen out some Win32 API functions that get the handle, then simply talk about their use! Finally, tell me how I understand and apply. Laugh!
Available Win32 API functions:
1.hWnd FindWindow (LPCTSTR LPCLASSNAME, LPCTSTR LPWINDOWNAME)
HWnd FindwindowEx (HWND HWNDPARENT, HWND HWNDCHILDAFTER, LPCTSTSTSTSTSTLASSNAME, LPCTSTR LPWINDOWNAME)
2.hWnd WindowFromPoint (Point & Point)
3. Bool Callback EnumchildProc (HWND HWND, LPARAM LPARAM)
Bool Callback EnumchildWindows (HWND HWNDPARENT, WNDENUMPROC LPENUMFUNC, LPARAM LPARAM)
Bool Callback Enumwindows (WndenumProc Lpenumfunc, LParam LPARAM)
Bool Callback EnumwindowsProc (HWND HWND, LPARAM LPARAM)
General purpose:
For the first one, everyone is familiar, is a conventional weapon to capture the handle, these two brothers can accept capture objects or one of the window titles, and return to an HWnd. But for the general masses, not necessarily knowing all the names of all windows (including the title bar, buttons, etc.)! Jujube can be simply for example, do you know the class name of the desktop icon? For window titles, there may be the same title, there are two new windows to refer to two processes, this is a trouble! Ok, this problem is put first, continue the next group.
The second group, the POINT structure defined by Win32 (Typedef struct tagpoint {long x;
LONG Y;} Point, to get the window HWnd of the current mouse cursor position, this is the most intuitive weapon! The routine operation is as follows: First get the Cursor's Point (Bool getCursorpos (LPPOINT) function, use WindowFromPoint. In this way, we can get any HWnd with any open function with the window! Then by obtaining the win32 API function of the class name (INT Get HWnd, LPTSTR LPCLASSNAME, INT NMAXCOUNT), the LPClassName here is best to use the character array address, nmaxcount is the size of the array, at the same time, this method is solved The first problem! Jujube I can put the mouse anywhere! * ^ _ ^ *
The third group, these are superior weapons used to list and handle any windows! By combining EnumWindows and EnumWindowsProc, EnumChildWindows and EnumChildProc, you can scan all windows of your desktop and processed it!
My understanding: (This part of the task-driven teaching method, who makes the younger brother is a teacher! Xi xi)
Task: Get all the names of all windows.
Solution 1: We will first think of the third group first, you can start from the desktop window (it is the ancestor of all windows), sequentially scans, get the class name. A bit like Visual Stdio's Spy , or Borland's WINSIGHT32, the specific method is as follows: (BCB) In the main program, call EnumWindows, the function address that is incoming younumProc is the first parameter, don't forget to convert to the WndenumProc type. The second reference can be NULL. :: Enumwindows (ReinterPret_cast Younumproc, NULL);
In the younumproc function, if the first entangled hWnd = = NULL, it will be jumped out of (return false;), it can be ended!
Then, prepare the class named group, get the class name, save it.
Returns the true value and continues the next scan.
It is not complicated, it is a function recursive. But I will explain! Face! : P
Second solution: Simple, intuitive jujube you want to come out, quite
First prepare a clock, a class method (I use TMEMO)
In the timer processing function:
1, get the current Cursor point location
2, use WindowFromPoint,
3, then get class name, put it in TMEMO
This allows you to get the window you want (including buttons, etc.) as soon as the mouse is put on the window. . . Haha
The third method: It should also be possible to use the FindWindow and the loop structure.
Summary: In fact, there are many ways to get HWnd, such as knowing the window level, and sweep it in turn. . . That's the third kind! But I think, my method is most effective, what do you say?
Welcome everyone to contact me and discuss this problem! I have many questions about this problem, such as the transformation of HWnd and ID, in the HWND or ID of the form control in the IE page, or other stuff, in short, it is possible to identify his. . . I am very confused, no way!
Unclear, everyone should refer to MSDN! (Good things!)
Here is an example from Microsoft, this enumerates all windows, then send a closed message to the window.
#include
Bool Callback EnumwindowsProc
HWND HWND,
DWord LPARAM
);
//
// EnumWindowsProc Must Be Called from A Windows
// Application on Windows 95.
//
Int Winapi WinMain
Hinstance hinstance,
Hinstance Hprevinstance,
LPSTR LPCMDLINE,
Int ncmdshow
)
{
//
// Close All Open Applications.
//
Enumwindows (EnumWindowsProc, 0);
// Now do a regular logoff.
EXITWINDOWSEX (EWX_LOGOFF, 0);
}
Bool Callback EnumwindowsProc
HWND HWND,
DWord LPARAM
)
{
DWORD PID = 0;
LRESULT LRESULT;
Handle HPROCESS;
DWORD DWRESULT;
LResult = sendMessagetimeout
HWnd,
WM_QUERYENDSession,
0,
Endsession_logoff,
SMTO_ABORTIFHUNG,
2000,
& dwresult);
IF (LRESULT)
{
//
// Application Will Terminate Nicely, So Let IT.
//
LResult = sendMessagetimeout
HWnd,
WM_ENDSession,
True,
Endsession_logoff,
SMTO_ABORTIFHUNG,
2000,
& dwresult);
}
Else // You Have to Take More Forceful MeasureS.
{
//
// Get the processid for this window.
//
GetWindowThreadProcessId (hwnd, & pid);
//
// Open the process with all access.
//
HProcess = OpenProcess (Process_All_Access, False, PID);
//
// Terminate the process.
//
TerminateProcess (HProcess, 0);
}
//
//Undinue the enumeration.
//
Return True;
}