开发者

Questions about Window Handles

开发者 https://www.devze.com 2022-12-22 14:14 出处:网络
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.

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?


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:

  • Process ID of Application X.
  • The name (or text) or Windows 1, 2, 3 and that these windows have uniquely identifiable text.

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消