C# WindowsAPI application GetDesktopWindow-Detailed explanation of the method of obtaining all window handles on the desktop

黄舟
Release: 2017-03-11 13:30:30
Original
4813 people have browsed it


Windows API


In addition to coordinating the execution of applications, allocating memory, and managing resources, Windows, a multi-operating system, is also a large The service center calls various services of this service center (each service is a function), which can help the application to open windows, draw graphics, use peripheral devices, etc., because the objects served by these functions are applications (Application ), so it is called Application Programming Interface, or API function for short. 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. The desktop window is the area on which all icons and other windows are drawn.
Function prototype: HWND GetDesktopWindow(VOID)
Parameters: None.
Return value: The function returns the handle of the desktop window.
Quick check: Windows NT: version 3.1 or above; Windows: version 95 or above:;
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 the handle of a window (desktop window) representing the entire screen
[Return value]
Long, the handle of the desktop window

Method to obtain the handles of all windows on the desktop


Create project

File->New->Project...
C# WindowsAPI application GetDesktopWindow-Detailed explanation of the method of obtaining all window handles on the desktop

##API Import

GetDesktopWindow

///


/// 该函数返回桌面窗口的句柄。桌面窗口覆盖整个屏幕。桌面窗口是一个要在其上绘制所有的图标和其他窗口的区域。
/// 【说明】获得代表整个屏幕的一个窗口(桌面窗口)句柄.
///

/// 返回值:函数返回桌面窗口的句柄。
[DllImport("user32.dll", EntryPoint = "GetDesktopWindow", CharSet = CharSet.Auto, SetLastError = true)]
static extern IntPtr GetDesktopWindow();

Copy after login

GetWindow

///  /// 该函数返回与指定窗口有特定关系(如Z序或所有者)的窗口句柄。 /// 函数原型:HWND GetWindow(HWND hWnd,UNIT nCmd); ///  /// 窗口句柄。要获得的窗口句柄是依据nCmd参数值相对于这个窗口的句柄。 /// 说明指定窗口与要获得句柄的窗口之间的关系。该参数值参考GetWindowCmd枚举。 /// 返回值:如果函数成功,返回值为窗口句柄;如果与指定窗口有特定关系的窗口不存在,则返回值为NULL。 /// 若想获得更多错误信息,请调用GetLastError函数。 /// 备注:在循环体中调用函数EnumChildWindow比调用GetWindow函数可靠。调用GetWindow函数实现该任务的应用程序可能会陷入死循环或退回一个已被销毁的窗口句柄。 /// 速查:Windows NT:3.1以上版本;Windows:95以上版本;Windows CE:1.0以上版本;头文件:winuser.h;库文件:user32.lib。 ///  [DllImport("user32.dll", SetLastError = true)] static extern IntPtr GetWindow(IntPtr hWnd, GetWindowCmd uCmd);
Copy after login

GetWindowCmd

///  /// 窗口与要获得句柄的窗口之间的关系。 ///  enum GetWindowCmd : uint { ///  /// 返回的句柄标识了在Z序最高端的相同类型的窗口。 /// 如果指定窗口是最高端窗口,则该句柄标识了在Z序最高端的最高端窗口; /// 如果指定窗口是顶层窗口,则该句柄标识了在z序最高端的顶层窗口: /// 如果指定窗口是子窗口,则句柄标识了在Z序最高端的同属窗口。 ///  GW_HWNDFIRST = 0, ///  /// 返回的句柄标识了在z序最低端的相同类型的窗口。 /// 如果指定窗口是最高端窗口,则该柄标识了在z序最低端的最高端窗口: /// 如果指定窗口是顶层窗口,则该句柄标识了在z序最低端的顶层窗口; /// 如果指定窗口是子窗口,则句柄标识了在Z序最低端的同属窗口。 ///  GW_HWNDLAST = 1, ///  /// 返回的句柄标识了在Z序中指定窗口下的相同类型的窗口。 /// 如果指定窗口是最高端窗口,则该句柄标识了在指定窗口下的最高端窗口: /// 如果指定窗口是顶层窗口,则该句柄标识了在指定窗口下的顶层窗口; /// 如果指定窗口是子窗口,则句柄标识了在指定窗口下的同属窗口。 ///  GW_HWNDNEXT = 2, ///  /// 返回的句柄标识了在Z序中指定窗口上的相同类型的窗口。 /// 如果指定窗口是最高端窗口,则该句柄标识了在指定窗口上的最高端窗口; /// 如果指定窗口是顶层窗口,则该句柄标识了在指定窗口上的顶层窗口; /// 如果指定窗口是子窗口,则句柄标识了在指定窗口上的同属窗口。 ///  GW_HWNDPREV = 3, ///  /// 返回的句柄标识了指定窗口的所有者窗口(如果存在)。 /// GW_OWNER与GW_CHILD不是相对的参数,没有父窗口的含义,如果想得到父窗口请使用GetParent()。 /// 例如:例如有时对话框的控件的GW_OWNER,是不存在的。 ///  GW_OWNER = 4, ///  /// 如果指定窗口是父窗口,则获得的是在Tab序顶端的子窗口的句柄,否则为NULL。 /// 函数仅检查指定父窗口的子窗口,不检查继承窗口。 ///  GW_CHILD = 5, ///  /// (WindowsNT 5.0)返回的句柄标识了属于指定窗口的处于使能状态弹出式窗口(检索使用第一个由GW_HWNDNEXT 查找到的满足前述条件的窗口); /// 如果无使能窗口,则获得的句柄与指定窗口相同。 ///  GW_ENABLEDPOPUP = 6 } /*GetWindowCmd指定结果窗口与源窗口的关系,它们建立在下述常数基础上: GW_CHILD 寻找源窗口的第一个子窗口 GW_HWNDFIRST 为一个源子窗口寻找第一个兄弟(同级)窗口,或寻找第一个顶级窗口 GW_HWNDLAST 为一个源子窗口寻找最后一个兄弟(同级)窗口,或寻找最后一个顶级窗口 GW_HWNDNEXT 为源窗口寻找下一个兄弟窗口 GW_HWNDPREV 为源窗口寻找前一个兄弟窗口 GW_OWNER 寻找窗口的所有者 */
Copy after login

Writing method

///  /// 获得桌面所有窗口句柄的方法 ///  ///  ///  private void button1_Click(object sender, EventArgs e) { dataGridView1.Rows.Clear(); //1、获取桌面窗口的句柄 IntPtr desktopPtr = GetDesktopWindow(); //2、获得一个子窗口(这通常是一个顶层窗口,当前活动的窗口) IntPtr winPtr = GetWindow(desktopPtr, GetWindowCmd.GW_CHILD); //3、循环取得桌面下的所有子窗口 while (winPtr != IntPtr.Zero) { //4、继续获取下一个子窗口 winPtr = GetWindow(winPtr, GetWindowCmd.GW_HWNDNEXT); } }
Copy after login

The above is the detailed content of C# WindowsAPI application GetDesktopWindow-Detailed explanation of the method of obtaining all window handles on the desktop. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Issues
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!