Table of Contents
Windows API
GetDesktopWindow
Method to obtain the handles of all windows on the desktop
Create project

///


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

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

Copy after login
" >

///


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

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

Copy after login
Home Backend Development C#.Net Tutorial C# WindowsAPI application GetDesktopWindow-Detailed explanation of the method of obtaining all window handles on the desktop

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

Mar 11, 2017 pm 01:30 PM


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

<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>
Copy after login

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);
Copy after login

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
              寻找窗口的所有者
         */
Copy after login

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);
            }
        }
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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Active Directory with C# Active Directory with C# Sep 03, 2024 pm 03:33 PM

Guide to Active Directory with C#. Here we discuss the introduction and how Active Directory works in C# along with the syntax and example.

C# Serialization C# Serialization Sep 03, 2024 pm 03:30 PM

Guide to C# Serialization. Here we discuss the introduction, steps of C# serialization object, working, and example respectively.

Random Number Generator in C# Random Number Generator in C# Sep 03, 2024 pm 03:34 PM

Guide to Random Number Generator in C#. Here we discuss how Random Number Generator work, concept of pseudo-random and secure numbers.

C# Data Grid View C# Data Grid View Sep 03, 2024 pm 03:32 PM

Guide to C# Data Grid View. Here we discuss the examples of how a data grid view can be loaded and exported from the SQL database or an excel file.

Patterns in C# Patterns in C# Sep 03, 2024 pm 03:33 PM

Guide to Patterns in C#. Here we discuss the introduction and top 3 types of Patterns in C# along with its examples and code implementation.

Factorial in C# Factorial in C# Sep 03, 2024 pm 03:34 PM

Guide to Factorial in C#. Here we discuss the introduction to factorial in c# along with different examples and code implementation.

Prime Numbers in C# Prime Numbers in C# Sep 03, 2024 pm 03:35 PM

Guide to Prime Numbers in C#. Here we discuss the introduction and examples of prime numbers in c# along with code implementation.

The difference between multithreading and asynchronous c# The difference between multithreading and asynchronous c# Apr 03, 2025 pm 02:57 PM

The difference between multithreading and asynchronous is that multithreading executes multiple threads at the same time, while asynchronously performs operations without blocking the current thread. Multithreading is used for compute-intensive tasks, while asynchronously is used for user interaction. The advantage of multi-threading is to improve computing performance, while the advantage of asynchronous is to not block UI threads. Choosing multithreading or asynchronous depends on the nature of the task: Computation-intensive tasks use multithreading, tasks that interact with external resources and need to keep UI responsiveness use asynchronous.

See all articles