将 Control Panel\Appearance and Personalization\Display
的“更改所有项目的大小” slider 设置为更大(这会更改此注册表项:HKEY_CURRENT_USER\Control Panel\Desktop\DesktopDPIOverride
) 导致 Control.PointToScreen() 方法计算错误。这可以在 Windows 窗体中使用以下 Class1 重现:
public class Class1 : Control
{
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
Draw(e.ClipRectangle, e.Graphics);
}
private void Draw(Rectangle rect, Graphics graphics)
{
Pen pen = new Pen(Color.Red);
pen.Width = 2;
graphics.DrawRectangle(pen, rect);
}
protected override void OnMouseDown(MouseEventArgs e)
{
base.OnMouseDown(e);
Point p = this.PointToScreen(new Point(0, 0));
ControlPaint.DrawReversibleFrame(new Rectangle(p, new Size(e.X, e.Y)), Color.Yellow, FrameStyle.Dashed);
}
protected override void OnMouseUp(MouseEventArgs e)
{
base.OnMouseUp(e);
this.Invalidate();
}
}
在 WinForm 中使用此控件并单击它会按预期工作。现在将“更改所有项目的大小”更改为“更大”并再次运行代码 - 代码不再按预期运行,PointToScreen 方法返回 (0, 0) 的错误值。
有人知道如何解决这个问题吗?非常感谢。
最佳答案
听起来您需要让它识别 DPI。你可以这样做
[DllImport("user32.dll")]
private static extern bool SetProcessDPIAware();
static void Main()
{
SetProcessDPIAware();
}
关于c# - 使用 DesktopDPIOverride 的 PointToScreen 不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24513090/