tags:

views:

365

answers:

2

I would like to send some keystrokes from a C++ program into another window.

For that reason I would like to have the user select the target window similar to how it is done in the Spy++ utility that comes with Visual Studio (drag a crosshair cursor over target window and have target window highlighted by a frame).

How is this dragging and selecting done in Windows? I am completely lost as to where I might start to look for a mechanism to implement this feature.

+1  A: 

Here's how it's usually done:

  1. Capture the mouse using SetCapture. This will cause all mouse messages to be routed toward your app's window.
  2. Handle the WM_MOUSEMOVE message. In your handler code, grab the window underneath the mouse using WindowFromPoint. That will get you the HWND of the window the mouse is currently over.
  3. Now that you've got the window, you need a device context (HDC). You can get one using GetWindowDC for the specified window.
  4. Now you can draw into the DC using typical GDI functions.

There are some things you have to look out for - cleanly erasing the selection rectangle and so forth, but that's one way to do it.

You could also draw into a screen DC to do this, but in any case you'll need the window handle in order to get the window rect.

If you Google around Spy++ source code you'll see a few examples of this technique.

James D
Problem with this approach is that DWM (in particular Aero) doesn't know about your nicely drawn images and obliterates them at random.
Ed Guiness
A: 

Formers answers are wrong.

Spy++ source code has been given on G. Groups for years (see mainly Win32 api ng news://194.177.96.26/comp.os.ms-windows.programmer.win32)

I'm familiar with the Spy++ code, but there's more than one way to do this. You can do it out of process, using the technique described above, or you can do it in-process, using hooks, DLL injection, and subclassing. Which I believe is what Spy++ does. But it's not strictly necessary for painting.
James D