簡體   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