Detecting Windows 10 Scaling Percentage (or DPI) (1 Viewer)

Derevon

Registered User.
Local time
Today, 03:15
Joined
Jan 14, 2014
Messages
51
I have a form that doesn't really fit fully on the screen with scaling set to 125% or more in Windows 10 in Full HD resolution, so I want another form to be loaded instead if users have scaling over 100%.

I already have code which checks if users have lower screen resolution, but I haven't been able to find a working way to deal with Windows scaling.

I found some code that's obtaining your current DPI, but for some reason it always seems to show 96 regardless of what scaling I use.

I have dual monitors and have tried changing scaling to 125% and 150% on both, but the number shown will always be 96 for some reason.

Does anyone know how to properly check this in another way? Perhaps by reading a Windows registry key or something like that?

Thank you

Code:
Option Compare Database
Option Explicit

Private Const LOGPIXELSX As Long = 88

Private Declare Function GetDeviceCaps Lib "gdi32.dll" ( _
ByVal hdc As Long, _
ByVal nIndex As Long) As Long

Private Declare Function GetDC Lib "user32.dll" ( _
ByVal hWnd As Long) As Long

Declare Function ReleaseDC Lib "user32.dll" ( _
ByVal hWnd As Long, _
ByVal hdc As Long) As Long

Public Function GetDPI() As Long

Dim hdcScreen As Long
Dim iDPI As Long

iDPI = -1
hdcScreen = GetDC(0)
If (hdcScreen) Then
iDPI = GetDeviceCaps(hdcScreen, LOGPIXELSX)
ReleaseDC 0, hdcScreen
End If

GetDPI = iDPI

End Function
 

sonic8

AWF VIP
Local time
Today, 03:15
Joined
Oct 27, 2015
Messages
998
I found some code that's obtaining your current DPI, but for some reason it always seems to show 96 regardless of what scaling I use.
You are using the correct approach to detect scaling and your code appears to be correct in my eyes.


I copied your code and executed it without the slightest change. It returns 120 DPI on my machine which is expected for the 125% scaling I use.
 

isladogs

MVP / VIP
Local time
Today, 02:15
Joined
Jan 14, 2017
Messages
18,216
It might be worth pointing out that if you change that scaling factor, GetDPI will only show the updated value after you log out & in again.

Once that is done, I agree with Phillip that it shows the correct values
 

Users who are viewing this thread

Top Bottom