C ++使用SetCursorPos在Windows中移动鼠标 [英] C++ move mouse in windows using SetCursorPos

查看:2978
本文介绍了C ++使用SetCursorPos在Windows中移动鼠标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

这是我的代码看起来像SendInput:

This is what my code looks like with 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));

SetCursorPos只有一行:

With SetCursorPos it's just one line:

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.

推荐答案

我知道这是有可能做到的,因为我试过一个智能手机应用程序控制光标,

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

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.

这是在Windows 8 +上的操作。

This is how it is done on Windows 8+.

我尝试使用以下方法:

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;
}

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

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.

这篇关于C ++使用SetCursorPos在Windows中移动鼠标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