How to check and modify display scale from VB.NET

SM 40 Reputation points
2023-04-07T10:09:08.7733333+00:00

Hello, I would like to know if it were possible to check and modify the scale of a display via VB.NET code. I succeed in checking the resolution using the command: My.Computer.Screen.Bounds Is there something similar even for the scale factor? Thank you

VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,593 questions
{count} votes

Accepted answer
  1. Castorix31 81,861 Reputation points
    2023-04-07T11:51:48.8266667+00:00

    To get it (one of the ways, there are others..) :

                Dim hMon As IntPtr = MonitorFromWindow(Me.Handle, MONITOR_DEFAULTTOPRIMARY)
                Dim dsf As DEVICE_SCALE_FACTOR
                GetScaleFactorForMonitor(hMon, dsf)
                MessageBox.Show(String.Format("Scale Factor : {0}", dsf.ToString()), "Information", MessageBoxButtons.OK, MessageBoxIcon.Information)
    
    

    To change it : (nIndex depends on the default scale; for example on my PC, 0 = 100%, 1 = 125%, ...)

                Dim nIndex As Integer = 1
                SystemParametersInfo(SPI_SETLOGICALDPIOVERRIDE, nIndex, 0, SPIF_UPDATEINIFILE Or SPIF_SENDWININICHANGE)
    
    

    With declarations :

        <DllImport("Shcore.dll", SetLastError:=True, CharSet:=CharSet.Unicode)>
        Public Shared Function GetScaleFactorForMonitor(hMon As IntPtr, ByRef pScale As DEVICE_SCALE_FACTOR) As HRESULT
        End Function
    
        Public Enum DEVICE_SCALE_FACTOR
            DEVICE_SCALE_FACTOR_INVALID = 0
            SCALE_100_PERCENT = 100
            SCALE_120_PERCENT = 120
            SCALE_125_PERCENT = 125
            SCALE_140_PERCENT = 140
            SCALE_150_PERCENT = 150
            SCALE_160_PERCENT = 160
            SCALE_175_PERCENT = 175
            SCALE_180_PERCENT = 180
            SCALE_200_PERCENT = 200
            SCALE_225_PERCENT = 225
            SCALE_250_PERCENT = 250
            SCALE_300_PERCENT = 300
            SCALE_350_PERCENT = 350
            SCALE_400_PERCENT = 400
            SCALE_450_PERCENT = 450
            SCALE_500_PERCENT = 500
        End Enum
    
        <DllImport("User32.dll", SetLastError:=True, CharSet:=CharSet.Unicode)>
        Public Shared Function MonitorFromWindow(ByVal hwnd As IntPtr, ByVal dwFlags As Integer) As IntPtr
        End Function
    
        <DllImport("User32.dll", SetLastError:=True, CharSet:=CharSet.Unicode)>
        Public Shared Function SystemParametersInfo(uiAction As UInteger, uiParam As Integer, ByRef pvParam As Integer, fWinIni As UInteger) As Boolean
        End Function
    
        Public Const SPIF_UPDATEINIFILE As Integer = &H1
        Public Const SPIF_SENDWININICHANGE As Integer = &H2
    
        Public Const MONITOR_DEFAULTTONULL As Integer = &H0
        Public Const MONITOR_DEFAULTTOPRIMARY As Integer = &H1
        Public Const MONITOR_DEFAULTTONEAREST As Integer = &H2
    
        Public Const SPI_GETLOGICALDPIOVERRIDE = &H9E
        Public Const SPI_SETLOGICALDPIOVERRIDE = &H9F
    
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. SM 40 Reputation points
    2023-04-12T12:16:15.89+00:00

    Hello, First of all, thank you for your answer. I made a test with your code and I succeed in changing the monitor scaling factor. Unfortunately, I did not succeed in viewing the current scaling factor: the message reports 100% when it was set to 100% (so in this case was correct), but when I tried to set other scaling factor (125, 150 and 175%) both from script and manually, the result was always "SCALE__140__PERCENT" I don't understand how the function "GetScaleFactorForMonitor" works, since there are no instruction before the End function