[RESOLVED] manual window resize with limits..-VBForums
Results 1 to 5 of 5

Thread: [RESOLVED] manual window resize with limits..

  1. #1

    Thread Starter
    Member
    Join Date
    Jan 2016
    Posts
    58

    Resolved [RESOLVED] manual window resize with limits..

    I'm not sure how to ask this, so I'm sorry for the inevitable confusion.


    I'm looking for a way (and wondering if it's even possible) to set a form window such that when it's manually resized (maximize option would be disabled), it keeps the same aspect ratio as the image being shown. The method being used to show the image has not been decided, it could be a picture box, form background or something else. It's the end result of the form window I'm after.

    TIA

  2. #2
    Addicted Member
    Join Date
    Nov 2017
    Posts
    193

    Re: manual window resize with limits..

    The code below shrinks the dimension (width or height) that exceeds the ratio. It would be possible to do the opposite, to enlarge the dimension that doesn't meet the ratio.

    You just adjust msngAspecRatio to the desired value.

    Code:
      Private msngAspectRatio As Single = 16 / 9
    
    
      Private Sub frmAspectRatioResizeTest_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
        PreserveFormAspectRatio()
    
      End Sub
    
      Private Sub frmAspectRatioResizeTest_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Resize
    
        PreserveFormAspectRatio()
    
      End Sub
    
      Private Sub PreserveFormAspectRatio()
    
        If (Me.Width / Me.Height) > msngAspectRatio Then
          'Too wide
          Me.Width = CInt(Me.Height * msngAspectRatio)
        ElseIf (Me.Width / Me.Height) < msngAspectRatio Then
          'Too tall
          Me.Height = CInt(Me.Width / msngAspectRatio)
        End If
    
      End Sub

  3. #3

    Thread Starter
    Member
    Join Date
    Jan 2016
    Posts
    58

    Re: manual window resize with limits..

    Nice,

    Thank you.

  4. #4
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    39,581

    Re: [RESOLVED] manual window resize with limits..

    In order to determine the aspect ratio of the picture, you can divide the width by the height.

    To keep the form to the same aspect ratio, whenever it is resized alter the width or height (preferably whichever has changed least, as the other one is probably what the user cares about) using the aspect ratio value you got from the picture. If the width stayed the same, set width to height times the aspect ratio. If the height stayed the same, set the height to the width divided by the aspect ratio.

    If your form has a border, you will also need to do the calculations in a way that ignores it (so read the height/width from ClientSize, then when finished add on the difference between Size and ClientSize).


    edit: oops, it seems i typed a bit slow!

  5. #5
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    21,560

    Re: [RESOLVED] manual window resize with limits..

    Try this...

    Code:
    Public Class Form1
    
        Private lastHeight As Integer
        Private lastWidth As Integer
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Me.Size = New Size(400, 300)
            Me.MaximumSize = New Size(600, 450)
            lastHeight = 300
            lastWidth = 400
            Me.DoubleBuffered = True
        End Sub
    
        Private Sub Form1_ResizeEnd(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.ResizeEnd
            If lastWidth <> Me.Width Then
                If Me.Height <> CInt(Me.Width * 0.75) Then
                    Me.Height = CInt(Me.Width * 0.75)
                    lastHeight = Me.Height
                End If
                lastWidth = Me.Width
            End If
            If lastHeight <> Me.Height Then
                If Me.Width <> CInt(Me.Height * (1 + (1 / 3))) Then
                    Me.Width = CInt(Me.Height * (1 + (1 / 3)))
                    lastWidth = Me.Width
                End If
                lastHeight = Me.Height
            End If
        End Sub
    
    End Class

Tags for this Thread

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width