问题
I am curious how spy++ Finder Tool finds out the window handle for the window over which the mouse is. Is there any WIN32 function for getting the handle of the topmost window that occupies a certain pixel on the display?
回答1:
There is a WindowFromPoint() function.
回答2:
Here is a pretty complete example of how to implement the spy++ finder.
http://www.codeproject.com/KB/dialog/windowfinder.aspx
回答3:
There is some nice information on the internals of Spy++ here: http://blogs.msdn.com/b/vcblog/archive/2007/01/16/spy-internals.aspx. It supports DeusAduro's reply, that Spy++ installs global hooks (actually 3 hooks, one of which is WH_CALLWNDPROC).
You can also find some more info on a Spy++-clone here: http://www.codeproject.com/KB/dialog/windowfinder.aspx.
Also, there should be a download for a very similar app in the SDK here: http://msdn.microsoft.com/en-us/library/Aa231779 but it seems to be broken (no download - like so many links on msdn :( ).
Depending on what you want to get (if it is not a hwnd) you can also get an AutomationElement:
System.Windows.Point pt = new System.Windows.Point(System.Windows.Forms.Cursor.Position.X, System.Windows.Forms.Cursor.Position.Y);
AutomationElement ae = AutomationElement.FromPoint(pt);
回答4:
WindowFromPoint or ChildWindowFromPoint API functions.
回答5:
Don't quote me on it, but I believe spy++ would install a WH_CALLWNDPROC hook. This then gets sent all the WM_MOUSEMOVE messages before they reach their target windows. Thus as soon as you mouse over a window, spy++ recieves a message telling it which window.
来源:https://stackoverflow.com/questions/1330074/how-does-spy-find-out-what-is-the-window-at-a-certain-point-on-the-screen