L
L
Laborant6662020-07-02 17:19:04
C++ / C#
Laborant666, 2020-07-02 17:19:04

How to simulate a mouse click in an inactive application?

For personal purposes, I set myself the task of learning how to click in any application programmatically. For example, you often need to work in several related applications and in order to open them and open the necessary sections or items in them, you need to simulate clicks. I tried to simulate text input in a notepad - it came out through SendMessage, I found the child window Edit easily. But then I wanted to learn how to draw a line in Paint in the same way as if I had drawn a line with the mouse - and here I ran into a problem - text input works, but mouse input does not work. You can't even click anywhere, not a single pixel is drawn by simulating a mouse click in the window. mouse_event is not suitable, because you need to move the cursor with it and so that the application window is active and on top of all windows, but you want to do the opposite - the window is covered by another window, but the click would pass. I also tried to send clicks to other applications and via SendInput, PostMessage - in vain. I ask you for help

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
Laborant666, 2020-07-03
@Laborant666

Applications in Windows do not consist of one window, but of several. And messages should be sent not to any application window, but to a specific
HWND hPaint = ::FindWindow(nullptr, L"Untitled - Paint");
HWND hWnd1 = ::FindWindowEx(hPaint, NULL, L"MSPaintView", nullptr);
HWND hWnd2 = ::FindWindowEx(hWnd1, NULL, L"Afx:00007FF72D900000:8", nullptr);
SendMessage(hWnd2, WM_LBUTTONDOWN, 0, MAKELPARAM(500, 500));
That is, you need to go down to the very bottom along the chain of child windows. To understand which window is used for what, we take Spy ++ and look at the messages of all windows from it and find the deepest one in the nesting doll that receives messages. In my case, the Afx window receives messages about mouse clicks and this is exactly the part where you draw, in Spy it is easy to determine what kind of window is the selection option, if you need to click on an element with a brush or pencil, then this is another window, which we also track by messages . First, the most important window - clicked on the button - the messages popped up. We climb into the windows inside the first main window, take the very first one, open the messages - click - there are no messages - we are looking for another window - if there is - we climb inside this window and so on until the very last

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question