4

I want to create the same function "Find windows..." of spy++ in C#. I have try with this function of the WINAPI:

HWND WINAPI WindowFromPoint(__in  POINT Point);

http://msdn.microsoft.com/en-US/library/ms633558.aspx But i don't arrive to get all element with that, because they are disabled or hidden.

For example with the window 7 calculator in Programmer mode, i cannot get the "A B C D E F" with my program if they are disable then spy++ can get it.

Edit: I have try this but it don't working:

[DllImport("user32.dll")]
public static extern ulong GetClassLongPtr(IntPtr hWnd, int nIndex);

[DllImport("user32.dll")]
public static extern IntPtr ChildWindowFromPointEx(IntPtr hWndParent, Win32Point pt, uint uFlags);

IntPtr hWnd = WindowFromPoint(myPoint);
hWnd= ChildWindowFromPointEx(hWnd , myPoint, 0x0000);

myPoint is the position of my mouse.

I don't familiar with the WINAPI and i imagine with your explanation that is a lack of understanding of me. It's possible to have a little example of ChildWindowFromPointEx function or know what my code don't working? thanx for your answer


I have try to create the loop but, it seems the handle is under the other handle but is not children of the handle, the loop send alway the same handle and no the desire child when the key " a b c d e f" is disabled. Do you have another idea?

casperOne
  • 73,706
  • 19
  • 184
  • 253
Gat
  • 115
  • 3
  • 11
  • 1
    For what it's worth, you probably *should* skip invisible windows (just not disabled ones). If you open Spy++, you'll see that the desktop is littered with dozens and dozens of invisible windows - mostly tooltips and menus and dialogs that have been dismissed but are ready to display again when appropriate. You'll generally want to ignore these otherwise they'll interfere and you could end up getting back a HWND that the user can't see anything corresponding to at all! – BrendanMcK Mar 01 '12 at 02:30

1 Answers1

5

WindowFromPoint returns a window handle. Since you are dealing with disabled/hidden windows, you would want to use ChildWindowFromPointEx, passing in hwndParent as whatever handle you obtained from WindowFromPoint.

You might find the following article helpful: http://blogs.msdn.com/b/oldnewthing/archive/2010/12/30/10110077.aspx


In regards to the code you added, ChildWindowFromPointEx takes client coordinates, whereas the mouse position coordinates you have are screen coordinates. You can do the conversion with ScreenToClient.

Note: This is the WinAPI way to do things. I have no idea whether or what APIs C# supplies.

Mike Kwan
  • 24,123
  • 12
  • 63
  • 96
  • thank you, i have try your function but it don't still working for me, i have edit my question for add information if it can help you to see my problem. thank you – Gat Mar 01 '12 at 00:21
  • 1
    Minor clarification: WindowFromPoint will actually return child windows; per MSDN and Raymond Chen's article, it will return the 'most nested' window. However, you're stuck with its policy for ignoring disabled windows; that's the problem here, not the top-level vs child-window issue - so best might be to roll your own version by using ChildWindowFromPointEx in a suitable loop. – BrendanMcK Mar 01 '12 at 02:27
  • Yes BrendanMcK and Mike Kwan, you have reason about the combinaison of ChildWindowFromPointEx and ScreenToClient, i finally arrive to obtain the child of my Window but i have the need to acces to the grandchildren and other, i will post the solution when i will find this – Gat Mar 01 '12 at 09:00
  • I have try to create the loop but, it seems the handle is under the other handle but is not children of the handle, the loop send alway the same handle and no the desire child when the key " a b c d e f" is disabled. Do you have another idea? – Gat Mar 01 '12 at 16:57