Results 1 to 4 of 4

Thread: [RESOLVED] Automatic Resizing of all Controls Depending on Screen Resolution

  1. #1

    Thread Starter
    Hyperactive Member Hassan Basri's Avatar
    Join Date
    Sep 2006
    Posts
    324

    Resolved [RESOLVED] Automatic Resizing of all Controls Depending on Screen Resolution

    Hello everybody, I have a form with lots of controls. This form can be used on several user machines each with their own screen résolutions. Instead of manually adding code in the Resize event for each individual control I decided instead to go for 2 procedures. A SaveSizes in the Form Load event and a ResizeControls in the Resize event. This works on about half of screen résolutions I tested. Several controls get resized wrong in the other half of screen résolutions (bigger sizes). Please see my code below if anybody has a suggestion on improving it or a better way of doing this. Thank you.

    Declarations
    Code:
    Private Type ControlPositionType
        Left As Single
        Top As Single
        Width As Single
        Height As Single
        FontSize As Single
    End Type
    
    Private m_ControlPositions() As ControlPositionType
    Private m_FormWid As Single
    Private m_FormHgt As Single
    Save Sizes called in the Form Load
    Code:
    Private Sub SaveSizes()
    Dim i As Integer
    Dim ctl As Control
    
        ' Save the controls' positions and sizes.
        ReDim m_ControlPositions(1 To Controls.Count)
        i = 1
        For Each ctl In Controls
            With m_ControlPositions(i)
                If TypeOf ctl Is Line Then
                    .Left = ctl.X1
                    .Top = ctl.Y1
                    .Width = ctl.X2 - ctl.X1
                    .Height = ctl.Y2 - ctl.Y1
                Else
                    .Left = ctl.Left
                    .Top = ctl.Top
                    .Width = ctl.Width
                    .Height = ctl.Height
                    On Error Resume Next
                    .FontSize = ctl.Font.Size
                    On Error GoTo 0
                End If
            End With
            i = i + 1
        Next ctl
    
        ' Save the form's size.
        m_FormWid = ScaleWidth
        m_FormHgt = ScaleHeight
    End Sub
    ResizeControls called in the Resize event
    Code:
    Private Sub ResizeControls()
    Dim i As Integer
    Dim ctl As Control
    Dim x_scale As Single
    Dim y_scale As Single
    
        ' Don't bother if we are minimized.
        If WindowState = vbMinimized Then Exit Sub
    
        ' Get the form's current scale factors.
        x_scale = ScaleWidth / m_FormWid
        y_scale = ScaleHeight / m_FormHgt
    
        ' Position the controls.
        i = 1
        For Each ctl In Controls
            With m_ControlPositions(i)
                If TypeOf ctl Is Line Then
                    ctl.X1 = x_scale * .Left
                    ctl.Y1 = y_scale * .Top
                    ctl.X2 = ctl.X1 + x_scale * .Width
                    ctl.Y2 = ctl.Y1 + y_scale * .Height
                Else
                    If ctl.Name <> "picClose" Then
                    ctl.Left = x_scale * .Left
                    ctl.Top = y_scale * .Top
                    ctl.Width = x_scale * .Width
                    If Not (TypeOf ctl Is ComboBox) Then
                        ' Cannot change height of ComboBoxes.
                        ctl.Height = y_scale * .Height
                    End If
                    On Error Resume Next
                    ctl.Font.Size = y_scale * .FontSize * 0.75
                    End If
                    On Error GoTo 0
                End If
            End With
            i = i + 1
        Next ctl
    End Sub

  2. #2
    PowerPoster dilettante's Avatar
    Join Date
    Feb 2006
    Posts
    24,487

    Re: Automatic Resizing of all Controls Depending on Screen Resolution

    Sizing and positioning is one thing, coping with High DPI is another.

    In any case your approach is doomed to failure. Trying to "magnify" your Forms is the wrong way to go. Font sizing alone will doom you, since fonts are not pixel-scalable.

    You have gone down the wrong path.

  3. #3
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    10,914

    Re: Automatic Resizing of all Controls Depending on Screen Resolution

    Hi Hassan,

    I didn't study your code, but I did read your paragraph. You said "Screen Resolution" but is this truly what you're interested in? There are three related, but very different concepts: 1) screen resolution, 2) dots-per-inch (DPI), and 3) virtual scaling.

    As far as I know, the last of those (virtual scaling) only work well on Windows 10.

    Regarding screen resolutions, I'm not entirely sure why you'd be interested in that. There's also a fourth concept: the physical size of the monitor.

    If screen resolution is less (say 1280 x 960, as compared to 1920 x 1080), what is it you'd like to do? Make your form and controls bigger or smaller? That's very unclear to me. If someone changes their screen resolution, I'd tend to not concern myself with it from my program.

    Now, if you're talking about someone changing DPI, LaVolpe has put together an extensive tutorial on this. And be forewarned. It's not a trivial matter.

    Personally, for me, if my users want to make things larger, I tell them to get Windows 10, and use its virtualization.

    Name:  res.png
Views: 4435
Size:  4.7 KB

    Microsoft has worked hard to make fonts, pictures, and everything scale with a minumum of fuzz/artifact. I'm quite happy with it. And this completely obviates me having to concern myself with it at all.

    Good Luck,
    Elroy
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

  4. #4

    Thread Starter
    Hyperactive Member Hassan Basri's Avatar
    Join Date
    Sep 2006
    Posts
    324

    Re: Automatic Resizing of all Controls Depending on Screen Resolution

    Thanks guys, my form is always maximized. So the controls have to be repositioned and resized for different screen resolutions. After some testing I realized that my resizing / repositioning code works correctly. It was my FitTextToLabel function that Elroy was so kind to provide that wasn't working correctly when the screen width was much longer then height. I modified the code to include a check on TextHeight to fix the issue. I will update the thread with the updated function. Thanks again for your comments.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width