I successfully figured out how to replace mouse movements and prayer with resolution-independent window handles: use Spy++ to survey the target window handles, and use AutoIt’s WinAPI.au3 lib to effect the automation.
Before we go into the feedback cycle, we’ll do some preliminary steps:
- Launch your target GUI program.
- Launch Spy++ and scroll among the harvest.
- Make note of the window handles.
The feedback loop is
- Write AutoIt functions using WinAPI.au3
- Try it out, sprinkling plenty of ConsoleWrite($hwnd & @CRLF) statements
- Refresh Spy++, Tree->Expand All, and scroll down to the relevant handles
- Repeat from 1.
Your first goal is to get the handle. Nothing else matters before this. Windows originate from a root Desktop window. It reminds me of a DOM tree. When you hear “tree,” think “traverse.” How do you travel among the nodes? Grab a nearby, easy parent:
$hwnd = WinActivate("Easy Title") ConsoleWrite($hwnd & @CRLF) ; compare handle in Spy++
$hwnd = _WinAPI_GetWindow($hwnd, $GW_CHILD) ConsoleWrite($hwnd & @CRLF) $hwnd = _WinAPI_GetWindow($hwnd, $GW_HWNDLAST) ConsoleWrite($hwnd & @CLRF) $hwnd = _WinAPI_GetWindow($hwnd, $GW_CHILD) ConsoleWrite($hwnd & @CRLF) $hwnd = _WinAPI_GetWindow($hwnd, $GW_HWNDLAST) ; are we there yet ?
Once you have the handle, you can try a couple strategies:
- Use _SendMessageA() and emulate sniffed event messages from Spy++
- Use _WinAPI_GetWindowRect(), unpack the coords, and use a targeted MouseClick – maybe a few pixels to the right and down for a stubborn combo box, for example.
Automating GUIs is street-fighting: there are no rules. At the very least, Spy++ and AutoIt will let you be systematic with your throat capture. Resolution independence is possible; free your users to use your app with zero coordinate fiddling!
Spyxx.exe is available in the Visual Studio 2013 Community Edition, but I have not tried it.