Please Check This DPI Changer for Windows

Just starting out? Need help? Post your questions and find answers here.
CalamityJames
User
User
Posts: 78
Joined: Sat Mar 13, 2010 4:50 pm

Please Check This DPI Changer for Windows

Post by CalamityJames »

I was adapting a program to be DPI aware, and I had to keep toggling my desktop scaling manually from its recommended 100% to 150% (to make any positioning errors clear). This got a bit tedious but I found the information on the internet to change the scaling and adapted it to Pure Basic. The source of the information is noted in the program listing.

As my normal scaling is 100% there are some things that I cannot check, so I would ask anyone (especially if their normal desktop scaling is not 100%) to check that the program does what it should. The program checks whether the current setting is at the recommended level and if it is, it changes it it be two steps greater (eg if it is 100% it goes to 150%). If it is not at the recommended level then it sets the recommended level, whatever that should be.

It works on Windows 11 and I therefore presume it works on Windows 10. It does not work on Windows 7. I do not know whether it works on Windows 8.

A word of caution: not all program windows take kindly to having the DPI changed in this way, so I recommend closing most programs before running this one. (PureBasic is OK!)

Code: Select all

;https://stackoverflow.com/questions/35233182/how-can-i-change-windows-10-display-scaling-programmatically-using-c-sharp (search on the page for "This is a very simple API." to see the source I used).
EnableExplicit
Global Dim ScaleArray.i(11)
#SPI_GETLOGICALDPIOVERRIDE = $0009E
#SPI_SETLOGICALDPIOVERRIDE = $0009F

Procedure.d GetCurrentScale()
  Protected lpx.d, lpy, DC = GetDC_(#Null)
  If DC
    lpx = GetDeviceCaps_(DC, #LOGPIXELSX)
    If lpx : lpx = lpx / 96.0 : EndIf
    ReleaseDC_(#Null, DC)
  EndIf
  ProcedureReturn lpx
EndProcedure

Procedure GetScaleData()
  Protected Inc.i
  For Inc = 0 To 11
    Read ScaleArray(Inc)
  Next
EndProcedure

Procedure ToggleScaleFactor()
  Protected RecommendedScaleIndex.i, ReturnValue.i, RelativeIndexToUse.i, RecommendedScale.d
  RecommendedScaleIndex = SystemParametersInfo_(#SPI_GETLOGICALDPIOVERRIDE, 0, 0, 1)
  RecommendedScale = ScaleArray(-RecommendedScaleIndex)/100
  If GetCurrentScale() <> RecommendedScale
    RelativeIndexToUse = -RecommendedScaleIndex
  Else
    RelativeIndexToUse = -RecommendedScaleIndex + 2
  EndIf
  ReturnValue = SystemParametersInfo_(#SPI_SETLOGICALDPIOVERRIDE, RelativeIndexToUse, 0, 1)
EndProcedure

GetScaleData()
ToggleScaleFactor()

DataSection
  Data.i 100, 125, 150, 175, 200, 225, 250, 300, 350, 400, 450, 500
EndDataSection