.NET forms on Windows 7 at 125% zoom level
If you have ever tried to personalize your Windows 7 desktop and chose to zoom to 125%, as per the following screenshot, you’ll perhaps notice that some applications do not display so well:
Believe it or not, the 125% or 150% zoom mode does not simply decrease the dpi of the display and make it look bigger, unlike the advanced display settings in Windows XP:
In fact, when the 125% or 150% zoom mode is set, Windows 7 does (at least) the following two things:
- Increase the default font size
- Tell every window (including child window) to increase its size
It’s up to the application to handle the change in font size and window size gracefully. If it does not, the application interface may look distorted, with text truncated or displayed outside its designated area and graphics being tiled up or stretched.
Interestingly the 125% or 150% zoom mode cannot be used during a Remote Desktop session.
Effects on .NET forms
Under 125% or 150% zoom, the size of a .NET form will increase proportionally. The following changes will have to be made for the form to display properly:
- All form controls should have their Anchor property set appropriately.
- Most importantly, the AutoScaleMode property of every form should be set to Dpi and not Font. AutoScaleMode of user controls can be set to either Dpi or Inherit.
If set inappropriately, only the form will resize while the controls do not resize, causing display distortion.
Also, do not open the form designer from inside Visual Studio 2008 under 125% or 150% zoom mode. Otherwise, Visual Studio will “zoom” the form (perhaps in an attempt to obey the Windows settings faithfully) by increasing its Size property and forgets (or is unable) to restore the original size when the zoom mode is set to 100%. If this happens, you’ll have to manually resize every form back to their original size.
I am not sure if it’s a Visual Studio bug, or it’s just supposed to be that way.
Also, in Visual Studio 2010 designer, if you create custom User Controls and place those on a form using "Medium (125%)" display setting they will, under the "Default" setting have exaggerated widths (scaled using the DPI issue mentioned in this article, no doubt). Meaning, your user control will, likely, wind up with some right side clipping! My Error Provider was on the right side and got clipped. But it was so subtle and all the other controls looked great. New levels of frustrated "works on my machine". Good lord, that was TRICKY to figure out!!! Bug, me thinks
Todd, thanks for sharing your findings with me
I too was bit with this anomaly of having the display settings in Windows set to 125% on my development machine. After two days of running my budding application on different machines set at 100% resulting in gross distortions and not believing what I was seeing, I finally installed Visual Studio on them so I could check what was really happening in the designer and debugger. I finally realized that pixels weren't pixels and I stumbled on to my 125% setting in Windows Display optins.. Thank you for posting this problem.
Thank you so much… this was a problem but now working like a charm.
Is there a way of checking what the text size is set to, something along the lines of checking the screen height, i.e.
ScreenHeight = Screen.PrimaryScreen.Bounds.Height
I have an application that needs to re-position GroupBoxes depending on the number of GroupBoxes that are being displayed; the user has the ability to dynamically turn groupboxes on and off. when the display is set to anything other than 100% the positions used is wrong.
I can't rely on the anchor as its anchor position can change.
Hi Martin,
Unfortunately till this day, I could not find a way to determine the 100% or 125% settings from code. The only workaround other than using the Anchor property is to paint the controls manually.
Hi,
Well I've had a search and this seems to be one way of doing it.
What I have found is if I test to see if the text size is set to 120% and if it is I can multiply my X and Y positions by 1.25 and they are then positioned correctly.
I assume that I can do the same for 150% although I haven't tested that.
Imports Microsoft.Win32
Dim ScreenMagnification As RegistryKey
Dim pRegKey As RegistryKey = My.Computer.Registry.CurrentUser
pRegKey = pRegKey.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\ThemeManager", False)
ScreenMagnificationValue = pRegKey.GetValue("LastLoadedDPI")
If ScreenMagnificationValue = "120" Then
XOffSet = 1.25
YOffSet = 1.25
ElseIf ScreenMagnificationValue = "150" Then
XOffSet = 1.5
YOffSet = 1.5
Else
XOffSet = 1
YOffSet = 1
End If
is there a reason for Dimming ScreenMagnification as RegistryKey and then not referencing it again? I’m a bit confused.
Hi Martin,
Thanks for the solution! For creating a ‘ControlPaint.DrawReversibleFrame’ window (e.g. for selection or zooming in) you need to use ‘PointToScreen()’ methods that don’t take the DPI scaling into account.
For this I need to manually correct is, and thanks to your solution I now can:
Dim ScreenMagnificationValue = pRegKey.GetValue(“LastLoadedDPI”)
Dim dpiFactor = CSng(ScreenMagnificationValue) / 96