使用 C++ 编写鼠标移动脚本 <Windows.h>

问题描述 投票:0回答:0

所以。这是我的第一篇文章。我正在编写一个脚本来以圆周运动的方式移动鼠标,并使用一些额外的参数作为用户输入。下面你可以看到我正在处理的功能的片段。光标停留在一个点上并一直停留到最后一个 for 循环结束并且我的程序的其余部分运行为止。怎么了?

void move_mouse_circle()
{

    int steps = 500; // init default values everything
    int radius = 200;
    float degree = 0;
    bool add_inconsistency;
    char add_incon_answ;
    
    std::cout << "Please, provide the amount of steps for smoothness (default is at " << steps << "): ";
    std::cin >> steps;
    std::cout << "Please, provide the radius of the desired circle (default is at " << radius << "): ";
    std::cin >> radius;
    std::cout << "Do you want to add inconsistency to mimic human input? y/n: ";
    std::cin >> add_incon_answ;
    if (add_incon_answ == 'y')
    {
        add_inconsistency = true;
    }
    else
    {
        add_inconsistency = false;
    }
    
    /* float x_coord;
    float y_coord; */
    float *x_coord = new float[steps];
    float *y_coord = new float[steps];
    
    float degree_increm = 360 / steps; // define degree increment per step
    
    for (int i = 0; i < steps; i++)
    {
        // float radian = degree_increm / (2 * M_PI); // converting degrees to radians
        x_coord[i] = radius * cos(degree);
        y_coord[i] = radius * sin(degree);
    
        degree = +degree_increm;
    }
    
    std::cout << "Starting in 3 seconds... ";
    Sleep(1000);
    std::cout << "2 seconds... ";
    Sleep(1000);
    std::cout << "1 second... " << std::endl;
    Sleep(990);
    
    float curr_x_coord; // define var for current cursor pos for x axis
    float curr_y_coord; // define var for current cursor pos for y axis
    
    POINT curr_cursor_p; // POINT typedef contains x and y long
    GetCursorPos(&curr_cursor_p);
    curr_x_coord = curr_cursor_p.x;
    curr_y_coord = curr_cursor_p.y;
    
    for (int u = 0; u < steps; u++)
    {
        SetCursorPos(x_coord[u] + curr_x_coord, y_coord[u] + curr_y_coord);
        if (add_inconsistency == true)
        {
            float millis = 2 + ((1 + rand() % 50) / 100);
            Sleep(millis);
        }
        else
        {
            Sleep(2);
        }
    };
    
    delete[] x_coord;
    delete[] y_coord;

};

试过修改参数,简化表达式等,还是不行。我想保留数学方法,所以用数学方程式来解决这个编程问题。

c++ winapi scripting mouse
© www.soinside.com 2019 - 2024. All rights reserved.