那么窗口句柄又该怎么获取,一直找不到一个看得懂的语句。
2条回答 默认 最新
- 真的小白啊 2021-08-11 16:24关注
获取窗口句柄也是容易的
EnumWindows
https://docs.microsoft.com/zh-cn/windows/win32/api/winuser/nf-winuser-enumwindowsprivate static void Main(string[] args) { List<Window> windows = Window.GetWindows(); foreach (Window window in windows) { Console.WriteLine(window); } Console.ReadKey(); }
public delegate bool EnumWindowsProc(IntPtr hwnd, IntPtr lParam); public class Window { private static List<Window> _windows = new List<Window>(); public string ClassName { get; set; } public IntPtr HWnd { get; set; } public string Title { get; set; } public override string ToString() { return $"标题:{Title}\n" + $"类名:{ClassName}\n" + $"句柄:{HWnd}\n"; } public static List<Window> GetWindows() { _windows.Clear(); EnumWindows(EnumWindowsFunc, IntPtr.Zero); return _windows; } private static bool EnumWindowsFunc(IntPtr hwnd, IntPtr lParam) { if (hwnd == IntPtr.Zero) { return false; } else { if (IsWindow(hwnd) != 0) { StringBuilder buffer = new StringBuilder(256); Window window = new Window { HWnd = hwnd }; _ = GetClassNameW(hwnd, buffer, buffer.Capacity); window.ClassName = buffer.ToString(); buffer.Clear(); _ = GetWindowTextW(hwnd, buffer, buffer.Capacity); window.Title = buffer.ToString(); _windows.Add(window); } return true; } } [DllImport("User32.dll")] private static extern bool EnumWindows(EnumWindowsProc lpEnumFunc, IntPtr lParam); [DllImport("User32.dll")] private static extern int IsWindow(IntPtr hWnd); [DllImport("User32.dll", CharSet = CharSet.Unicode)] private static extern int GetClassNameW(IntPtr hWnd, StringBuilder lpClassName, int nMaxCount); [DllImport("User32.dll", CharSet = CharSet.Unicode)] private static extern int GetWindowTextW(IntPtr hWnd, StringBuilder lpString, int nMaxCount); }
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报
悬赏问题
- ¥15 有人会SIRIUS 5.8.0这个软件吗
- ¥30 comsol仿真等离激元
- ¥15 静电纺丝煅烧后如何得到柔性纤维
- ¥15 (标签-react native|关键词-镜像源)
- ¥100 照片生成3D人脸视频
- ¥15 伪装视频时长问题修改MP4的时长问题,
- ¥15 JETSON NANO
- ¥15 VS开发qt时如何在paintgl函数中用pushbutton控制切换纹理
- ¥20 关于 openpyxl 处理excel文件地问题
- ¥15 MS中不知道高分子的构型怎么构建模型