Reputation: 2963
I need to implement some Spy++-esque functionality for an Windows application automation project.
For simplicity's sake, let us assume that my application A
(written in C# or C++) is trying to automate an existing Win32 Application B
(not a WPF or OpenGL application). Let us also assume that application B
has a picture-box which I'd like to capture. The exact size and location of this picture box inside of B
is not known at runtime, as it is dynamically created and resized by B
. However, one can be certain that the global screen coordinates (x, y)
will always be inside the picture box in question.
I can retrieve the main window handle of B
at runtime using [user32.dll] FindWindow
. However, the picture box inside B
has a HWND which is unknown to my application (A
) at runtime. Can I retrieve the picture box' HWND if I know that it is positioned under the global screen coordinates (x, y)
when B
is maximized and in the foreground?
i.e. something like this:
when being positioned over a specific pair of screen coordinates.
I tried the solution proposed by @David Heffernan in the comments (namely the usage of the function ([user32.dll] WindowFromPoint
)[https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-windowfrompoint], however this did NOT work:
void* window_hwnd = FindWindow(null, "<title of the window>");
SendMessage(window_hwnd, WM_SYSCOMMAND, SC_RESTORE, 0);
ShowWindow(window_hwnd, SW_MAXIMIZE);
SetForegroundWindow(window_hwnd);
SetActiveWindow(window_hwnd);
SetWindowPos(window_hwnd, null, 0, 0, 0, 0, SWP_SHOWWINDOW | SWP_NOMOVE | SWP_NOSIZE);
RedrawWindow(window_hwnd, null, null, RDW_FRAME | RDW_INVALIDATE | RDW_ALLCHILDREN);
void* control_hwnd = WindowFromPoint(300, 300);
This should theoretically give me the HWND to the control at position (x=300, y=300)
. However, control_hwnd
has the same value as window_hwnd
.
Upvotes: 1
Views: 442
Reputation: 2963
Thanks to the comment of @Simon Mourier under my original post, I was able to create a working solution using the Windows UI Automation APIs by means of the FlaUI wrapper library for C#.
I will mark that (for now) as the accepted answer, however, if someone has a better alternative, I will be gladly accepting the respective answer.
Upvotes: -1