I have vb.net winform app that has AutoScaleMode = dpi AutoScale = false AutoSize = true
I\'ve signed off after changing DPI setting. I also tried restarting the machine
I found solution. My app's manifest has to say it is dpiAware. Because I am trying to detect high DPI and show a warning message box and not really trying to make my app dpi aware, I couldn't do that. You can get dpi aware information from registry: HKEY_CURRENT_USER\Control Panel\Desktop LogPixels.
If you are using default, you won't have the key. Changing DPI setting will create one.
I ran into this problem and found that if you override the OnPaint(PaintEventArgs e)
method of the form, and then get the Graphics object from the argument 'e' i.e. e.Graphics
then the DpiX and DpiY value of this Graphics object is correct.
Unfortunately, the way windows handles DPI scaling is all over the place:
Using g As Graphics = form.CreateGraphics()
Dim dpiX As Single = g.DpiX
This code will only work if user has "Use Windows XP Style DPI Scaling" selected when setting custom DPI. Don't know if that option is even available in the new versions of Windows (8.x and 10) or if they've taken it out.
Your best bet would be to just read the registry:
Dim regUseDpiScaling As Integer
Try 'Use Try / Catch since the reg value may not exist if user using 96 DPI.
regUseDpiScaling = CInt(My.Computer.Registry.GetValue("HKEY_CURRENT_USER\Software\Microsoft\Windows\DWM", "UseDpiScaling", Nothing)) ' if this returns 1, it means users is using modern DPI scaling.
Catch ex As Exception
regUseDpiScaling = 0 ' 0 means no DPI scaling or XP DPI scaling
End Try
If Not (regUseDpiScaling = 0) Then
boolUsesModernDPIScaling = True 'Means you haven't clicked "Use Windows XP Style DPI Scaling" while setting DPI in your system.
Else
boolUsesModernDPIScaling = False
MsgBox("2")
End If
Dim regAppliedDPI As Integer
Try
regAppliedDPI = CInt(My.Computer.Registry.GetValue("HKEY_CURRENT_USER\Control Panel\Desktop\WindowMetrics", "AppliedDPI", Nothing))
Catch ex As Exception
regAppliedDPI = 96
End Try
DPIratioX = regAppliedDPI / 96
DPIratioY = regAppliedDPI / 96
I found that having XP DPI scaling can result in different behavior, so it's good to have the program detect if it's being used.