views:

183

answers:

2

Recognising that a bit of interop may be required, how do I send a mouse click event to a window that is currently not being displayed? I have an application that is running as a service and fiddling about with hidden windows and needs to send a mouse click event to one of them, even though it can't actually display the window.

The normal way I would do it is to simply send a click to the screen in the appropriate location, but obviously that method won't work in this case. Ideas?

+1  A: 

Use Spy++ to find out what Windows messages are sent to the window when it's shown and you physically click it with the mouse, and then use SendMessage to send those messages directly.

RichieHindle
Ok, Spy++ has a few helpful hints there, how do I access "SendMessage"?
Nathan Ridley
@Nathan: See http://www.pinvoke.net/default.aspx/user32.SendMessage
RichieHindle
Ooh, cheers for that
Nathan Ridley
+1  A: 

If you can get the HWND of the window, it's pretty easy. Use PostMessage() with WM_LBUTTONDOWN for a left click.

Example: click on position 10,10 (untested, C)

HWND hWnd = (hwnd of window)
WORD mouseX = 10;
WORD mouseY = 10;
PostMessage(hWnd,WM_LBUTTONDOWN,0,MAKELPARAM(mouseX,mouseY));
DaMacc
Nice, I'll give that a go, thanks
Nathan Ridley