The example in this article describes the method used by the C # Windows API to obtain all window handles on the desktop based on GetDesktopWindow. Share to everyone for your reference, specific as follows:
Windows API
Windows this multi-operating system, in addition to coordinating application execution, allocating memory, managing resources ... , it is also a very large service center, call the service center of the various services (each service is a function), can help the application to open windows, graphics, the use of peripheral devices, and so on, because these function services object is the application (application), so it is called Application programming Interface, abbreviation API function. The WIN32 API is the application programming interface of the Microsoft Windows 32-bit platform.
GetDesktopWindow
function function: This function returns the handle of the desktop window. The desktop window covers the entire screen. A desktop window is an area on which you want to draw all the icons and other windows.
Function prototype: HWND GetDesktopWindow (VOID)
Parameters: None.
Return value: The function returns a handle to the desktop window.
Quick check: Windows nt:3.1 above version; windows:95 above version:
Header file: Winuser.h; library file: User32.lib.
Statement
Vb:
Public Declare Function GetDesktopWindow Lib "user32" Alias "GetDesktopWindow" () as Long
Vb_net:
Public Declare Function GetDesktopWindow Lib "user32" Alias "GetDesktopWindow" () as Integer
C#:
[DllImport ("user32.dll", EntryPoint = "GetDesktopWindow", CharSet = CharSet.Auto, SetLastError = True)]
static extern IntPtr GetDesktopWindow ();
Description
Get a window (desktop window) handle that represents the entire screen
"Return value"
Long, handle to the desktop window
How to get all the window handles on the desktop
Create a project
File-> new-> project ...
API Import
GetDesktopWindow
<summary>
///This function returns the handle to the desktop window. The desktop window covers the entire screen. A desktop window is an area on which you want to draw all the icons and other windows.
///"description" gets a handle to a window (desktop window) that represents the entire screen.
</summary>
///<returns> return value: function returns the handle to the desktop window. </returns>
[DllImport ("user32.dll", EntryPoint = "GetDesktopWindow", CharSet = CharSet.Auto, SetLastError = true)]
static extern IntPtr GetDesktopWindow ();
GetWindow
<summary>
///This function returns a window handle that has a specific relationship to the specified window, such as Z-order or owner.
///function Prototype: HWND GetWindow (hwnd hwnd,unit ncmd);
///</summary>
///<param name= "HWND" > Window handle. The window handle to get is based on the Ncmd parameter value relative to the handle of the window. </param>
///<param name= "Ucmd" > describes the relationship between the specified window and the window to which the handle is to be obtained. The parameter value refers to the Getwindowcmd enumeration. </param>
///<returns> return value: If the function succeeds, the return value is the window handle, or null if the window that has a specific relationship to the specified window does not exist.
///If you want to get more error messages, call the GetLastError function.
///Note: Calling function Enumchildwindow in the loop body is more reliable than calling the GetWindow function. An application that calls the GetWindow function to implement the task may fall into a dead loop or return to a window handle that has been destroyed.
///: Windows nt:3.1 above version; windows:95 above version; Windows ce:1.0 version; header file: winuser.h; library file: User32.lib.
///</returns>
[DllImport ("user32.dll", SetLastError = True)]
static extern IntPtr GetWindow (IntPtr hWnd, Getwindowcmd ucmd);
Getwindowcmd
<summary>///The relationship between the window and the window to get the handle.
The handle returned by </summary> enum Getwindowcmd:uint {///<summary>///identifies the same type of window at the highest end of the z-order.
If the specified window is the highest-end window, the handle identifies the highest-end window at the top of the z-order;///if the specified window is a top-level window, the handle identifies the top-level window at the top of the Z-order:///If the specified window is a child window, the handle identifies the same generic window at the highest end of the z-order.
</summary> Gw_hwndfirst = 0, the handle returned by///<summary>///identifies the same type of window at the lowest end of the z-order.
If the specified window is the highest-end window, the handle identifies the highest-end window at the lowest end of the Z-order:///If the specified window is a top-level window, the handle identifies the top-level window at the lowest end of the z-order;///if the specified window is a child window, the handle identifies the same generic window at the lowest end of the z-order.
</summary> gw_hwndlast = 1,///<summary>///The returned handle identifies the same type of window under the specified window in Z-order.
If the specified window is the highest-end window, the handle identifies the highest-end window under the specified window:///If the specified window is a top-level window, the handle identifies the top-level window under the specified window;///If the specified window is a child window, the handle identifies the same generic window under the specified window.
</summary> Gw_hwndnext = 2,///<summary>///Returns a handle that identifies the same type of window on the specified window in the z-order.
If the specified window is the highest-end window, the handle identifies the top-end window on the specified window;///If the specified window is a top-level window, the handle identifies the top-level window on the specified window;///If the specified window is a child window, the handle identifies the same generic window on the specified window.
</summary> Gw_hwndprev = 3, the handle returned by///<summary>///identifies the owner window of the specified window (if present). Gw_owner and GW_child is not a relative parameter, does not have the meaning of the parent window, if you want the parent window, use GetParent ().
For example: for example, sometimes the gw_owner of a dialog box's control does not exist.
</summary> Gw_owner = 4,///<summary>///If the specified window is a parent window, the handle of the child window at the top of the tab order is obtained, otherwise null.
function checks only the child windows of the specified parent window and does not check the inheritance window. </summary> gw_child = 5,///<summary>///(WindowsNT 5.0) The returned handle identifies the in-State pop-up window that belongs to the specified window (the first one retrieved using the Gw_hwnd
NEXT Check the window that satisfies the conditions mentioned above;///the handle is the same as the specified window if there is no enabling window. </summary> gw_enabledpopup = 6}/*getwindowcmd Specifies the relationship between the result window and the source window based on the following constants: Gw_child looking for the first child window of the source window Gw_h Wndfirst find the first sibling window for a source child window, or look for the first top-level window gw_hwndlast to find the last sibling window for a source child window, or look for the last top-level window gw_hwndnext the source window for the next
A brother window Gw_hwndprev for the source window looking for the previous sibling window Gw_owner looking for the owner of the window * *
Writing methods
<summary>
///the way to get all window handles on the desktop
///</summary>
///<param name= "Sender" ></param >
///<param name= "E" ></param>
private void Button1_Click (object sender, EventArgs e)
{
dataGridView1.Rows.Clear ();
1, get the desktop window handle
IntPtr desktopptr = GetDesktopWindow ();
2. Get a child window (this is usually a top-level window, the currently active window)
IntPtr winptr = GetWindow (desktopptr, getwindowcmd.gw_child);
3. Loop to get all the child windows while
(winptr!= IntPtr.Zero)
{
//4, continue to get the next child window
winptr = GetWindow (Winptr, Getwindowcmd.gw_hwndnext);
}
Read more about C # Interested readers can view the site topics: "C # form Operation Tips Summary", "C # Data structure and algorithm tutorial", "C # Common control usage Tutorial", "C # object-oriented Program design Introductory Course" and "C # Programming Thread Usage Skills Summary"
I hope this article will help you with C # programming.