在多显示器环境中计算 SendInput() 的归一化坐标

Posted

技术标签:

【中文标题】在多显示器环境中计算 SendInput() 的归一化坐标【英文标题】:Calculate normalized coordinates for SendInput() in a multi-monitor environment 【发布时间】:2020-07-06 15:15:40 【问题描述】:

我想以编程方式将鼠标输入发送到我连接的任何显示器。

我知道我可以使用 SendInput 函数来执行此操作,但我目前的方法不适用于多显示器设置。

我正在使用以下方法:

    获取点击点相对于当前屏幕的像素坐标; 使用 GetMonitorInfoW 函数和适当的 HMONITOR 句柄获取当前屏幕左上角像素的像素坐标; 获取相对于整个虚拟屏幕的像素坐标:为此,我们将当前屏幕边缘的坐标(左上角像素)与点击点的坐标相加; 将生成的像素坐标转换为绝对坐标 (0-65535),供 SendInput 函数使用; 将SendInput 函数与MOUSEEVENTF_ABSOLUTE 一起使用| MOUSEEVENTF_VIRTUALDESK 标志和相对于整个虚拟桌面的绝对坐标。

但是,在我当前的代码中,鼠标输入的位置错误。 我对如何正确计算绝对坐标感到困惑。

目前,我确实使用another question 上讨论的公式检索绝对坐标。 要获取虚拟屏幕的宽度/高度,我使用GetSystemMetrics 和 SM_CXVIRTUALSCREEN/SM_CYVIRTUALSCREEN 参数。

// ________________________________________________
//
// GetAbsoluteCoordinate
//
// PURPOSE: 
// Convert pixel coordinate to absolute coordinate (0-65535).
//
// RETURN VALUE:
// Absolute Coordinate
// ________________________________________________
//
UINT16 GetAbsoluteCoordinate(INT PixelCoordinate, INT ScreenResolution)

    UINT16 AbsoluteCoordinate = ( (65536 * PixelCoordinate) / ScreenResolution ) + 1;
    return AbsoluteCoordinate;



// ________________________________________________
//
// GetAbsoluteCoordinates
//
// PURPOSE: 
// Retrieve virtual screen absolute coordinates from pixel coordinates.
//
// PARAMETERS:
// x, y coordinates passed by reference. These will be changed by the function and used as return values.
//
// RETURN VALUE:
// None (see parameters)
// ________________________________________________
//
void GetAbsoluteCoordinates(INT32 &X, INT32 &Y)

    // Get multi-screen coordinates
    MONITORINFO MonitorInfo =  0 ;
    MonitorInfo.cbSize = sizeof(MonitorInfo);
    if (GetMonitorInfoW(hMonitor, &MonitorInfo))
    
        // 1) Get pixel coordinates of topleft pixel of target screen, relative to the virtual desktop ( coordinates should be 0,0 on Main screen);
        // 2) Get pixel coordinates of mouse cursor, relative to the target screen;
        // 3) Sum topleft margin pixel coordinates with mouse cursor coordinates;
        X = MonitorInfo.rcMonitor.left + X;
        Y = MonitorInfo.rcMonitor.top + Y;

        // 4) Transform the resulting pixel coordinates into absolute coordinates.
        X = GetAbsoluteCoordinate(X, GetSystemMetrics(SM_CXVIRTUALSCREEN));
        Y = GetAbsoluteCoordinate(Y, GetSystemMetrics(SM_CYVIRTUALSCREEN));
    
 



// ________________________________________________
//
// SendMouseInput
//
// PURPOSE: 
// Send mouse input, supporting any of the connected displays
//
// PARAMETERS:
// X, Y are pixel coordinates, relative to the current screen.
// ________________________________________________
//
 void SendMouseInput(INT X, INT Y) 
  
     INPUT Input;
     GetAbsoluteCoordinates(X, Y);
     memset(&Input, 0, sizeof(INPUT)); 
     Input.type = INPUT_MOUSE;
     Input.mi.dx = X;
     Input.mi.dy = Y;
     Input.mi.dwFlags = MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_VIRTUALDESK | MOUSEEVENTF_MOVE | MOUSEEVENTF_LEFTDOWN;
     SendInput(1, &Input, sizeof(Input));
  

