繁体   English   中英

如何在C ++中模拟鼠标光标的移动

[英]How to simulate mouse cursor movement in C++

我正在利用业余时间创建一个程序,并试图模拟鼠标光标的移动。

我试图这样做,以便在我启动程序时将光标从[x,y]移动到[0,0](这是屏幕的左上角)。

反正有没有不传送就可以做到这一点吗?

到目前为止,这是我的鼠标光标移动程序:

POINT p;
GetCursorPos( &p );
double mouseX = p.x;
double mouseY = p.y;
SetCursorPos(0, 0);

有什么方法可以真正看到我的鼠标正在移动,而不仅仅是立即传送到[0,0]?

您将需要一次逐渐逐步移动鼠标。 例如,考虑以下伪代码功能:

def moveMouse (endX, endY, stepCount, stepDelay):
    GetCurrentPosTo(startX, startY);
    for step = 1 to stepCount
        currX = startX + (endX - startX) * step / stepCount
        currY = startY + (endY - startY) * step / stepCount
        SetCurrentPosFrom(currX, currY)
        DelayFor(stepDelay)
    endfor
enddef

这将计算当前位置(循环内),作为从(startX, startY)(endX, endY)行程的一部分,并根据您希望执行的步骤数进行调整。

因此,使用100的stepCount和10毫秒的stepDelay ,鼠标光标将平稳地移动一秒钟。

可能还有其他可能性,例如以特定速度而不是花费特定时间移动光标,或者指定最小速度和最大时间来组合这两种方法。

我将其作为一项额外的练习。 可以说,这涉及一次只移动光标一点的相同方法,而不仅仅是立即将其位置设置为最终值。

您将不得不多次调用SetCursorPos,坐标首先靠近您的点,然后逐渐靠近(0,0)。 没有任何故意的延迟,它似乎只是立即发生,所以请记住这一点。

这是我在网上的小调料!

它从屏幕中心向外以阿基米德螺旋线旋转鼠标,并且相当流畅。 您还可以在循环中弄乱数学,特别是`cos()`和`sin()`函数可以使它执行不同的动作。 仅出于教育目的。

请享用 :)

#include <Windows.h>
#include <iostream>

void moveMouse(int x, int y){

    int count = 800;
    int movex, movey;
    float angle = 0.0f;

    // set mouse at center screen
    SetCursorPos(x/2, y/2); 

    // begin spiral! :)
    for(int i = 0; i <= count; i++){
        angle = .3 * i;
        movex = (angle * cos(angle) * 2) + x/2;
        movey = (angle * sin(angle) * 2) + y/2;
        SetCursorPos(movex, movey);
        Sleep(1);
    }

}

int main(){
    int Height = GetSystemMetrics(SM_CYSCREEN);
    int Width = GetSystemMetrics(SM_CXSCREEN);
    moveMouse(Width,Height);
    return 0;
}

尝试使用此代码调试输入。.将其放在每帧运行的位置。 获取鼠标位置,然后设置鼠标位置。 您的鼠标应该不动或数学错误。

{
    POINT       pos;
    GetCursorPos(&pos);

    INPUT input[1];
    memset(&input, 0, sizeof(input));

    int left    = GetSystemMetrics(SM_XVIRTUALSCREEN);
    int top     = GetSystemMetrics(SM_YVIRTUALSCREEN);
    int width   = GetSystemMetrics(SM_CXVIRTUALSCREEN);
    int heigth  = GetSystemMetrics(SM_CYVIRTUALSCREEN);

    // 0x1000 because 0 to 0xffff is not 65535, its 65536.
    // we add 0.5f to the location to put us in the center of the pixel. to avoid rounding errors.  Take it out and your mouse will move up and to the left.
    POINT Desired;
    Desired.x = ((float)(pos.x - left ) + 0.5f) * (float) (0x10000) / (float) (width);
    Desired.y = ((float)(pos.y - top) + 0.5f) * (float) (0x10000) / (float) (heigth);

    // move to new location
    input[0].type           = INPUT_MOUSE;
    input[0].mi.dx          = Desired.x;
    input[0].mi.dy          = Desired.y;
    input[0].mi.mouseData   = 0;
    input[0].mi.dwFlags     = MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE_NOCOALESCE | MOUSEEVENTF_MOVE | MOUSEEVENTF_VIRTUALDESK;
    input[0].mi.time        = 0;

    SendInput(1, &input[0], sizeof(INPUT));
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM