c# - 使用 DesktopDPIOverride 的 PointToScreen 不正确

标签 c# windows winforms

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/

相关文章:

c# - 在抽象基的构造函数中创建新的派生类型

c# - 如何使用 Cairo-Sharp 将 Pixbuf 绘制到表面上?

windows - 系统如何定义进程获得的虚拟内存部分?

c# - 以基本形式定义的自定义属性在重建时以继承形式丢失其状态

c# - 参数 varbinary 数据类型中的空值

c# - 在 C# 中显示来自 Presenter 的表单

c# - 在 C#/.NET 中执行批量更新的最快方法

c# - 上传到 Azure Blob 存储时出现 400 错误

c# - 等待进程完成然后显示消息 (C#)

python - "ImportError: No module named serial"- 安装 pyserial 之后