C # GetDesktopWindow for Windows API applications -- method for obtaining the handles of all Windows on the desktop,

Source: Internet
Author: User

C # GetDesktopWindow for Windows API applications -- method for obtaining the handles of all Windows on the desktop,
Windows API

In addition to coordinating application execution, allocating memory, and managing resources, Windows Multi-job system... In addition, it is also a large service center that calls various services of this Service Center (each service is a function ), it can help applications enable Windows, depict graphics, and use peripheral devices. Because these function services are intended for applications, they are called Application Programming interfaces, API functions. WIN32 API is the Application Programming Interface of Microsoft Windows 32-bit platform.

Getasktopwindow

Function: Return the handle of the desktop window. The desktop window overwrites the entire screen. A desktop window is the area on which all icons and other windows are to be drawn.
Function prototype: HWND getasktopwindow (VOID)
Parameters: None.
Return Value: Return the handle of the desktop window.
Fast Query: Windows NT: 3.1 or later; Windows: 95 or later :;
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 = "getshorttopwindow", CharSet = CharSet. Auto, SetLastError = true)]
Static extern IntPtr getasktopwindow ();

[Description]
Obtain a window (desktop window) handle representing the entire Screen
[Return value]
Long: the handle of the desktop window.

Create a project by obtaining the handle of all windows on the desktop

File-> New-> project...

API import getasktopwindow
/// <Summary> /// return the handle of the desktop window. The desktop window overwrites the entire screen. A desktop window is the area on which all icons and other windows are to be drawn. /// [Description] obtain a window (desktop window) handle representing the entire screen. /// </summary> /// <returns> return value: the handle of the desktop window returned by the function. </Returns> [DllImport ("user32.dll", EntryPoint = "getshorttopwindow", CharSet = CharSet. Auto, SetLastError = true)] static extern IntPtr getshorttopwindow ();
GetWindow
/// <Summary> /// this function returns the window handle that has a specific relationship with 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 be obtained is based on the handle of the nCmd parameter value relative to this window. </Param> /// <param name = "uCmd"> describes the relationship between the specified window and the window to obtain the handle. For more information about this parameter value, see GetWindowCmd enumeration. </Param> // <returns> return value: If the function succeeds, the return value is a window handle. If the window with a specific relationship with the specified window does not exist, the return value is NULL. /// To obtain more error information, call the GetLastError function. /// Note: calling the EnumChildWindow function in the loop body is more reliable than calling the GetWindow function. The application that calls the GetWindow function to implement the task may be in an endless loop or return a destroyed window handle. /// Quick query: Windows NT: 3.1 or later; Windows: 95 or later; Windows CE: 1.0 or later; header file: winuser. h; library file: user32.lib. /// </Returns> [DllImport ("user32.dll", SetLastError = true)] static extern IntPtr GetWindow (IntPtr hWnd, GetWindowCmd uCmd );
GetWindowCmd
/// <Summary> /// relationship between the window and the window to obtain the handle. /// </Summary> enum GetWindowCmd: uint {// <summary> // The returned handle identifies windows of the same type at the top end of the Z sequence. /// If the specified window is the top-end window, the handle identifies the top-end window at the top end of the Z sequence. /// if the specified window is a top-level window, the handle identifies the top-level window at the top end of the z sequence: // if the specified window is a subwindow, the handle identifies the same window at the top end of the Z sequence. /// </Summary> GW_HWNDFIRST = 0, /// <summary> // The returned handle identifies windows of the same type at the lowest end of the z sequence. /// If the specified window is the highest-end window, the handle identifies the lowest-end window in the z sequence: // if the specified window is a top-level window, the handle identifies the top-level window at the lowest end of the z sequence; // if the specified window is a subwindow, the handle identifies the same window at the lowest end of the Z sequence. /// </Summary> GW_HWNDLAST = 1, /// <summary> // The returned handle identifies windows of the same type in the specified window in the Z sequence. /// If the specified window is the highest-end window, the handle identifies the highest-end window in the specified window: // if the specified window is a top-level window, the handle identifies the top-level window in the specified window; // if the specified window is a subwindow, the handle identifies the same window in the specified window. /// </Summary> GW_HWNDNEXT = 2, /// <summary> // The returned handle identifies windows of the same type on the specified window in the Z sequence. /// If the specified window is the highest-end window, the handle identifies the highest-end window in the specified window; // if the specified window is a top-level window, the handle identifies the top-level window in the specified window; // if the specified window is a subwindow, the handle identifies the same window in the specified window. /// </Summary> GW_HWNDPREV = 3, /// <summary> // The returned handle identifies the owner window of the specified window (if any ). /// GW_OWNER and GW_CHILD are not relative parameters and do not have the meaning of the parent window. If you want to get the parent window, use GetParent (). /// For example, sometimes the GW_OWNER of the control in the dialog box does not exist. /// </Summary> GW_OWNER = 4, /// <summary> /// if the specified window is a parent window, the obtained sub-window handle at the top of the Tab order. Otherwise, it is NULL. /// The function only checks the Child Window 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 pop-up window in the enable state that belongs to the specified window (retrieve the window that meets the preceding conditions found by GW_HWNDNEXT); // if no enable window exists, the obtained handle is the same as the specified window. /// </Summary> GW_ENABLEDPOPUP = 6}/* GetWindowCmd specifies the relationship between the result window and the source window based on the following constants: GW_CHILD find source window the first child window GW_HWNDFIRST is a source Child Window to find the first brother (same level) window, or find the first top-level window GW_HWNDLAST is a source Child Window to find the last brother (same level) window, or find the last top-level window GW_HWNDNEXT for the source window find the next brother window GW_HWNDPREV for the source window find the previous brother window GW_OWNER find the window owner */
Writing Method
/// <Summary> /// method for obtaining the handle of all windows on the desktop /// </summary> /// <param name = "sender"> </param> // /<param name = "e"> </param> private void button#click (object sender, eventArgs e) {maid. clear (); // 1. Get the desktop window handle IntPtr export topptr = GetDesktopWindow (); // 2. Get a subwindow (this is usually a top-level window, window of the current activity) IntPtr winPtr = GetWindow (optional topptr, GetWindowCmd. GW_CHILD); // 3. cyclically retrieve all child windows in the desktop while (winPtr! = IntPtr. Zero) {// 4. Continue to get the next sub-window winPtr = GetWindow (winPtr, GetWindowCmd. GW_HWNDNEXT );}}

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.