关于窗把手的问题
lang: c#.net 2.0
我有一个应用程序 X,它调用 SubWindows 1、2 和 3。
当打开 1、2 或 3 时,我将弹出一个消息框。
我的想法是一个计时器,每 3 秒滴答一次并检查窗户是否打开。
但如何找到这 3 个确切的窗口呢?我可以使用 Window-Handle-Spy-Application 并在代码中对 Window-Handles 进行硬编码以找到它们吗?或者在新打开应用程序 X 时更改窗口句柄?
lang: c#.net 2.0
I have a Application X, that calls SubWindows 1, 2 and 3.
I will Pop up a MessageBox, when 1, 2 or 3 is opened.
My idea is a timer that ticks every 3 seconds and checks if the windows are opened.
But how to find the 3 exact windows? Can I use a Window-Handle-Spy-Application and Hardcode the Window-Handles in my code to find them ever? Or change the window handle when the Application X is opened new?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我将从Win32来回答,因为我对Win32比.Net Framework更熟悉,你可以在.Net Framework中找到合适的函数或使用P/Invoke来调用这些Win32函数。
我假设您知道以下信息:
如果您不知道进程 ID,则需要枚举进程并将每个进程的名称与应用程序 X 进行比较,当获得匹配项时,您就知道了进程 ID。
要找到这三个窗口,我们需要做的第一件事是找到具有进程 ID 的顶层窗口。其中之一将是子窗口的祖先。
使用 EnumWindows() 枚举所有窗口;在枚举过程中,记下具有您要查找的进程 ID 的所有窗口(可能有多个)。
通过与进程 ID 匹配的窗口列表,您需要检查该列表中每个窗口的所有后代。您可以通过使用 EnumChildWindows(); 来做到这一点确保您执行子窗口的所有子窗口等,直到没有更多子窗口为止。这样你就可以覆盖这个过程中的每个窗口。对于您找到的每个窗口,将其与您正在查找的子窗口的已知文本进行比较。当您在某处获得 HWND 的匹配存储时,您可以使用它。
现在您已经有了 HWND,我假设您可以将它们转换为 .Net 可用的 Windows 控件。
我快速搜索了一下,似乎 EnumWindows 不是 .Net Framework 的一部分,因此您必须使用 P/Invoke 来完成这项工作。对您来说,用 C/C++ 编写一个辅助函数来完成所有搜索并仅将 HWND 传回数组中可能会更容易 - 这样您只需通过 P/Invoke 调用一个函数即可完成所有工作搜索可以在 C/C++ 中完成,直接调用 Win32 会更直接。
I'm going to answer in terms of Win32 because I'm more familiar with Win32 than the .Net Framework and you can find the appropriate functions in the .Net Framework or use P/Invoke to call these Win32 functions.
I'm assuming that you know the following:
If you don't know the process id you need to enumerate the processes and compare each process's name to Application X and when you get a match, then you know the process id.
To find the three windows the first thing we need to do is find the top level windows with the process ID. One of these will be the ancestor of the subwindows.
Enumerate all windows using EnumWindows(); During the enumeration make a note of all windows (there may be more than one) that have the process id you are looking for.
With the list of windows matching the process id, you need to check all the descendents of each window in that list. You can do that by using EnumChildWindows(); Make sure you do all child windows of the child windows etc until there are no more. That way you'll cover every window in the process. For each window you find, compare it the known text of the subwindows you are looking for. When you get a match store that HWND somewhere you can use it.
Now that you have the HWNDs, I'm assuming you can turn them into .Net usable Windows controls.
I did a quick search, it seems that EnumWindows is not part of the .Net Framework, so you will have to use P/Invoke to do this work. It may just be easier for you to write a helper function in C/C++ that does all the searching and just passes back the HWNDs in an array - that way you only have to call one function via P/Invoke and all the work for this searching can be done in C/C++ where calling Win32 directly will be more straightforward.