How to send mouse click? [closed]

2019-03-07 03:02发布

问题:

I need to write an app that clicks on windows.

How can I send a left button click to some screen x/y coordinate where a window is located?

回答1:

Use the SendInput() function:

INPUT Inputs[3] = {0};

Inputs[0].type = INPUT_MOUSE;
Inputs[0].mi.dx = ...; // desired X coordinate
Inputs[0].mi.dy = ...; // desired Y coordinate
Inputs[0].mi.dwFlags = MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE;

Inputs[1].type = INPUT_MOUSE;
Inputs[1].mi.dwFlags = MOUSEEVENTF_LEFTDOWN;

Inputs[2].type = INPUT_MOUSE;
Inputs[2].mi.dwFlags = MOUSEEVENTF_LEFTUP;

SendInput(3, Inputs, sizeof(INPUT));

Be sure to read the comments in the MOUSEINPUT documentation regarding how to specify dx and dy correctly when using MOUSEEVENTF_ABSOLUTE in a multi-monitor environment.



回答2:

You can use mouse_event function to click on (x,y):

mouse_event(MOUSEEVENTF_LEFTDOWN, x, y, 0, 0);
mouse_event(MOUSEEVENTF_LEFTUP, x, y, 0, 0);