VB.Net adjusting forms and controls to display resolution

Claude Larocque 666 Reputation points
2021-10-23T11:39:17.653+00:00

There are many explanations on how to develop an application that will use the display resolution to adjust automatically its forms and controls.

But it becomes confusing and I tried so many things that didn't work so my question is:

Someone knows an example (solution) that works perfectly with the display resolution an .sln file or .csproj
in VB.Net?

If I would have an example of one form, then I can apply the configuration to all my forms.

Claude from Quebec, Canada
Thanks in advance
******@autocaisse.onmicrosoft.com

Developer technologies VB
{count} votes

Accepted answer
  1. Karen Payne MVP 35,586 Reputation points Volunteer Moderator
    2021-10-24T19:22:07.523+00:00

    My two cents is that (and I realize this is not what you are working with) WPF handles resizing a good deal better than Windows forms, kind of like responsive design for a web app say using Bootstrap with containers.

    What I've seen work pretty good is a third party library from a company known as Progress and in the following blog there is a good discussion steps to follow for auto resizing with and without their product.

    WinForms Scaling at Large DPI Settings–Is It Even Possible?

    Unfortunately most developer are squeamish about purchasing products to make their life easier and that is okay yet they miss out on simple solutions.

    0 comments No comments

7 additional answers

Sort by: Most helpful
  1. Claude Larocque 666 Reputation points
    2021-10-24T14:52:17.213+00:00

    Well like I mentioned in my first post, I read and try all scenarios but none is working. Your links that you provided brings me in a labyrinth of links and suggestions. I think to understand how to resize your windows form base on the screen resolution, meaning resizing not only the form but the any controls on that form you must work at Microsoft in the development department.

    So many links... so many refer to... or go to... that after an hour of clicking on these links and try to understand what to do in VB.Net to do the automatic resizing, I am loosing the battle.

    So
    If someone feels strong enough in programming to create only Form1 with the configuration and the code that will make that form and controls resizing itself automatically, something like a .csproj file or .sln so by changing the resolution on the monitor that form and controls would resize, then I will accept the answer.

    Claude from Quebec, Canada

    1 person found this answer helpful.

  2. Castorix31 90,521 Reputation points
    2021-10-23T16:00:47.063+00:00
    0 comments No comments

  3. Claude Larocque 666 Reputation points
    2021-10-29T09:25:28.327+00:00

    I will do like Karen suggested, buy a proven software and no more problems for that part, thanks so lot for the information

    Claude


  4. Leon Stanley 96 Reputation points
    2022-02-06T02:19:05.43+00:00

    HI Claude
    Have you considered the users choice in windows/settings/system/display. Whether the user choices 100%, 125% or whatever. The point is you need to use methods in the size and layout of all your forms, dialogs, and controls, and fonts on your user interfaces. WPF make this all easier, but if your are like myself a stickler for windows.forms, you need to take care of all those details yourself in code - usually the load event, the resize event, or maximise events.
    Use {My.Computer.Screen.WorkingArea.Width} (and .Height) to see how much room you've got to play with.
    If the user changes their windows display setting, this will be reflected in the pixel width/height that is returned.
    Here is a simplified example of variable form sizing resulting in fonts and controls being resized correctly.

    Public Class Form5 ' Example regarding resizing forms and control bounds
        Dim isMax, isEvntSus As Boolean
        Dim fW, fH As Integer
        Dim strFntName As String
        Dim fnt As Font
        Dim reScale As Double
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Dim I, J, K As Integer
            I = My.Settings.FrmSz.IndexOf(",")
            fW = CInt(My.Settings.FrmSz.Substring(0, I))
            fH = CInt(My.Settings.FrmSz.Substring(I + 1, My.Settings.FrmSz.Length - I - 1))
            isEvntSus = True
            ClientSize = New Size(fW, fH)
            isEvntSus = False
            I = Screen.PrimaryScreen.WorkingArea.Width - Width + fW
            J = Screen.PrimaryScreen.WorkingArea.Height - Height + fH
            If (fW / fH) > (I / J) Then ' screen width will detirmine
                K = CInt(I * 0.75) + Height - fH
                MaximizedBounds = New Rectangle(0, CInt((Screen.PrimaryScreen.WorkingArea.Height - K) / 2), _
                                                Screen.PrimaryScreen.WorkingArea.Width, K)
            Else ' screen height will detirmine
                K = CInt(J / 0.75) + Width - fW
                MaximizedBounds = New Rectangle(CInt((Screen.PrimaryScreen.WorkingArea.Width - K) / 2), _
                                                0, K, Screen.PrimaryScreen.WorkingArea.Height)
            End If
            MinimumSize = New Size(400 + Width - fW, 300 + Height - fH)
            MaximumSize = MaximizedBounds.Size
            strFntName = "microsoft sans serif"
            For Each FF As FontFamily In FontFamily.Families
                If FF.Name.ToLower = "segoe ui" Then strFntName = "segoe ui"
            Next
            Label1.Text = "Label1 Text"
            Button1.Text = "Button1 Text"
            TextBox1.Multiline = True : TextBox1.ScrollBars = ScrollBars.Vertical
            TextBox1.Text = "With the people of that age the value of all things was determined by outward show." & _
                " As religion had declined in power, it had increased in pomp. The educators" & _
                " of the time sought to command respect by display and ostentation. To all this" & _
                " the life of Jesus presented a marked contrast. - Ed pg 77 - Ellen G White"
            refreshBounds()
    
            CenterToScreen()
        End Sub
    
        Private Sub Form5_Paint(sender As Object, e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
            e.Graphics.TextRenderingHint = Drawing.Text.TextRenderingHint.ClearTypeGridFit
            e.Graphics.DrawString("Some GDI drawn text . . .", fnt, Brushes.DarkBlue, CInt(reScale * 30), CInt(reScale * 20))
        End Sub
    
        Private Sub Form5_ResizeEnd(sender As Object, e As System.EventArgs) Handles Me.ResizeEnd
            If isEvntSus Then Exit Sub
            Dim K, L As Integer
            K = ClientSize.Width : L = ClientSize.Height
            If K = fW And L = fH Then
                Exit Sub
            ElseIf K = fW And Not L = fH Then
                ClientSize = New Size(CInt(L / 0.75), L)
            ElseIf Not K = fW And L = fH Then
                ClientSize = New Size(K, CInt(K * 0.75))
            ElseIf Not K = fW And Not L = fH Then
                If K / L < 1.333333 Then
                    ClientSize = New Size(K, CInt(K * 0.75))
                Else
                    ClientSize = New Size(CInt(L / 0.75), L)
                End If
            End If
            My.Settings.FrmSz = ClientSize.Width.ToString & "," & ClientSize.Height.ToString
            refreshBounds()
            Invalidate()
        End Sub
    
        Private Sub Form5_SizeChanged(sender As Object, e As System.EventArgs) Handles Me.SizeChanged
            If WindowState = FormWindowState.Minimized Then Exit Sub
            If isMax Then
                isMax = False
                refreshBounds()
                Invalidate()
            End If
            If WindowState = FormWindowState.Maximized Then
                isMax = True
                refreshBounds()
                Invalidate()
            End If
        End Sub
    
        Sub refreshBounds()
            fW = ClientSize.Width : fH = ClientSize.Height
            reScale = fW / 400
            fnt = New Font(strFntName, CInt(reScale * 18), FontStyle.Regular, GraphicsUnit.Pixel)
            Label1.Location = New Point(CInt(reScale * 30), CInt(reScale * 60))
            Button1.SetBounds(CInt(reScale * 200), CInt(reScale * 60), CInt(reScale * 170), CInt(reScale * 40))
            TextBox1.SetBounds(CInt(reScale * 30), CInt(reScale * 120), CInt(reScale * 340), CInt(reScale * 150))
            Label1.Font = fnt : Button1.Font = fnt : TextBox1.Font = fnt
            Text = "Clientsize = { " & fW.ToString & " : " & fH.ToString & " }"
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As System.EventArgs) Handles Button1.Click
            MsgBox("Ouiy")
        End Sub
    End Class
    

    All the pixel related methods will still work correctly except for the screen capture method (I forget what that is at this moment). It always captures the co-ordinates of the machine pixels (as if the user has 100% chosen in display settings.
    There is a way to query your machines screen pixel width and height. If you are interested in that -

    Imports System.Runtime.InteropServices ' Mr.Monkeyboy provided this code in principal
    Public Class Form12
    
        <DllImport("gdi32.dll")> _
        Private Shared Function GetDeviceCaps(hdc As IntPtr, nIndex As Integer) As Integer
        End Function
    
        Private Sub Form12_Load(sender As Object, e As System.EventArgs) Handles Me.Load
            Dim g As Graphics = Graphics.FromHwnd(IntPtr.Zero)
            Dim desktop As IntPtr = g.GetHdc()
    
            'MessageBox.Show(GetDeviceCaps(desktop, 4).ToString & "  Physical width mm" & vbCrLf & _
            '                GetDeviceCaps(desktop, 6).ToString & "  Physical height mm" & vbCrLf & _
            '                GetDeviceCaps(desktop, 8).ToString & "  Virtual pixel width" & vbCrLf & _
            '                GetDeviceCaps(desktop, 10).ToString & "  Virtual pixel height" & vbCrLf & _
            '                GetDeviceCaps(desktop, 118).ToString & "  Factory pixel width" & vbCrLf & _
            '                GetDeviceCaps(desktop, 117).ToString & "  Factory pixel height")
    
            MessageBox.Show(GetDeviceCaps(desktop, 116).ToString & "  Video Hardware refresh rate Hertz. (FPS).")
    
        End Sub
    
        Public Enum DeviceCap
            ''' <summary>
            ''' Device driver version
            ''' </summary>
            DRIVERVERSION = 0
            ''' <summary>
            ''' Device classification
            ''' </summary>
            TECHNOLOGY = 2
            ''' <summary>
            ''' Horizontal size in millimeters
            ''' </summary>
            HORZSIZE = 4
            ''' <summary>
            ''' Vertical size in millimeters
            ''' </summary>
            VERTSIZE = 6
            ''' <summary>
            ''' Horizontal width in pixels
            ''' </summary>
            HORZRES = 8
            ''' <summary>
            ''' Vertical height in pixels
            ''' </summary>
            VERTRES = 10
            ''' <summary>
            ''' Number of bits per pixel
            ''' </summary>
            BITSPIXEL = 12
            ''' <summary>
            ''' Number of planes
            ''' </summary>
            PLANES = 14
            ''' <summary>
            ''' Number of brushes the device has
            ''' </summary>
            NUMBRUSHES = 16
            ''' <summary>
            ''' Number of pens the device has
            ''' </summary>
            NUMPENS = 18
            ''' <summary>
            ''' Number of markers the device has
            ''' </summary>
            NUMMARKERS = 20
            ''' <summary>
            ''' Number of fonts the device has
            ''' </summary>
            NUMFONTS = 22
            ''' <summary>
            ''' Number of colors the device supports
            ''' </summary>
            NUMCOLORS = 24
            ''' <summary>
            ''' Size required for device descriptor
            ''' </summary>
            PDEVICESIZE = 26
            ''' <summary>
            ''' Curve capabilities
            ''' </summary>
            CURVECAPS = 28
            ''' <summary>
            ''' Line capabilities
            ''' </summary>
            LINECAPS = 30
            ''' <summary>
            ''' Polygonal capabilities
            ''' </summary>
            POLYGONALCAPS = 32
            ''' <summary>
            ''' Text capabilities
            ''' </summary>
            TEXTCAPS = 34
            ''' <summary>
            ''' Clipping capabilities
            ''' </summary>
            CLIPCAPS = 36
            ''' <summary>
            ''' Bitblt capabilities
            ''' </summary>
            RASTERCAPS = 38
            ''' <summary>
            ''' Length of the X leg
            ''' </summary>
            ASPECTX = 40
            ''' <summary>
            ''' Length of the Y leg
            ''' </summary>
            ASPECTY = 42
            ''' <summary>
            ''' Length of the hypotenuse
            ''' </summary>
            ASPECTXY = 44
            ''' <summary>
            ''' Shading and Blending caps
            ''' </summary>
            SHADEBLENDCAPS = 45
    
            ''' <summary>
            ''' Logical pixels inch in X
            ''' </summary>
            LOGPIXELSX = 88
            ''' <summary>
            ''' Logical pixels inch in Y
            ''' </summary>
            LOGPIXELSY = 90
    
            ''' <summary>
            ''' Number of entries in physical palette
            ''' </summary>
            SIZEPALETTE = 104
            ''' <summary>
            ''' Number of reserved entries in palette
            ''' </summary>
            NUMRESERVED = 106
            ''' <summary>
            ''' Actual color resolution
            ''' </summary>
            COLORRES = 108
    
            ' Printing related DeviceCaps. These replace the appropriate Escapes
            ''' <summary>
            ''' Physical Width in device units
            ''' </summary>
            PHYSICALWIDTH = 110
            ''' <summary>
            ''' Physical Height in device units
            ''' </summary>
            PHYSICALHEIGHT = 111
            ''' <summary>
            ''' Physical Printable Area x margin
            ''' </summary>
            PHYSICALOFFSETX = 112
            ''' <summary>
            ''' Physical Printable Area y margin
            ''' </summary>
            PHYSICALOFFSETY = 113
            ''' <summary>
            ''' Scaling factor x
            ''' </summary>
            SCALINGFACTORX = 114
            ''' <summary>
            ''' Scaling factor y
            ''' </summary>
            SCALINGFACTORY = 115
    
            ''' <summary>
            ''' Current vertical refresh rate of the display device (for displays only) in Hz
            ''' </summary>
            VREFRESH = 116
            ''' <summary>
            ''' Vertical height of entire desktop in pixels
            ''' </summary>
            DESKTOPVERTRES = 117
            ''' <summary>
            ''' Horizontal width of entire desktop in pixels
            ''' </summary>
            DESKTOPHORZRES = 118
            ''' <summary>
            ''' Preferred blt alignment
            ''' </summary>
            BLTALIGNMENT = 119
        End Enum
    
    End Class
    

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.