-
Jan 11th, 2018, 02:33 PM
#1
Thread Starter
Addicted Member
[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
-
Jan 11th, 2018, 02:59 PM
#2
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
-
Jan 11th, 2018, 03:01 PM
#3
Thread Starter
Addicted Member
Re: manual window resize with limits..
-
Jan 11th, 2018, 03:06 PM
#4
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!
-
Jan 11th, 2018, 03:09 PM
#5
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
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|