Goal
I am trying to use SendMessage / Post message to send WM_LBUTTONDOWN and WM_LBUTTONUP to a window.
What I've Tried
In the docs it indicates there are some conditions under which this message will not cause a mouse click:
If the mouse is not captured, the message is posted to the window beneath the cursor. Otherwise, the message is posted to the window that has captured the mouse.
To satisfy these conditions I tried called
ShowWindow(..., SW_SHOW)andSetForegroundWindow(...)followed by sending my messages.This seems to bring the window up front, but the clicks don't seem to do anything.
I opened up Spy++ and verified the messages are getting through, and that the coordinates are correct (verified by looking at a manual click at the same x, y).
I was wondering if these messages had to come from within the same process that is being clicked. So I built a DLL and injected it into the process. When I remotely called my function I can see the message is received in Spy++ but still no result.
I've also seen information that you must use
SendInputto simulate mouse and keyboard inputs correctly. I used this method to send a click and it did cause a click right where the cursor was. Regardless of which process I wanted to click on. To send a click at the correct location it seems like I'd need to move the cursor and take control from the user. I was hoping to avoid this loss of user control / battling with the user's own mouse movements however.I am also a little confused because there are tons of answers indicating SendMessage / PostMessage can work for mouse.
Question
What calls do I need to make to satisfy the pre-conditions for a click being received.
Additionally is it possible to click on a window which the user isn't currently focusing? This was my initial goal, but at this point I'd settle for any sort of click. By reading other SO answers it seemed like this was be possible.