C # WINDOWSAPI Application getdesktopwindow-get a handle on all the windows of the desktop in a detailed way

Source: Internet
Author: User


Windows API

In addition to coordinating application execution, allocating memory, managing resources, Windows is a multi-job system ... , it is also a large service center, invoking the service center of the various services (each service is a function), can help the application to open windows, graphics, use peripheral devices, and so on, because these functions serve the object is the application (application), so called Application programming Interface, referred to as API functions. The WIN32 API is the application programming interface for Microsoft Windows 32-bit platforms.

GetDesktopWindow

function Function: This function returns the handle of the desktop window. The desktop window covers the entire screen. The desktop window is an area on which you want to draw all the icons and other windows.
function prototype : HWND GetDesktopWindow (VOID)
parameter : None.
return value : The function returns a handle to the desktop window.
Quick Check : Windows nt:3.1 or above version; windows:95 or above version:;
header file : Winuser.h; library file: User32.lib.
"declaration"
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 representing the entire screen
"return value"
Long, handle to the desktop window

How to get all the window handles on the desktop

Create a project

New project, File--...

API Import

GetDesktopWindow

<summary>
The function returns a handle to the desktop window. The desktop window covers the entire screen. The desktop window is an area on which you want to draw all the icons and other windows.
Description Gets a window (desktop window) handle representing the entire screen.
</summary>
<returns> return Value: The function returns a handle to the desktop window. </returns>
[DllImport ("user32.dll", EntryPoint = "GetDesktopWindow", CharSet = CharSet.Auto, SetLastError = True)]
static extern IntPtr GetDesktopWindow ();

GetWindow

        <summary>        ////The function returns a window handle that has a specific relationship with the specified window, such as the z-order or owner.        ///function prototype: HWND GetWindow (hwnd hwnd,unit ncmd);///</summary>///        <param name= "HWND" > Window handle. The window handle to get is a handle to the window based on the value of the Ncmd parameter. </param>        //<param name= "Ucmd" > describes the relationship between the specified window and the window to get the handle. The parameter value references 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 the 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 a window handle that has been destroyed.        ///Quick check: Windows nt:3.1 or above, windows:95 or above, Windows ce:1.0 or above; 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. </summary> enum Getwindowcmd:uint {///<summary>///return handle identifies the highest Z-order            The same type of window. 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 highest Z-order            End of the same genus window.            </summary> Gw_hwndfirst = 0,////<summary>//The handle that is returned 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 z-order:///If the specified window is a top-level window, the handle identifies the top-level window at the lowest z-order, or if the specified window is a child window, the sentence            The handle identifies the same genus window at the lowest z-order.             </summary> gw_hwndlast = 1,////<summary>//The handle that is returned identifies the same type of window under the specified window in the 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 genus window under the specified window. </summary> Gw_hwndnext = 2,////<summary>//Returns the handle identified in the Z-orderSpecifies the same type of window on the window. If the specified window is the highest-end window, the handle identifies the highest-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 specified window            On the same genus window.            </summary> Gw_hwndprev = 3,////<summary>//The handle that is returned identifies the owner window of the specified window, if one exists.            Gw_owner and Gw_child are not relative arguments, there is no parent window meaning, if you want the parent window, use GetParent ().            For example: for example, sometimes the gw_owner of a control in a dialog box does not exist. </summary> Gw_owner = 4,///<summary>//If the specified window is a parent window, the handle to the child window at the top of the tab order is obtained            , otherwise null.            The function checks only the child window of the specified parent window and does not check the inheritance window. </summary> Gw_child = 5,///<summary>//(WindowsNT 5.0) returns a handle that identifies the specified window.            In the Enable status pop-up window (retrieved using the first window found by Gw_hwndnext to satisfy the aforementioned conditions);//////If no enable window, the handle obtained is the same as the specified window.              </summary> gw_enabledpopup = 6}/*getwindowcmd Specifies the relationship of the resulting window to the source window, which is based on the following constants: Gw_child look for the first subwindow of the source window Gw_hwndfirsT finds the first sibling window for a source subwindow, or looks for the first top-level window Gw_hwndlast to find the last sibling window for a source subwindow, or to find the last top-level window               Gw_hwndnext for the source window look for the next sibling window Gw_hwndprev for the source window looking for the previous sibling window Gw_owner Find the owner of the window */

Writing methods

        <summary>///        How 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 handle of the desktop window            IntPtr desktopptr = GetDesktopWindow ();            2. Get a Subwindow (this is usually a top-level window, the currently active window)            IntPtr winptr = GetWindow (desktopptr, getwindowcmd.gw_child);            3, loop to get all the sub windows under the desktop while            (winptr! = IntPtr.Zero)            {                //4, continue to get the next subwindow                winptr = GetWindow (Winptr, Getwindowcmd.gw_hwndnext);            }        }
Related Article

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.