API to find out the currently used display percentage

JedNebula
JedNebula used Ask the Experts™
on
I am looking for a method where I can find the display percentage used in Windows 7, 8 and 10. I'm fairly sure this is NOT the same as the magnification level. It is making all forms size in a weird way in my projects but I'm finding more and more people are using this feature (especially with Tablets) and so more and more of my forms look strange.

I'm using VB6 and VBA still so I could do with examples formatted for this language.

Below, I have put an image of where you change the setting and you'll see it is not the same as Magnification - a subject for which there seem to be a lot of pages on the web discussing. I think I am unsure of the correct terminology and therefore not finding the pages/solutions I need.

Display.png
Thank you in advance.

Jed
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Karrtik IyerSoftware Architect

Commented:
I think it is called DPI settings in windows, and we have a windows C/C++ API to get the current settings, I think that function is called GetDeviceCaps.
I shall try to give you a VBA code for the same.
Software Architect
Commented:
It would be something like below. I have written below for a 64 bit system so PtrSafe prefix before function declaration.
Using the code below.
For 100% it would return 96 as DPI
For 125% it would return 120 as DPI
For 150% it would return 144 as DPI
Option Explicit
Private Const LOGPIXELSX As Long = 88

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

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

Declare PtrSafe 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

Open in new window

Author

Commented:
Perfect - thank you very much.

I was able to ensure that I did a few simple calculations so as more DPIs are added (as Windows 10 already has 175%) I can deal with it...

GetDPIAsDecimal = ((GetDpi * 100) / 96) / 100
GetDPIAsPercent = (GetDpi * 100) / 96
Karrtik IyerSoftware Architect

Commented:
You are welcome.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial