如何自动执行鼠标操作

时间:2015-02-08 08:35:15

标签: c++ winapi events mouseevent

我需要自动执行一些鼠标操作。

我需要做 mousemove1,lbuttondown1,wait1,mousemove1,lbuttonup1,wait1,
mousemove2,lbuttondown2,wait2,mousemove2,lbuttonup2,wait2,
...

动作必须与屏幕坐标有关。必须接受事件的窗口是此时的顶部窗口。

有一个包含数据的文件 例如

500 450  1000  500 300  2000
600 450  1000  600 300  5000

我尝试做什么

#include <fstream>
#include <vector>
#include <windows.h>

struct A
{
    POINT point1;
    unsigned sleep1;
    POINT point2;
    unsigned sleep2;
    A() { point1.x = point1.y = sleep1 = point2.x = point2.y = sleep2 = 0; }
};

void f(const A &a)
{
    mouse_event(MOUSEEVENTF_LEFTDOWN, a.point1.x, a.point1.y, 0, 0);
    mouse_event(MOUSEEVENTF_MOVE,     a.point1.x, a.point1.y, 0, 0);
    Sleep(a.sleep1);

    mouse_event(MOUSEEVENTF_LEFTUP,   a.point2.x, a.point2.y, 0, 0);
    mouse_event(MOUSEEVENTF_MOVE,     a.point2.x, a.point2.y, 0, 0);
    Sleep(a.sleep2);
}

int main()
{
    std::vector<A> as;

    std::ifstream fin("params.txt");
    if (fin) {
        A a;
        while (fin.good()) {
            fin >> a.point1.x;
            fin >> a.point1.y;
            fin >> a.sleep1;

            fin >> a.point2.x;
            fin >> a.point2.y;
            fin >> a.sleep2;

            if (fin.eof()) {
                break;
            }
            as.push_back(a);
        }
    }

    for (;;) {
        for (const A &a : as) {
            f(a);
        }
    }
}

有些事情正在发生,但我无法理解什么是错误,哪里出错。

2 个答案:

答案 0 :(得分:4)

您的代码存在的问题是您使用的是带有屏幕坐标的mouse_event,而不是标准化的绝对坐标。无论桌面大小如何,标准化的绝对坐标始终位于左上角的(0,0)到右下角的(65535,65535)之间。

以下示例中的MouseTo函数接受屏幕坐标作为输入,然后使用dekstop窗口的大小转换为标准化的绝对坐标。此示例使用SendInput,它取代了mouse_event,但它们都使用相同的坐标。我不确定mouse_event是否可以使用MOUSEEVENTF_VIRTUALDESK标志,但这是用于支持多显示器桌面。

如果您希望构建此示例,请从新的Win32控制台应用程序开始。

#include <Windows.h>
#include <cmath>

void MouseTo(int x, int y) {
    RECT desktop_rect;
    GetClientRect(GetDesktopWindow(), &desktop_rect);
    INPUT input = {0};
    input.type = INPUT_MOUSE;
    input.mi.dwFlags =
        MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_VIRTUALDESK | MOUSEEVENTF_MOVE;
    input.mi.dx = x * 65536 / desktop_rect.right;
    input.mi.dy = y * 65536 / desktop_rect.bottom;
    SendInput(1, &input, sizeof(input));
}

void MouseLButton(bool tf_down_up) {
    INPUT input = {0};
    input.type = INPUT_MOUSE;
    input.mi.dwFlags = tf_down_up ? MOUSEEVENTF_LEFTDOWN : MOUSEEVENTF_LEFTUP;
    SendInput(1, &input, sizeof(input));
}

void MouseLButtonDown() { MouseLButton(true);  }
void MouseLButtonUp()   { MouseLButton(false); }

