C++ 使用 SetCursorPos 在窗口中移动鼠标

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22259936/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 00:01:11  来源:igfitidea点击:

C++ move mouse in windows using SetCursorPos

c++windowsmousemovesendinput

提问by user3394180

I created a device similar to a wiimote and i want to use it as a mouse in windows (8.1). The device connects over tcp to a c++ win32 program on my windows computer and sends the position where the mouse cursor should move. I am using the SetCursorPos function to set the position, which works great to control most programs. But when I try to control for example the task manager, the cursor doesn't move anymore. When I switch from the task manager back to some other program it works again. I also tried to use the SendInput function with the same results.

我创建了一个类似于 wiimote 的设备,我想在 Windows (8.1) 中将它用作鼠标。该设备通过 tcp 连接到我的 Windows 计算机上的 c++ win32 程序,并发送鼠标光标应移动的位置。我正在使用 SetCursorPos 函数来设置位置,这对于控制大多数程序非常有效。但是当我尝试控制例如任务管理器时,光标不再移动。当我从任务管理器切换回其他程序时,它再次工作。我还尝试使用具有相同结果的 SendInput 函数。

This is what my code looks like with SendInput:

这是我使用 SendInput 的代码的样子:

INPUT Input = { 0 };
Input.type = INPUT_MOUSE;

Input.mi.dx = (LONG)posX;
Input.mi.dy = (LONG)posY;

// set move cursor directly
Input.mi.dwFlags = MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE;

SendInput(1, &Input, sizeof(INPUT));

With SetCursorPos it's just one line:

使用 SetCursorPos 它只是一行:

SetCursorPos(posX, posY);

Can anybody tell me why it doesn't work for some programs? I know it has to be possible to do this, since I tried a smartphone app which controls the cursor and it worked in all programs.

谁能告诉我为什么它不适用于某些程序?我知道必须有可能做到这一点,因为我尝试了一个控制光标的智能手机应用程序,它可以在所有程序中运行。

采纳答案by Brandon

You cannot set the cursor position or input of a window that required higher privileges than your program has..

您不能设置需要比您的程序具有更高权限的窗口的光标位置或输入。

If you want your program to be able to move the cursor over task manager, you require the same privileges as task manager: Administrator Privileges.

如果您希望您的程序能够将光标移动到任务管理器上,您需要与任务管理器相同的权限:管理员权限。

This is how it is done on Windows 8+.

这是在 Windows 8+ 上完成的方式。

I tried it with the following:

我尝试了以下方法:

int main()
{
    HWND window = FindWindow("TaskManagerWindow", "Task Manager");
    if (window)
    {
        RECT rect = {0};
        GetWindowRect(window, &rect);

        SetForegroundWindow(window);
        SetActiveWindow(window);
        SetFocus(window);
        Sleep(300);
        SetCursorPos(rect.right - 200, rect.bottom - 200);
    }

    return 0;
}

Cursor only moves over task manager when ran as admin. It is the same for all context menus and windows in Windows 8+. Not just task manager.

当以管理员身份运行时,光标只会在任务管理器上移动。Windows 8+ 中的所有上下文菜单和窗口都是一样的。不仅仅是任务管理器。

回答by Shagun verma - Developer

#include <Windows.h>

int main()
{
    SetCursorPos(200, 200);
    return 0;
}