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:
Form.OnPaint
method will have different e.Graphics.DpiX in depends on what control is in focus, e.g., if Forms is being resizedOnPaint
is called and DpiX == 96, if move mouse over any ButtonOnPaint
is called with DpiX == 120.OnPaint
for UserControl always has DpiX == 120, butthis.CreateGraphics()
returns DpiX == 96.
I still don't get why different components have different Dpi.