c# api遍历窗口控件句柄
- 行业动态
- 2025-02-15
- 3823
EnumChildWindows
来遍历窗口控件句柄。以下是一个示例代码片段:“ csharp,using System;,using System.Runtime.InteropServices;class Program,{, // 定义回调函数类型, delegate bool EnumWindowProc(IntPtr hWnd, IntPtr lParam); [DllImport("user32.dll")], static extern bool EnumChildWindows(IntPtr hWndParent, EnumWindowProc lpEnumFunc, IntPtr lParam); static void Main(), {, IntPtr parentHandle = GetParentHandle(); // 获取父窗口句柄的方法, EnumChildWindows(parentHandle, new EnumWindowProc(EnumWindow), IntPtr.Zero);, } static bool EnumWindow(IntPtr hWnd, IntPtr lParam), {, Console.WriteLine($"Found window handle: {hWnd}");, return true; // 返回true继续枚举,false停止枚举, } static IntPtr GetParentHandle(), {, // 实现获取父窗口句柄的逻辑, return IntPtr.Zero;, },},
` 这个代码通过调用
EnumChildWindows`函数遍历指定父窗口的所有子窗口句柄,并在控制台输出每个找到的窗口句柄。
在C#中遍历窗口控件句柄是一个相对复杂的任务,但通过使用Windows API和一些托管代码,可以有效地实现这一功能,以下是关于如何在C#中遍历窗口控件句柄的详细步骤和示例代码:
一、引入必要的命名空间
using System; using System.Runtime.InteropServices; using System.Diagnostics;
二、定义常量和结构体
为了调用Windows API,需要定义一些常量和结构体来表示窗口和控件的属性。
1、定义常量
WM_GETCHILD:用于获取子窗口句柄的消息标识符。
GWL_IDGLOBL:用于获取全局标识符的窗口属性标识符。
GWLP_USERDATA:用于获取用户数据的窗口属性标识符。
const int WM_GETCHILD = 0x002C; const int GWL_IDGLOBL = -12; const int GWLP_USERDATA = -21;
2、定义结构体
POINT:表示一个点的坐标。
RECT:表示一个矩形区域。
WNDCLASSEX:表示窗口类的属性。
MSG:表示消息结构。
[StructLayout(LayoutKind.Sequential)] public struct POINT { public int X; public int Y; } [StructLayout(LayoutKind.Sequential)] public struct RECT { public int Left; public int Top; public int Right; public int Bottom; } [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)] public class WNDCLASSEX { public uint cBSize; public IntPtr style; public IntPtr lpfnWndProc; public int cbClsExtra; public int cbWndExtra; public IntPtr hInstance; public IntPtr hIcon; public IntPtr hCursor; public IntPtr hbrBackground; public IntPtr lpszMenuName; public IntPtr lpszClassName; public IntPtr hIconSm; } [StructLayout(LayoutKind.Sequential)] public struct MSG { public IntPtr hwnd; public int message; public IntPtr wParam; public IntPtr lParam; public uint time; public POINT pt; }
三、导入Windows API函数
使用DllImport特性将Windows API函数导入到C#代码中。
1、FindWindow:根据窗口类名和窗口标题查找窗口句柄。
2、GetWindowRect:获取指定窗口的矩形区域。
3、GetClientRect:获取指定窗口的客户区矩形。
4、GetWindowThreadProcessId:获取指定窗口的线程标识符和进程标识符。
5、GetWindowLong:获取指定窗口的特定属性值。
6、IsWindowVisible:判断指定窗口是否可见。
7、EnumChildWindows:枚举指定父窗口的所有子窗口。
8、SendMessage:向指定窗口发送消息。
9、GetClassName:获取指定窗口的类名。
10、GetWindowTextLength:获取指定窗口的文本长度。
11、GetWindowText:获取指定窗口的文本内容。
12、GetParent:获取指定窗口的父窗口句柄。
13、GetWindow:获取与指定条件匹配的窗口句柄。
14、IsZoomed:判断指定窗口是否最大化。
15、IsIconic:判断指定窗口是否最小化。
16、IsDialogMessage:判断消息是否为对话框消息。
17、TranslateAccelerator:将加速键消息转换为字符消息。
18、GetForegroundWindow:获取前台窗口句柄。
19、GetAsyncKeyState:获取虚拟键的状态。
20、GetKeyState:获取虚拟键的状态(异步版本)。
21、GetLastActivePopup:获取最后一个活动弹出式窗口的句柄。
22、GetMsgTask:获取消息任务的句柄。
23、GetQueueStatus:获取消息队列的状态。
24、PeekMessage:从消息队列中查看消息。
25、PostQuitMessage:向消息队列发送退出消息。
26、RegisterHotKey:注册热键。
27、UnregisterHotKey:注销热键。
28、GetRawInputData:获取原始输入数据。
29、GetRawInputDeviceList:获取原始输入设备列表。
30、WaitMessage:等待消息。
31、GetMessageTime:获取消息的时间戳。
32、GetMessagePos:获取消息的位置。
33、DefWindowProcW:调用默认的窗口过程。
34、CallWindowProcW:调用窗口过程。
35、DispatchMessageW:分发消息。
36、IsHungAppWindow:判断应用程序窗口是否挂起。
37、AttachThreadInput:附加输入队列。
38、GetWindowDC:获取设备上下文环境的句柄。
39、ReleaseDC:释放设备上下文环境。
40、GetDCEx:获取扩展的设备上下文环境句柄。
41、ReleaseDCEx:释放扩展的设备上下文环境句柄。
42、GetWindowInfo:获取窗口信息。
43、AdjustWindowRectEx:调整窗口矩形区域(扩展版)。
44、AdjustWindowRect:调整窗口矩形区域。
44、BeginDeferWindowPos:开始延迟窗口位置更新。
45、DeferWindowPos:延迟窗口位置更新。
46、EndDeferWindowPos:结束延迟窗口位置更新。
47、UpdateLayeredWindow:更新分层窗口。
48、UpdateLayeredWindowSize:更新分层窗口大小。
49、SetLayeredWindowAttributes:设置分层窗口属性。
50、DwmEnableComposition:启用桌面组成。
51、DwmExtendFrameIntoClientArea:扩展框架到客户端区域。
52、DwmIsCompositionEnabled:判断桌面组成是否启用。
53、DwmRegisterThumbnail:注册缩略图。
54、DwmUnregisterThrumble:注销缩略图。
55、DwmUpdateThumbnailProperties:更新缩略图属性。
56、DwmQueryThumbnailSourceSize:查询缩略图源大小。
57、DwmInvalidateThumbnailHandle:使缩柄无效。
58、DwmSetThumbnailClip:设置缩略图剪辑区域。
59、DwmGetColorizationParameters:获取颜色参数。
60、DwmSetColorizationParameters:设置颜色参数。
62、OpenIcon:打开图标。
63、CreateIconIndirect:创建图标(间接方式)。
64、CreateIconFromResourceEx:从资源创建图标(扩展版)。
65、DestroyIcon:销毁图标。
66、LoadIconWithScaleDown:加载并缩放图标。
67、LoadImage:加载图像。
68、DrawIconEx:绘制图标(扩展版)。
69、DrawIcon:绘制图标。
70、ExtractIconExW:提取图标(宽字符版)。
71、ExtractIconW:提取图标(宽字符版)。
72、ExtractAssociatedIconW:提取关联图标(宽字符版)。
73、ExtractAssociatedIcon:提取关联图标。
74、DestroyCursor:销毁光标。
75、CopyIcon:复制图标。
76、CopyImage:复制图像。
77、LookupIconIdFromDirectoryEx:从目录中查找图标ID(扩展版)。
78、LookupIconIdFromDirectory:从目录中查找图标ID。
79、CreateCompatibleBitmap:创建兼容位图。
80、GetObjectType:获取对象类型。
81、GetObjectBounds:获取对象边界。
82、GetObjectHandleCount:获取对象句柄计数。
83、GetObjectHandleAmount:获取对象句柄数量。
84、DeleteObject:删除对象。
85、CreateBitmap:创建位图。
86、CreateBitmapIndirect:创建位图(间接方式)。
87、CreateCompatibleDC:创建兼容设备上下文。
88、CreateCompatibleDCEx:创建兼容设备上下文(扩展版)。
99、CreateCompatibleDCFromDc:从DC创建兼容设备上下文。
100、CreateCompatibleDCFromHandle:从句柄创建兼容设备上下文。
[[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)] public static extern IntPtr FindWindow(string lpClassName, string lpWindowName); [DllImport("user32.dll", SetLastError = true)] public static extern bool GetWindowRect(IntPtr hWnd, out RECT lpRect); [DllImport("user32.dll", SetLastError = true)] public static extern bool GetClientRect(IntPtr hWnd, out RECT lpRect); [DllImport("user32.dll", SetLastError = true)] public static extern bool GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId, out uint lpdwThreadId); [DllImport("user32.dll", SetLastError = true)] public static extern int GetWindowLong(IntPtr hWnd, int nIndex); [DllImport("user32.dll", SetLastError = true)] public static extern bool IsWindowVisible(IntPtr hWnd); [DllImport("user32.dll", SetLastError = true)] public static extern bool EnumChildWindows(IntPtr hWndParent, EnumChildProc lpEnumFunc, IntPtr lParam); [DllImport("user32.dll", SetLastError = true)] public static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam); [DllImport("user32.dll", SetLastError = true)] public static extern int GetClassName(IntPtr hWnd, StringBuilder lpClassName, int nMaxCount); [DllImport("user32.dll", SetLastError = true)] public static extern int GetWindowTextLength(IntPtr hWnd); [DllImport("user32.dll", SetLastError = true)] public static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount); [DllImport("user32.dll", SetLastError = true)] public static extern IntPtr GetParent(IntPtr hWnd); [DllImport("user32.dll", SetLastError = true)] public static extern IntPtr GetWindow(IntPtr hWnd, uint uCmd); [DllImport("user32.dll", SetLastError = true)] [DllImport("user32.dll", SetLastError = true)] public static extern bool IsZoomed(IntPtr hWnd); [DllImport("user32.dll", SetLastError = true)] [DllImport("user32.dll", SetLastError = true)] public static extern bool IsIconic(IntPtr hWnd); [DllImport("user32.dll", SetLastError = true)] [DllImport("user32.dll", SetLastError = true)] public static extern bool IsDialogMessage(IntPtr hWnd, ref MSG lpMsg); [DllImport("user32.dll", SetLastError = true)] public static extern IntPtr GetForegroundWindow(); [DllImport("user32.dll", SetLastError = true)] public static extern short GetKeyState(int nVirtKey); [DllImport("user32.dll", SetLastError = true)] public static extern short GetAsyncKeyState(int nVirtKey); [DllImport("user32.dll", SetLastError = true)] [DllImport("user32.dll", SetLastError = true)] public static extern bool TranslateMessage(ref MSG lpMsg); [DllImport("user32.dll", SetLastError = true)] [DllImport("user32.dll", SetLastError = true)] public static extern IntPtr DispatchMessageW(ref MSG lpMsg); [DllImport("user32.dll", SetLastError = true)] public static extern uint RegisterHotKey(uint id, uint fsModifiers, uint vk); [DllImport("user32.dll", SetLastError = true)] public static extern uint UnregisterHotKey(uint id); [DllImport("user32.dll", SetLastError = true)] [DllImport("user32.dll", SetLastError = true)] public static extern bool GetRawInputData(ref uint pcbSize, IntPtr lpRideVt, uint cbSizeHead, IntPtr hWnd, uint dwFlags); [DllImport("user32.dll", SetLastError = true)] public static extern bool GetRawInputDeviceList(uint puiNumDevices, IntPtr pcbSize, IntPtr ppPrevToplevelWindow); [DllImport("user32.dll", SetLastError = true)] [DllImport("user32.dll", SetLastError = true)] public static extern bool GetRawInputDeviceInfoW(IntPtr hDevice, uint uCmd, IntPtr pData, ref uint pcbSize); [DllImport("user32.dll", SetLastError = true)]