前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >使用 EnumWindows 找到满足你要求的窗口

使用 EnumWindows 找到满足你要求的窗口

作者头像
walterlv
发布2023-10-22 10:06:10
3520
发布2023-10-22 10:06:10
举报

在 Windows 应用开发中,如果需要操作其他的窗口,那么可以使用 EnumWindows 这个 API 来枚举这些窗口。

本文介绍使用 EnumWindows 来枚举并找到自己关心的窗口(如 QQ/TIM 窗口)。


EnumWindows

你可以在微软官网了解到 EnumWindows

要在 C# 代码中使用 EnumWindows,你需要编写平台调用 P/Invoke 代码。使用我在另一篇博客中的方法可以自动生成这样的平台调用代码:

我这里直接贴出来:

1 2

[DllImport("user32.dll")] public static extern int EnumWindows(WndEnumProc lpEnumFunc, int lParam);

遍历所有的顶层窗口

官方文档对此 API 的描述是:

Enumerates all top-level windows on the screen by passing the handle to each window, in turn, to an application-defined callback function.

遍历屏幕上所有的顶层窗口,然后给回调函数传入每个遍历窗口的句柄。

不过,并不是所有遍历的窗口都是顶层窗口,有一些非顶级系统窗口也会遍历到,详见:EnumWindows 中的备注节

所以,如果需要遍历得到所有窗口的集合,那么可以使用如下代码:

1 2 3 4 5 6 7 8 9 10 11 12 13

public static IReadOnlyList<int> EnumWindows() { var windowList = new List<int>(); EnumWindows(OnWindowEnum, 0); return windowList; bool OnWindowEnum(int hwnd, int lparam) { // 可自行加入一些过滤条件。 windowList.Add(hwnd); return true; } }

遍历具有指定类名或者标题的窗口

我们需要添加一些可以用于过滤窗口的 Win32 API。以下是我们即将用到的两个:

1 2 3 4 5 6 7

// 获取窗口的类名。 [DllImport("user32.dll")] private static extern int GetClassName(int hWnd, StringBuilder lpString, int nMaxCount); // 获取窗口的标题。 [DllImport("user32")] public static extern int GetWindowText(int hwnd, StringBuilder lptrString, int nMaxCount);

于是根据类名找到窗口的方法:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18

public static IReadOnlyList<int> FindWindowByClassName(string className) { var windowList = new List<int>(); EnumWindows(OnWindowEnum, 0); return windowList; bool OnWindowEnum(int hwnd, int lparam) { var lpString = new StringBuilder(512); GetClassName(hwnd, lpString, lpString.Capacity); if (lpString.ToString().Equals(className, StringComparison.InvariantCultureIgnoreCase)) { windowList.Add(hwnd); } return true; } }

使用此方法,我们可以传入 "txguifoundation" 找到 QQ/TIM 的窗口:

1

var qqHwnd = FindWindowByClassName("txguifoundation");

要获取窗口的标题,或者把标题作为过滤条件,则使用 GetWindowText

在 QQ/TIM 中,窗口的标题是聊天对方的名字或者群聊名称。

1 2

var lptrString = new StringBuilder(512); GetWindowText(hwnd, lptrString, lptrString.Capacity);


参考资料

本文会经常更新,请阅读原文: https://blog.walterlv.com/post/find-specific-window-by-enum-windows.html ,以避免陈旧错误知识的误导,同时有更好的阅读体验。

本作品采用 知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议 进行许可。欢迎转载、使用、重新发布,但务必保留文章署名 吕毅 (包含链接: https://blog.walterlv.com ),不得用于商业目的,基于本文修改后的作品务必以相同的许可发布。如有任何疑问,请 与我联系 ([email protected])

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-04-30,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • EnumWindows
  • 遍历所有的顶层窗口
  • 遍历具有指定类名或者标题的窗口
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档