关于窗户把手的问题 [英] Questions about Window Handles

查看:116
本文介绍了关于窗户把手的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

lang:c#.net 2.0

lang: c#.net 2.0

我有一个应用程序X,它调用SubWindows 1、2和3。

I have a Application X, that calls SubWindows 1, 2 and 3.

打开1、2或3时,我将弹出一个消息框。

I will Pop up a MessageBox, when 1, 2 or 3 is opened.

我的想法是一个计时器,每3秒计时一次,并检查窗口是否

My idea is a timer that ticks every 3 seconds and checks if the windows are opened.

但是如何找到3个确切的窗口?我可以使用Window-Handle-Spy-Application应用程序并对我的代码中的Window-Handles进行硬编码以找到它们吗?还是在新打开应用程序X时更改窗口句柄?

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?

推荐答案

我打算就Win32回答问题,因为我与.Net Framework相比,Win32更熟悉Win32,您可以在.Net Framework中找到适当的功能,或者使用P / Invoke调用这些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假设您了解以下信息:

I'm assuming that you know the following:


  • 应用程序X的进程ID。

  • 名称(或文本)或Windows 1、2、3,并且这些窗口具有唯一可识别的文本。

如果您不知道进程ID,您需要枚举进程并将每个进程的名称与Application X进行比较,当您找到匹配项时,便知道该进程ID。

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.

要找到三个窗口我们要做的第一件事是找到带有进程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.

使用EnumWindows()枚举所有窗口;其中一个是子窗口的祖先。在枚举过程中,记下所有您要查找的进程ID的窗口(可能有多个)。

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.

并带有与该进程匹配的窗口列表id,您需要检查该列表中每个窗口的所有后代。您可以通过使用EnumChildWindows()来实现。确保您执行所有子窗口等所有子窗口,直到没有更多子窗口为止。这样,您将覆盖过程中的每个窗口。对于找到的每个窗口,将其与要查找的子窗口的已知文本进行比较。当您在某个地方找到具有HWND的火柴库时,就可以使用它。

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.

现在您有了HWND,我假设您可以将它们变成.Net可用的Windows控件。

Now that you have the HWNDs, I'm assuming you can turn them into .Net usable Windows controls.

我进行了快速搜索,看来EnumWindows不属于.Net Framework的一部分,因此您必须使用P / Invoke来完成此工作。您可能会更轻松地用C / C ++编写一个帮助程序函数,该函数执行所有搜索并仅将HWND传回数组中-这样,您只需要通过P / Invoke调用一个函数,并为此完成所有工作搜索可以在C / C ++中完成,直接调用Win32会更简单。

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.

这篇关于关于窗户把手的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