【问题讨论】:

【参考方案1】:
    获取相对于整个虚拟屏幕的像素坐标:to do 这个,我们将当前屏幕边距的坐标相加(topleft pixel) 与要点击的点的坐标;

看起来您正在使用相对于监视器的坐标 (X,Y)。你是怎么得到坐标的?

如果是这样,那么样品基本没有问题。如果你的鼠标坐标是通过类似于GetCursorPos的东西得到的,那么你就不需要计算相对于虚拟屏幕的坐标了(是的)。

该示例几乎对我有用,我更改了一些代码如下:

#include <windows.h>
#include <iostream>
using namespace std;
// ________________________________________________
//
// GetAbsoluteCoordinate
//
// PURPOSE: 
// Convert pixel coordinate to absolute coordinate (0-65535).
//
// RETURN VALUE:
// Absolute Coordinate
// ________________________________________________
//
INT GetAbsoluteCoordinate(INT PixelCoordinate, INT ScreenResolution)

    INT AbsoluteCoordinate = MulDiv(PixelCoordinate, 65535, ScreenResolution);
    return AbsoluteCoordinate;


void GetAbsoluteCoordinates(HMONITOR hMonitor, INT32& X, INT32& Y)

    // Get multi-screen coordinates
    MONITORINFO MonitorInfo =  0 ;
    MonitorInfo.cbSize = sizeof(MonitorInfo);
    if (GetMonitorInfoW(hMonitor, &MonitorInfo))
    
        // 1) Get pixel coordinates of topleft pixel of target screen, relative to the virtual desktop ( coordinates should be 0,0 on Main screen);
        // 2) Get pixel coordinates of mouse cursor, relative to the target screen;
        // 3) Sum topleft margin pixel coordinates with mouse cursor coordinates;
        X = MonitorInfo.rcMonitor.left + X;
        Y = MonitorInfo.rcMonitor.top + Y;

        // 4) Transform the resulting pixel coordinates into absolute coordinates.
        X = GetAbsoluteCoordinate(X, GetSystemMetrics(SM_CXVIRTUALSCREEN));
        Y = GetAbsoluteCoordinate(Y, GetSystemMetrics(SM_CYVIRTUALSCREEN));
    


void SendMouseInput(HMONITOR hMonitor, INT X, INT Y)

    INPUT Input[2];
    GetAbsoluteCoordinates(hMonitor, X, Y);
    memset(Input, 0, sizeof(INPUT));
    Input[0].type = Input[1].type = INPUT_MOUSE;
    Input[0].mi.dx = Input[1].mi.dx = X;
    Input[0].mi.dy = Input[1].mi.dy = Y;
    Input[0].mi.dwFlags = MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_MOVE | MOUSEEVENTF_VIRTUALDESK;
    Input[1].mi.dwFlags = MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_LEFTUP | MOUSEEVENTF_MOVE | MOUSEEVENTF_VIRTUALDESK;

    SendInput(2, Input, sizeof(INPUT));

BOOL CALLBACK Monitorenumproc(
    HMONITOR Arg1,
    HDC Arg2,
    LPRECT Arg3,
    LPARAM Arg4)

    SendMouseInput(Arg1, 725, 85);
    return TRUE;

int main(void)

    EnumDisplayMonitors(NULL,NULL, Monitorenumproc,0);
    return 0;

结果:

我有 2 台具有相同显示分辨率 (1920 x 1080) 的显示器进行测试,例如:

示例将在每个监视器上的相同位置单击。

延伸阅读:The Virtual Screen

【讨论】:

错误确实在于我在转换像素坐标之前如何获取它们。感谢您提供代码示例,它有助于解决问题。

以上是关于在多显示器环境中计算 SendInput() 的归一化坐标的主要内容,如果未能解决你的问题,请参考以下文章

如何在 C++ 中快速计算向量的归一化 l1 和 l2 范数?

我想通过 SendInput API 发送一些密钥

将线程输入附加到目标进程后,SendInput 不起作用

图像的归一化互信息Normlized Mutual Information

计算数字的归一化和工程科学记数法的最快算法

对三维点集的归一化变换