查找包含进程特定文本的窗口
我正在尝试查找某个进程是否已打开特定的窗口。该过程会产生多个窗口,我需要检查所有窗口。
我很容易找到流程,
foreach (Process p in Process.GetProcesses())
{
if (p.MainModule.FileName.ToLower().EndsWith("foo.exe"))
FindChildWindowWithText(p); //do work
问题是下一步该怎么做。我无法使用 Process'MainWindowText
,因为它会随着激活的窗口而变化。
然后我尝试使用 Windows 函数 EnumChildWindows
和 GetWindowText
,但我不确定是否向 EnumChildWindows 传递了正确的句柄。当传递 MainWindowHandle 时,EnumChildWindows
按预期工作,但当然 MainWindowHandle 会随着活动窗口的变化而变化。所以我传递了 Process.Handle,但是在切换应用程序窗口时我得到了不同的句柄和不同的结果。 (我知道 EnumChildWindows 不仅返回 windows 的句柄,还返回 .net 中的 controls,如果我也能获取窗口的标题,那就没问题了)
也许我是以错误的方式执行此操作,我需要一种不同的方法 - 同样,我的问题就像找到一个包含与特定正则表达式匹配的文本的窗口一样简单。所以我可能需要一个函数来枚举在任务栏等中可见的所有窗口。
谢谢
I'm trying to find if a window with specific has been open by a Process. That process spawns multiple windows, and I need to check them all.
I have no trouble finding the process, with
foreach (Process p in Process.GetProcesses())
{
if (p.MainModule.FileName.ToLower().EndsWith("foo.exe"))
FindChildWindowWithText(p); //do work
the problem is what to do next. I cannot use Process' MainWindowText
, because it changes with whichever window is activated.
Then I've tried to use Windows function EnumChildWindows
and GetWindowText
, but I am not sure if I'm passing a correct handle to EnumChildWindows. The EnumChildWindows
works as expected when passed MainWindowHandle, but of course the MainWindowHandle changes with active window. So I passed Process.Handle
, but I get different handles and different results when switching the app's windows. (I understand that EnumChildWindows returns handles to not only windows, but controls in .net speak, that's no problem if I could get the caption of the window too)
Maybe I am doing this the wrong way and I need a different approach - again, my problem is as simple as finding a window with text that matches specific regular expression. So I would probably need a function that enumerates all windows, that are visible in the taskbar or so.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
拥有进程后,您可以枚举进程中的所有窗口并测试其中是否有任何窗口与您要查找的窗口匹配。
您将需要以下 P/Invoke 声明
以下是一对可用于在特定进程中查找窗口的函数的示例,我从您的问题中了解到您有进程,问题是枚举窗口。
然后您可以调用FindWindowInProcess函数来查找标题以“ABC”结尾的窗口作为示例。
当然你可以替换 s => s.EndsWith("ABC") 与任何满足窗口搜索条件的表达式,它可以是正则表达式等。
这也是 FindThreadWindow 的一个版本,它也将检查子窗口的第一级。如果您的窗口位于层次结构的更深处,您可以更进一步,将其设为递归函数。
Once you have the Process, you can enumerate all the Windows in the process and test if any of them match the window you are looking for.
You will need the following P/Invoke declarations
The followng is an example of a pair of functions that can be used to find the windows in a specific process, I understood from your question that you have the Process, the problem is enumerating the windows.
Then you can call the FindWindowInProcess function to find a window that's title ends with "ABC" as an example.
Of course you can replace s => s.EndsWith("ABC") with any expression that will satisfy your search criteria for the window, it could be a regex etc.
Here is also a version of FindThreadWindow that will also check the first level of child windows. You could take this further and make it a recursive function if your windows is deeper down in the hierarchy.
我不是枚举进程并查找窗口,而是枚举窗口(使用 EnumWindows)并查找进程(使用 GetGuiThreadInfo)。
Rather than enumerating processes and finding the window, I'd enumerate the windows (using EnumWindows) and find the process (using GetGuiThreadInfo).