15

Setting the "Change the size of all items" slider of Control Panel\Appearance and Personalization\Display to Larger (which changes this registry entry: HKEY_CURRENT_USER\Control Panel\Desktop\DesktopDPIOverride) causes the Control.PointToScreen() method to miscalculate. This can be reproduced using the following Class1 in a Windows Form:

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();
  }
}

Using this control in a WinForm and clicking on it works as expected. Now change "Change the size of all items" to "Larger" and run the code again - the code no longer runs as expected, the PointToScreen method is returning an erroneous value for (0, 0).

Does anybody know how to resolve this issue? Many thanks.

11
  • 2
    Drawing PaintEventArgs.ClipRectangle never makes any sense. Commented Jul 1, 2014 at 16:18
  • 3
    Since Control.PointToScreen is just a wrapper (reference source) to MapWindowPoints API, you are effectively stating that the whole Windows is broken.
    – Ivan Stoev
    Commented Apr 21, 2016 at 17:55
  • 2
    @adv12 What I'm saying is that the issue is not specific to Windows Forms. MapWindowPoints is used in every Windows application (managed, unmanaged, whatever).
    – Ivan Stoev
    Commented Apr 21, 2016 at 19:53
  • 2
    @adv12 - It is GetDCEx() that is broken right now. ControlPaint() uses that function to get a device context for the desktop window to paint on. DPI virtualization is not applied. Probably highly specific to the Windows version, I'd guess this went wrong at Win8.1 and I see it go wrong on Windows 10 version 10586. Not easy to work around and risky to do If you don't want to declare your app dpiAware then it is rather best to call Microsoft Support. Commented Apr 21, 2016 at 20:45
  • 3
    Sigh. No, there isn't any way that dpi virtualization is going to give you the correct view on another app's windows. You must declare your app dpiAware. Commented Apr 21, 2016 at 21:47

1 Answer 1

2

Sounds like you need to make it DPI aware. You can do it like so

[DllImport("user32.dll")]
private static extern bool SetProcessDPIAware();

static void Main()
{
    SetProcessDPIAware();

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.