Results 1 to 11 of 11

Thread: The perfect resize algorithm... [VB6]

  1. #1

    Thread Starter
    PowerPoster Dave Sell's Avatar
    Join Date
    Mar 2004
    Location
    /dev/null
    Posts
    2,961

    The perfect resize algorithm... [VB6]

    Well I came up with a way to resize a control on a form and it turned out to be rather elegant so I decided to post it here:

    In the Form:

    VB Code:
    1. Option Explicit
    2. '
    3. Private m_intLastHeight As Integer
    4. Private m_intLastWidth As Integer

    and:

    VB Code:
    1. Private Sub Form_Load()
    2.     '
    3.     m_intLastHeight = Me.Height
    4.     m_intLastWidth = Me.Width
    5.     '
    6. End Sub

    and:

    VB Code:
    1. Private Sub Form_Resize()
    2. On Error GoTo Err
    3.     '
    4.     Me.AnyControl.Height = Me.AnyControl.Height + Me.Height - m_intLastHeight
    5.     Me.AnyControl.Width = Me.AnyControl.Width + Me.Width - m_intLastWidth
    6.     '
    7.     m_intLastHeight = Me.Height
    8.     m_intLastWidth = Me.Width
    9.     '
    10.     Exit Sub
    11. Err:
    12.     If Err.Number = 380 Then
    13.         Exit Sub ' This happens when I minimize
    14.     End If
    15. End Sub

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974
    Well Dave, I like the idea, but there are a couple of issues for you:

    1) Rather than handle the error it is better to not let it happen in the first place (for people like me who log errors, it could get very annoying!)

    To fix that, simply put the resizing code inside an If block like this:
    VB Code:
    1. If Me.WindowState <> vbMinimized Then
    2.   'Resizing code here
    3. End If
    This will also allow you to perform any other funtions in the resize event, without them failing if the form is minimised.

    2) Not all controls can be resized, and not all can be set outside of certain limits (I think textbox/combobox have minimum heights). You need to cater for these too

  3. #3

    Thread Starter
    PowerPoster Dave Sell's Avatar
    Join Date
    Mar 2004
    Location
    /dev/null
    Posts
    2,961
    1) Rather than handle the error it is better to not let it happen in the first place
    Excellent, I added that.

    2) Not all controls can be resized, and not all can be set outside of certain limits (I think textbox/combobox have minimum heights). You need to cater for these too
    Agreed. Not all controls are meant to take part in a Form-related resize.

    This was mainly for a single RichTextBox Edit control, sort of like a notepad. Obviously you wouldn't want text boxes to grow the way the Form is growing!

  4. #4
    Member EricDalnas's Avatar
    Join Date
    Sep 2004
    Location
    Rhode Island
    Posts
    51
    I've used similar code which is effective for many apps. I've found things get a little strange sometimes when adding toolbars and menus though.
    Maybe if you'd stop breathing and die for a change I wouldn't be so pissed off all the time.

    www.mredkj.com

  5. #5

    Thread Starter
    PowerPoster Dave Sell's Avatar
    Join Date
    Mar 2004
    Location
    /dev/null
    Posts
    2,961
    You're right of course.

    A StatusBar can throw a wrench in the works as well, but I dealt with it in my Project by using a Constant to offset by the Height of the StatusBar.

  6. #6
    New Member
    Join Date
    Mar 2007
    Posts
    1

    Re: The perfect resize algorithm... [VB6]

    Thank you, this was a great way to do it.
    I added a couple of Subs to be able to list the controls I wanted to handle.
    One is for moving controls, the other for resizing.
    Small contols close to the bottom can stay there (ControleMove)
    Large controls (listBox) can resize (ContoleSize)
    I'm only interested in Height, you can do the same for width.

    Code:
    ' In Form_Resize: 
    
        If Me.WindowState <> vbMinimized Then
            ' Resize these controls
            ControleSize LB_Product(0)
            ControleSize LB_Product(1)
            
            ' Move these controls
            ControleMove Label3
            ControleMove cmd_OK(4)
            ControleMove CB_SuperNode(1)
            
            'Save size
            MintLastHeight = Me.height
            
            ' Reset width, I dont want to allow resizing the width
            If Me.WindowState = vbNormal Then
                Me.width = MintLastWidth
            End If
        End If
    Code:
    Sub ControleSize(ctr As Control)
        ctr.height = ctr.height + Me.height - MintLastHeight
        'ctr.width = ctr.width + Me.width - MintLastWidth
    End Sub
    Sub ControleMove(ctr As Control)
        ctr.Top = ctr.Top + Me.height - MintLastHeight
        ' ctr.width = ctr.width + Me.width - MintLastWidth
    End Sub

  7. #7
    Hyperactive Member ProphetBeal's Avatar
    Join Date
    Aug 2006
    Location
    New York
    Posts
    424

    Re: The perfect resize algorithm... [VB6]

    I generally don't like my users to be able to resize the form, so what i wrote is a class that resizes the form and controls on the form based on scale.

    This is the form level controls. I allow the users to resized by using Ctrl + Up or Down arrow keys. If there is some sort of problem with the resize they can use Ctrl + R to restore to its original position.
    VB Code:
    1. 'Form level code
    2. Private mobjResizeForm As clsResizeForm
    3.  
    4. 'On form load
    5. Private Sub Form_Load()
    6. Set mobjResizeForm = New clsResizeForm
    7. End Sub
    8. 'Form KeyDown
    9. Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
    10.     'Debug.Print "Form_KeyDown"; Shift; KeyCode
    11.     If Shift = ShiftConstants.vbCtrlMask Then
    12.         Select Case KeyCode
    13.             Case KeyCodeConstants.vbKeyUp
    14.                 'ChangeFontSize 2
    15.                 mobjResizeForm.ResizeFormBasedOnScale 1.3, 1.3, Me
    16.             Case KeyCodeConstants.vbKeyDown
    17.                 'ChangeFontSize -2
    18.                 mobjResizeForm.ResizeFormBasedOnScale (1 / 1.3), (1 / 1.3), Me
    19.             Case KeyCodeConstants.vbKeyR
    20.                 mobjResizeForm.RestoreFormToOrig Me ', mcolControlPos
    21.         End Select
    22.     End If
    23. End Sub
    This is the code from my class. It handles the resizing of all the controls.
    VB Code:
    1. 'Class To handle resizing of all forms
    2. Option Explicit
    3. '>>>*******************************************************************
    4. '           Private Variables
    5. '>>>*******************************************************************
    6. Private mobjRSControls As ADODB.Recordset
    7. 'Private mbolSaveState As Boolean
    8.  
    9. '>>>*******************************************************************
    10. '           Private Properties
    11. '>>>*******************************************************************
    12. Private Property Get mbolSaveState() As Boolean
    13.     mbolSaveState = (mobjRSControls Is Nothing)
    14. End Property
    15.  
    16. '>>>*******************************************************************
    17. '           Public Subs
    18. '>>>*******************************************************************
    19. Public Sub RestoreFormToOrig(ByRef objForm As Form)
    20.     Dim objControl As Control
    21.     If mobjRSControls Is Nothing Then
    22.         GoTo PROC_Exit
    23.     End If
    24.    
    25.     With mobjRSControls
    26.         .Filter = adFilterNone
    27.         .Sort = ""
    28.        
    29.         .Filter = "ControlName = '" & objForm.Name & "'"
    30.         If Not .BOF Then
    31.             .MoveFirst
    32.         End If
    33.         If Not .EOF Then
    34.             objForm.Height = .Fields("Height").Value
    35.             objForm.Width = .Fields("Width").Value
    36.             objForm.Font.SIZE = .Fields("Font").Value
    37.         End If
    38.            
    39.         On Error Resume Next
    40.         For Each objControl In objForm
    41.             .Filter = adFilterNone
    42.             .Sort = ""
    43.            
    44.             .Filter = "ControlName = '" & objControl.Name & "'"
    45.             If Not .BOF Then
    46.                 .MoveFirst
    47.             End If
    48.             If Not .EOF Then
    49.                 objControl.Font.SIZE = .Fields("Font").Value
    50.                 objControl.Height = .Fields("Height").Value
    51.                 objControl.Width = .Fields("Width").Value
    52.                 objControl.Top = .Fields("Top").Value
    53.                 objControl.Left = .Fields("Left").Value
    54.             End If
    55.         Next
    56.     End With
    57. PROC_Exit:
    58.     Set objControl = Nothing
    59. End Sub
    60.  
    61. Public Sub SaveOrigFormSize(ByRef objForm As Form)
    62. On Error Resume Next
    63.     Dim objControl As Control
    64.    
    65.     Set mobjRSControls = New ADODB.Recordset
    66.     With mobjRSControls
    67.         Set .ActiveConnection = Nothing
    68.         .CursorLocation = adUseClient
    69.         .LockType = adLockBatchOptimistic
    70.         With .Fields 'Create the fields that will be apart of this recordset.
    71.             .Append "ControlName", adLongVarChar, 100
    72.             .Append "Height", adDouble
    73.             .Append "Width", adDouble
    74.             .Append "Left", adDouble
    75.             .Append "Top", adDouble
    76.             .Append "Font", adDouble
    77.         End With
    78.         .Open
    79.        
    80.         'Start populating the recordset
    81.         .AddNew
    82.         .Fields("ControlName") = objForm.Name
    83.         .Fields("Height") = objForm.Height
    84.         .Fields("Width") = objForm.Width
    85.         .Fields("Font") = objForm.Font.SIZE
    86.         .UpdateBatch
    87.         For Each objControl In objForm.Controls
    88.             .AddNew
    89.             .Fields("ControlName") = objControl.Name
    90.             .Fields("Height") = objControl.Height
    91.             .Fields("Width") = objControl.Width
    92.             .Fields("Left") = objControl.Left
    93.             .Fields("Top") = objControl.Top
    94.             .Fields("Font") = objControl.Font.SIZE
    95.             .UpdateBatch
    96.         Next
    97.     End With
    98.     Set objControl = Nothing
    99. End Sub
    100.  
    101.  
    102. Public Sub ResizeFormBasedOnScale(ByVal sngSFX As Single, ByVal sngSFY As Single, ByRef objCurForm As Form)
    103. On Error GoTo PROC_Err
    104.     Dim objControl As Control
    105.     Dim sngSFFont As Single
    106.     If mbolSaveState Then
    107.         SaveOrigFormSize objCurForm ', mcolControlPos
    108.     End If
    109.     'mbolSaveState = False
    110.     sngSFFont = (sngSFX + sngSFY) / 2  ' average scale
    111.     ' Size the Controls for the new resolution
    112.     On Error Resume Next  ' for read-only or nonexistent properties
    113.     With objCurForm
    114.         For Each objControl In .Controls 'i = 0 To .Count - 1
    115.             objControl.Left = objControl.Left * sngSFX
    116.             objControl.Top = objControl.Top * sngSFY
    117.             objControl.Width = objControl.Width * sngSFX
    118.            
    119.             If TypeOf objControl Is ComboBox Then
    120.                 ' cannot change Height
    121.             Else
    122.                 objControl.Height = objControl.Height * sngSFY
    123.             End If
    124.             'I think this code should be inside the above loop and I am moving the same.
    125.             ' Be sure to resize and reposition before changing the FontSize
    126.             objControl.Font.SIZE = objControl.Font.SIZE * sngSFFont
    127.         Next
    128.         'If mbolRePosForm Then
    129.             ' Now size the Form
    130.             '.Move .Left * sngSFX, .Top * sngSFY, .Width * sngSFX, .Height * sngSFY
    131.             .Move .Left, .Top, .Width * sngSFX, .Height * sngSFY
    132.         'End If
    133.     End With
    134. PROC_Exit:
    135.     Set objControl = Nothing
    136.     Exit Sub
    137. PROC_Err:
    138.     If Err.Number = 438 Then
    139.         Resume Next
    140.     Else
    141.         Resume PROC_Exit
    142.     End If
    143. End Sub
    144.  
    145. '>>>*******************************************************************
    146. '           Private Subs
    147. '>>>*******************************************************************
    148. Private Sub Class_Terminate()
    149.     If Not mobjRSControls Is Nothing Then
    150.         If mobjRSControls.State <> adStateClosed Then
    151.             mobjRSControls.Close
    152.         End If
    153.     End If
    154.     Set mobjRSControls = Nothing
    155. End Sub

    Helpful Links:
    VB 6 - How to get the "Key" Value in a collection
    VB.NET - File Search Utility || VB.NET - How to compare 2 directories || VB.NET - How to trust a network share
    VB.NET - Create Excel Spreadsheet From Array || VB.NET - Example Code & Hints you may not know
    VB.NET - Save JPEG with a certain quality (image compression) || VB.NET - DragDrop Files, Emails, and Email Attachments

    Please post some of the code you need help with (it makes it easier to help you)
    If your problem has been solved then please mark the thread [RESOLVED].
    Don't forget to Rate this post

    "Pinky, you give a whole new meaning to the phrase, 'counter-intelligence'."-The Brain-

  8. #8
    New Member
    Join Date
    Dec 2009
    Location
    UK
    Posts
    1

    Re: The perfect resize algorithm... [VB6]

    Hi, I realise this thread is 2.5 years old, but I found this topic via google and it came in very handy for me.

    Dave, I used your code, it works very well.

    However the control doesn't seem to scale perfectly when the form is resized. This is more noticable the larger you make the form with respect to its original size (ie maximised).

    I altered the code by changing this

    VB Code:
    1. Me.AnyControl.Height = Me.AnyControl.Height + Me.Height - m_intLastHeight
    2. Me.AnyControl.Width = Me.AnyControl.Width + Me.Width - m_intLastWidth

    To this

    VB Code:
    1. Me.AnyControl.height = Me.AnyControl.height * (Me.height / m_intLastHeight)
    2. Me.AnyControl.width = Me.AnyControl.width * (Me.width / m_intLastWidth)

    And now it seems to work better.

    I hope this comes in handy for someone else!


    Turkish

  9. #9
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: The perfect resize algorithm... [VB6]

    Welcome to VBForums

    The problem is caused by a common mistake... using Me.Height and Me.Width (which are the size of the outside of the form, including the title bar etc), rather than Me.ScaleHeight and Me.ScaleWidth (which are the size of the area that controls can be placed in).

  10. #10
    Lively Member
    Join Date
    Dec 2005
    Location
    UK
    Posts
    127

    Re: The perfect resize algorithm... [VB6]

    Quote Originally Posted by si_the_geek View Post
    Welcome to VBForums

    The problem is caused by a common mistake... using Me.Height and Me.Width (which are the size of the outside of the form, including the title bar etc), rather than Me.ScaleHeight and Me.ScaleWidth (which are the size of the area that controls can be placed in).
    great tip ill be using this later

  11. #11
    Lively Member C0der's Avatar
    Join Date
    Mar 2010
    Location
    Somewhere in Internet
    Posts
    113

    Re: The perfect resize algorithm... [VB6]

    Hello guys,

    I don't remember where i found a similar code like this,
    but helped me solve what i wanted to do, Thank You.

    C0der

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