


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...
<p style="max-width:90%">/// <summary><br/> /// 该函数返回桌面窗口的句柄。桌面窗口覆盖整个屏幕。桌面窗口是一个要在其上绘制所有的图标和其他窗口的区域。<br/> /// 【说明】获得代表整个屏幕的一个窗口(桌面窗口)句柄.<br/> /// </summary><br/> /// <returns>返回值:函数返回桌面窗口的句柄。</returns><br/> [DllImport("user32.dll", EntryPoint = "GetDesktopWindow", CharSet = CharSet.Auto, SetLastError = true)] <br/> static extern IntPtr GetDesktopWindow();<br/></p>
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);
GetWindowCmd /// <summary>
/// 窗口与要获得句柄的窗口之间的关系。
/// </summary>
enum GetWindowCmd : uint
{ /// <summary>
/// 返回的句柄标识了在Z序最高端的相同类型的窗口。
/// 如果指定窗口是最高端窗口,则该句柄标识了在Z序最高端的最高端窗口;
/// 如果指定窗口是顶层窗口,则该句柄标识了在z序最高端的顶层窗口:
/// 如果指定窗口是子窗口,则句柄标识了在Z序最高端的同属窗口。
/// </summary>
GW_HWNDFIRST = 0, /// <summary>
/// 返回的句柄标识了在z序最低端的相同类型的窗口。
/// 如果指定窗口是最高端窗口,则该柄标识了在z序最低端的最高端窗口:
/// 如果指定窗口是顶层窗口,则该句柄标识了在z序最低端的顶层窗口;
/// 如果指定窗口是子窗口,则句柄标识了在Z序最低端的同属窗口。
/// </summary>
GW_HWNDLAST = 1, /// <summary>
/// 返回的句柄标识了在Z序中指定窗口下的相同类型的窗口。
/// 如果指定窗口是最高端窗口,则该句柄标识了在指定窗口下的最高端窗口:
/// 如果指定窗口是顶层窗口,则该句柄标识了在指定窗口下的顶层窗口;
/// 如果指定窗口是子窗口,则句柄标识了在指定窗口下的同属窗口。
/// </summary>
GW_HWNDNEXT = 2, /// <summary>
/// 返回的句柄标识了在Z序中指定窗口上的相同类型的窗口。
/// 如果指定窗口是最高端窗口,则该句柄标识了在指定窗口上的最高端窗口;
/// 如果指定窗口是顶层窗口,则该句柄标识了在指定窗口上的顶层窗口;
/// 如果指定窗口是子窗口,则句柄标识了在指定窗口上的同属窗口。
/// </summary>
GW_HWNDPREV = 3, /// <summary>
/// 返回的句柄标识了指定窗口的所有者窗口(如果存在)。
/// GW_OWNER与GW_CHILD不是相对的参数,没有父窗口的含义,如果想得到父窗口请使用GetParent()。
/// 例如:例如有时对话框的控件的GW_OWNER,是不存在的。
/// </summary>
GW_OWNER = 4, /// <summary>
/// 如果指定窗口是父窗口,则获得的是在Tab序顶端的子窗口的句柄,否则为NULL。
/// 函数仅检查指定父窗口的子窗口,不检查继承窗口。
/// </summary>
GW_CHILD = 5, /// <summary>
/// (WindowsNT 5.0)返回的句柄标识了属于指定窗口的处于使能状态弹出式窗口(检索使用第一个由GW_HWNDNEXT 查找到的满足前述条件的窗口);
/// 如果无使能窗口,则获得的句柄与指定窗口相同。
/// </summary>
GW_ENABLEDPOPUP = 6
} /*GetWindowCmd指定结果窗口与源窗口的关系,它们建立在下述常数基础上:
GW_CHILD
寻找源窗口的第一个子窗口
GW_HWNDFIRST
为一个源子窗口寻找第一个兄弟(同级)窗口,或寻找第一个顶级窗口
GW_HWNDLAST
为一个源子窗口寻找最后一个兄弟(同级)窗口,或寻找最后一个顶级窗口
GW_HWNDNEXT
为源窗口寻找下一个兄弟窗口
GW_HWNDPREV
为源窗口寻找前一个兄弟窗口
GW_OWNER
寻找窗口的所有者
*/
Writing method /// <summary>
/// 获得桌面所有窗口句柄的方法
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
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);
}
}
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!

The combination of C# and .NET provides developers with a powerful programming environment. 1) C# supports polymorphism and asynchronous programming, 2) .NET provides cross-platform capabilities and concurrent processing mechanisms, which makes them widely used in desktop, web and mobile application development.

.NETFramework is a software framework, and C# is a programming language. 1..NETFramework provides libraries and services, supporting desktop, web and mobile application development. 2.C# is designed for .NETFramework and supports modern programming functions. 3..NETFramework manages code execution through CLR, and the C# code is compiled into IL and runs by CLR. 4. Use .NETFramework to quickly develop applications, and C# provides advanced functions such as LINQ. 5. Common errors include type conversion and asynchronous programming deadlocks. VisualStudio tools are required for debugging.

C# is a modern, object-oriented programming language developed by Microsoft, and .NET is a development framework provided by Microsoft. C# combines the performance of C and the simplicity of Java, and is suitable for building various applications. The .NET framework supports multiple languages, provides garbage collection mechanisms, and simplifies memory management.

C# and .NET runtime work closely together to empower developers to efficient, powerful and cross-platform development capabilities. 1) C# is a type-safe and object-oriented programming language designed to integrate seamlessly with the .NET framework. 2) The .NET runtime manages the execution of C# code, provides garbage collection, type safety and other services, and ensures efficient and cross-platform operation.

To start C#.NET development, you need to: 1. Understand the basic knowledge of C# and the core concepts of the .NET framework; 2. Master the basic concepts of variables, data types, control structures, functions and classes; 3. Learn advanced features of C#, such as LINQ and asynchronous programming; 4. Be familiar with debugging techniques and performance optimization methods for common errors. With these steps, you can gradually penetrate the world of C#.NET and write efficient applications.

The relationship between C# and .NET is inseparable, but they are not the same thing. C# is a programming language, while .NET is a development platform. C# is used to write code, compile into .NET's intermediate language (IL), and executed by the .NET runtime (CLR).

C#.NET is still important because it provides powerful tools and libraries that support multiple application development. 1) C# combines .NET framework to make development efficient and convenient. 2) C#'s type safety and garbage collection mechanism enhance its advantages. 3) .NET provides a cross-platform running environment and rich APIs, improving development flexibility.

C#.NETisversatileforbothwebanddesktopdevelopment.1)Forweb,useASP.NETfordynamicapplications.2)Fordesktop,employWindowsFormsorWPFforrichinterfaces.3)UseXamarinforcross-platformdevelopment,enablingcodesharingacrossWindows,macOS,Linux,andmobiledevices.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver Mac version
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version
Useful JavaScript development tools