Jump to content

Function to determine Scaling Factor of primary monitor


Recommended Posts

Previously, I used a method of GetDeviceCaps that attempted to read out the DPI of the desktop (96 or otherwise) ... but that kept returning 96 for some monitors when scaling was set to 125% or 150%. (There are multiple references to the DLL call for $Logpixelsy in posts on this forum. But apparently, $Logpixelsy doesn't exists on all systems ... so the call returns 0 ... which is interpreted as 96.)

After much research, I've settled on a method that works reliably on several different PCs, with monitors up to 1920 x 1080 pixels:

MsgBox(0, "Scaling", _GetDPI_Ratio())

Func _GetDPI_Ratio()
    Local $hWnd = 0
    Local $hDC = DllCall("user32.dll", "long", "GetDC", "long", $hWnd)
    Local $aRet = DllCall("gdi32.dll", "long", "GetDeviceCaps", "long", $hDC[0], "long", 10)  ; = reported vert height (not logical, which is param=90)
    Local $bRet = DllCall("gdi32.dll", "long", "GetDeviceCaps", "long", $hDC[0], "long", 117) ; = true vert pixels of desktop
    $hDC = DllCall("user32.dll", "long", "ReleaseDC", "long", $hWnd, "long", $hDC)
    Return $bRet[0] / $aRet[0]
EndFunc


For reference, here's a link to a method that uses PowerShell: link
It confirms the validity of using the ratio of these two parameters.

There's one other aspect to this that I want to mention: In the latest versions of Win10—and on particular PCs—scaling is set to 100% on monitors as large as 1920 x 1080 ... yet the desktop uses medium icons and applications have what I consider to be normal sizes. I haven't been able to determine how certain combinations are able to do this, because some PCs I have access to require 125% scaling on a 1920 monitor for applications to have anything close to normal size. (IOW, at 100%, the desktop appears uncomfortably small.) A table in MSFT's documentation seems to indicate there are certain magic combinations of display sizes versus pixels that can use 100% scaling.

table

Nevertheless, I'm glad to be able to read out the scaling. But certainly feel free to add to the discussion. I only ask that you cite online sources as supporting references.

 

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...