1

所以我找不到任何东西可以帮助我解决这个问题,这让我发疯了。

使用Windows Input Simulator (C# SendInput Wrapper - Simulate Keyboard and Mouse)

当我的软件的用户碰巧有一个非标准的 100% 窗口缩放InputSimulator.Mouse.MoveMouseTo(toX, toY);被抛出,移动到一个不正确的位置。

我目前的计算方式:

int toX = (int)((65535.0f * (newX / (float)screen.Bounds.Width)) + 0.5f);
int toY = (int)((65535.0f * (newY / (float)screen.Bounds.Height)) + 0.5f);

例如,如果用户将缩放设置为 125%,我将如何计算正确的 100% 缩放位置?

提前致谢。

4

1 回答 1

0

答案很简单,只是一个令人沮丧的问题需要解决。

首先检测用户是否像这样应用了缩放:

[DllImport("gdi32.dll")]
public static extern int GetDeviceCaps(IntPtr hdc, int nIndex);

public enum DeviceCap { VERTRES = 10, DESKTOPVERTRES = 117 }
public float GetScalingFactor()
{
    Graphics g = Graphics.FromHwnd(IntPtr.Zero);
    IntPtr desktop = g.GetHdc();
    int LogicalScreenHeight = GetDeviceCaps(desktop, (int)DeviceCap.VERTRES);
    int PhysicalScreenHeight = GetDeviceCaps(desktop, (int)DeviceCap.DESKTOPVERTRES);

    float ScreenScalingFactor = (float)PhysicalScreenHeight / (float)LogicalScreenHeight;

    return ScreenScalingFactor; // 1.25 = 125%
}

然后存储比例并将初始坐标除以它。

ScreenScale = Utils.GetScalingFactor();
int toX = (int)((65535.0f * ((x / ScreenScale) / (float)wowScreen.Bounds.Width)) + 0.5f);
int toY = (int)((65535.0f * ((y / ScreenScale) / (float)wowScreen.Bounds.Height)) + 0.5f);
于 2019-09-24T11:28:20.523 回答