2

I have WinForms applications which started to behave strangely after some Windows Update. So, I set Font scale settings to 125% (120 Dpi). After that application started to shorten text. After investigation I found the cause, method this.CreateGraphics() returns Graphics object with DpiX/DpiY == 96 even though in OnPaint() e.Graphics has DpiX/DpiY == 120. How is it possible?

p.s. if I execute Graphics.FromHwnd in OnPaint(), it still returns 96 Dpi.

p.s2. Dpi virtualization disabled

Update

After additional investigation I'm able to reproduce that behavior in a simple application. By default application is being run with Dpi virtualization (if nothing set in manifest file and SetProcessDPIAware wasn't called).

In order to make mess with Dpi SetProcessDPIAware should be called after Form creation. After that two strange things will happen:

  1. Form.OnPaint method will have different e.Graphics.DpiX in depends on what control is in focus, e.g., if Forms is being resized OnPaint is called and DpiX == 96, if move mouse over any Button OnPaint is called with DpiX == 120.
  2. OnPaint for UserControl always has DpiX == 120, but this.CreateGraphics() returns DpiX == 96.

I still don't get why different components have different Dpi.

Change Windows scaling

  • 1
    I don't think setting font scale will change your dpi... – EpicKip Oct 09 '17 at 10:14
  • 2
    Getting 96 is pretty normal. Be sure to declare your app [to be dpiAware correctly](https://stackoverflow.com/a/13228495/17034). – Hans Passant Oct 09 '17 at 10:32
  • @HansPassant Not, it not normal. It always used to get correct Dpi until last time. 96 is OK when Dpi virtualization enabled, but with disabled one it should return correct number. – Aleksandr Reznichenko Oct 09 '17 at 11:25
  • I don't disagree, but it certainly wasn't obvious from the question that you took care of that. Getting both 96 and 120, hmya, extraordinary claims do normally require extraordinary evidence. – Hans Passant Oct 09 '17 at 11:52
  • So it seems you did not declare it dpiAware correctly. You must call SetProcessDPIAware() *before* you create any window. Just do it right, favor the manifest. – Hans Passant Oct 10 '17 at 09:28
  • @HansPassant: it clear from my update that SetProcessDPIAware() should be called before create form, but I want to understand why different Dpis appear. – Aleksandr Reznichenko Oct 10 '17 at 09:39

0 Answers0