C# Windows API应用之基于GetDesktopWindow获得桌面所有窗口句柄的方法

2019-12-30 13:46:17王振洲

本文实例讲述了C# Windows API应用之基于GetDesktopWindow获得桌面所有窗口句柄的方法。,具体如下:

Windows API

Windows 这个多作业系统除了协调应用程序的执行、分配内存、管理资源…之外, 它同时也是一个很大的服务中心,调用这个服务中心的各种服务(每一种服务就是一个函数),可以帮应用程式达到开启视窗、描绘图形、使用周边设备等目的,由于这些函数服务的对象是应用程序(Application), 所以便称之为 Application Programming Interface,简称 API 函数。WIN32 API也就是Microsoft Windows 32位平台的应用程序编程接口。

GetDesktopWindow

函数功能:该函数返回桌面窗口的句柄。桌面窗口覆盖整个屏幕。桌面窗口是一个要在其上绘制所有的图标和其他窗口的区域。

函数原型:HWND GetDesktopWindow(VOID)

参数:无。

返回值:函数返回桌面窗口的句柄。

速查:Windows NT:3.1以上版本;Windows:95以上版本:

头文件:Winuser.h;库文件:user32.lib。

【声明】

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();

【说明】

获得代表整个屏幕的一个窗口(桌面窗口)句柄

【返回值】

Long,桌面窗口的句柄

获得桌面所有窗口句柄的方法

创建项目

文件->新建->项目…

C#,Windows,API,GetDesktopWindow,获得,桌面窗口,句柄

API导入

GetDesktopWindow


/// <summary>
/// 该函数返回桌面窗口的句柄。桌面窗口覆盖整个屏幕。桌面窗口是一个要在其上绘制所有的图标和其他窗口的区域。
/// 【说明】获得代表整个屏幕的一个窗口(桌面窗口)句柄.
/// </summary>
/// <returns>返回值:函数返回桌面窗口的句柄。</returns>
[DllImport("user32.dll", EntryPoint = "GetDesktopWindow", CharSet = CharSet.Auto, SetLastError = true)]
static extern IntPtr GetDesktopWindow();

GetWindow


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