SetCursorPos移动鼠标两次?

时间:2013-03-02 05:05:08

标签: c++ windows winapi

我正在制作一个程序,允许用户自定义热键并使它们执行特定的功能。

我做的第一个功能是将光标移动到游戏窗口中的一组特定坐标。它通过查找客户区域然后添加设置坐标然后将光标移动到那里来完成此操作。然而,似乎每次调用SetCursorPos时,它都会移动两次:首先是正确的点,然后是(0,0)。我不确定是什么导致了这一点,我花了几个小时谷歌搜索和调试无济于事。除了它移动两次外,一切正常。我正在使用Windows 8并使用Code :: Blocks进行编译。

脚本在鼠标移动之前和之后输出它应该移动的坐标。它们应该是一样的。当按下热键时它还输出“按键”,释放热键时“按键”。

按F1将使鼠标移动。

#include <iostream>
#include <windows.h>
using namespace std;

class macro{
public:
    int x, y, w, h;
    HWND hWnd;

    HWND get_hWnd(){
        hWnd   =   FindWindow(NULL,"Untitled - Notepad");
        return hWnd;
    }

    void get_Pos(){
        RECT pos;
        GetClientRect(hWnd,(LPRECT)&pos);
        ClientToScreen(hWnd,(LPPOINT)&pos.left);
        ClientToScreen(hWnd,(LPPOINT)&pos.right);
        w   =   pos.right;
        h   =   pos.bottom;
        x   =   pos.left;
        y   =   pos.top;
    }
    void activate(){
        ShowWindow(hWnd, SW_SHOWMAXIMIZED);
    }
    void initialize(){
        get_hWnd();
        get_Pos();
    }
    void mousemove(int rx, int ry){
        get_Pos();
        cout << "Pre-move: " << rx+x << "," << ry+y << endl;
        SetCursorPos(rx + x,ry + y);
        cout << "Post-move: " << rx+x << "," << ry+y << endl;
    }
}macro;

int main(){
    macro.initialize();
    bool loop   =   true;
    bool action_complete    =   true;
    int prev    =   0;
    int curr    =   0;
    bool key_state; // True if down, false if up.

    while(loop){
        if(GetAsyncKeyState(0x70)){
            curr    =   1;
        }
        else{
            curr    =   0;
        }
        if (prev != curr){
            if(curr){
                key_state   =   true;
                macro.mousemove(100,100);
                cout << "key down" << endl;
                Sleep(100);
            }
            else{
                key_state   =   false;
                cout << "key up" << endl;
                Sleep(100);
            }
            prev    =   curr;
        }
    }
}

0 个答案:

没有答案
相关问题