void AnimatedDrag(const POINT& from, const POINT& to) {
    static const double iteration_dist     = 20;
    static const DWORD  iteration_delay_ms = 1;

    const double dx = to.x - from.x;
    const double dy = to.y - from.y;
    const double dist = sqrt(dx*dx + dy*dy);
    const int count = static_cast<int>(dist / iteration_dist);

    MouseTo(from.x, from.y);
    MouseLButtonDown();

    for(int i=1; i<count; ++i) {
        const int x = from.x + static_cast<int>(dx * i / count);
        const int y = from.y + static_cast<int>(dy * i / count);
        MouseTo(x, y);
        Sleep(iteration_delay_ms);
    }

    MouseTo(to.x, to.y);
    MouseLButtonUp();
}

int main() {
    // minimize console window
    ShowWindow(GetConsoleWindow(), SW_SHOWMINNOACTIVE);
    Sleep(500);

    // Drag whatever is at the window coordinates in "from" to "to"
    const POINT from = {300, 100};
    const POINT to   = {900, 600};
    AnimatedDrag(from, to);
}

答案 1 :(得分:0)

克里斯托弗的答案应该足够了,但对于那些不熟悉C ++的人来说,可能会有点吓人,并且只是试图破解点击实用程序。对于大多数新手来说,这应该很容易破解。

请原谅宏的使用;我正在使用它们使代码的意图更加英语友好。

应该在主显示屏上单击鼠标右键(除非您将X坐标线更改为已注释),然后移动几个像素并左键单击以关闭右键单击菜单提示(如果已创建)。您可以根据自己的自定义要求查看MSDN上还有哪些其他内容。

我一直按住/不点击/移动作为单独的动作,所以像拖动和放大器一样。当开始使用所有正确的成分时,滴剂应该相当直观。

#include <Windows.h>

// Uses absolute coords where the primary display starts at 0,0
// That works well with enumerated monitors structures and their reported coords.
#define QUEUE_MV_MOUSE ip.mi.dwFlags = MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE; 
#define QUEUE_RC_START_MOUSE ip.mi.dwFlags = MOUSEEVENTF_RIGHTDOWN;
#define QUEUE_RC_END_MOUSE ip.mi.dwFlags = MOUSEEVENTF_RIGHTUP;
#define QUEUE_LC_START_MOUSE ip.mi.dwFlags = MOUSEEVENTF_LEFTDOWN;
#define QUEUE_LC_END_MOUSE ip.mi.dwFlags = MOUSEEVENTF_LEFTUP;
#define SEND_IT SendInput(1, &ip, sizeof(ip));
#define VIRTUAL_X_MODIFIER (65536 / GetSystemMetrics(SM_CXSCREEN));
#define VIRTUAL_Y_MODIFIER (65536 / GetSystemMetrics(SM_CYSCREEN));

int main()
{
    INPUT ip;

    ip.type = INPUT_MOUSE;
    ip.mi.mouseData = 0;

    // Change 500 to -500 for a left-hand extended display.
    ip.mi.dx = 500 * VIRTUAL_X_MODIFIER;
    ip.mi.dy = 1000 * VIRTUAL_Y_MODIFIER;

    // Un-comment this Sleep timer if you're debugging in an IDE and need a quick pause.
    // Sleep(500); 
    QUEUE_MV_MOUSE;
    SEND_IT;

    // Various users advise brief Sleep pauses between queued mouse and keyboard events.
    // 500 milliseconds is probably overkill for your automation requirements.
    Sleep(500); 

    QUEUE_RC_START_MOUSE;
    SEND_IT;
    Sleep(500);
    QUEUE_RC_END_MOUSE;
    SEND_IT;

    Sleep(500);

    ip.mi.dx -= 10 * VIRTUAL_X_MODIFIER;
    ip.mi.dx -= 10 * VIRTUAL_Y_MODIFIER;

    QUEUE_MV_MOUSE;
    SEND_IT;

    Sleep(500);

    QUEUE_LC_START_MOUSE;
    SEND_IT;
    Sleep(500);
    QUEUE_LC_END_MOUSE;
    SEND_IT;

    return 0;
}