【问题标题】:How to handle SendInput mouse movement with windows scaling? [duplicate]如何使用窗口缩放处理 SendInput 鼠标移动? [复制]
【发布时间】:2019-09-22 23:10:09
【问题描述】:

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

使用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% 缩放位置?

提前致谢。

【问题讨论】:

  • 创建 dpiAware 程序已经很重要了 18 年。随着 Win10 的发布,它现在变得至关重要,它的安装程序不再选择 96 dpi(又名 100%)作为默认值。以适当的修复结束。

标签: c# dpi sendinput dpi-aware


【解决方案1】:

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

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

[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);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-10-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-05
    • 2020-08-08
    相关资源
    最近更新 更多