此问题已经在这里有了答案:
所以我找不到任何可以帮助我解决此问题的东西,这使我发疯了。
使用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%缩放位置?
提前感谢。
答案很简单,只是一个令人沮丧的问题要解决。
首先检测用户是否已像这样应用缩放:
[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);