vb: how to auto resize, expand, stretch control?
Website Design
Website Promotion
Graphic Design
Programming
Free Software
Computer Tips
Discount Stores
|
This site provides users with the information about vb, visual basic, vb6, how to auto expand control, stretch controls, and more.
If you think that this site is helpful, please recommend your friends to visit our site.
How to auto resize, expand, stretch controls?
The program provided here will automatically change the size of all the controls when the form is dragged larger or smaller. But you should note that the picture and the combbox cannot be resized.
The following is how to design the program:
1. Add the following code in declare section:
Option Explicit
Private Type ControlDef
Left As Integer
Top As Integer
Width As Integer
Height As Integer
FontSize As Integer
End Type
Private s_ControlPositions() As ControlDef
Private s_FormWid As Integer
Private s_FormHgt As Integer
Private i As Integer
Private s_control As Control
Private x_scale As Integer
Private y_scale As Integer
2. Create a sub with the name of SaveOriginalSizes and the code inside like the following:
Private Sub SaveOriginalSizes()
On Error Resume Next
ReDim s_ControlPositions(1 To Controls.Count)
i = 1
For Each s_control In Controls
With s_ControlPositions(i)
If TypeOf s_control Is Line Then
.Left = s_control.X1
.Top = s_control.Y1
.Width = s_control.X2 - s_control.X1
.Height = s_control.Y2 - s_control.Y1
Else
.Left = s_control.Left
.Top = s_control.Top
.Width = s_control.Width
.Height = s_control.Height
.FontSize = s_control.Font.Size
End If
End With
i = i + 1
Next s_control
s_FormWidth = ScaleWidth
s_FormHeight = ScaleHeight
End Sub
3. Create a sub with the name of AutoResizeControls and the code inside like the following:
Private Sub AutoResizeControls()
On Error Resume Next
If WindowState = vbMinimized Then Exit Sub
x_scale = ScaleWidth / s_FormWidth
y_scale = ScaleHeight / s_FormHeight
i = 1
For Each s_control In Controls
With s_ControlPositions(i)
If TypeOf s_control Is Line Then
s_control.X1 = x_scale * .Left
s_control.Y1 = y_scale * .Top
s_control.X2 = s_control.X1 + x_scale * .Width
s_control.Y2 = s_control.Y1 + y_scale * .Height
Else
s_control.Left = x_scale * .Left
s_control.Top = y_scale * .Top
s_control.Width = x_scale * .Width
If Not (TypeOf s_control Is ComboBox) Then
s_control.Height = y_scale * .Height
End If
s_control.Font.Size = y_scale * .FontSize
End If
End With
i = i + 1
Next s_control
End Sub
4. In Form_Load, you need first save the original sizes of the controls and the code like the following:
Private Sub Form_Load()
'for testing WebBrowser, you need to add a WebBrowser control
WebBrowser1.Navigate "http://www.edusoftmax.com/"
SaveOriginalSizes
End Sub
5. In Form_Resize, you need to call the resize function and the code like the following:
Private Sub Form_Resize()
AutoResizeControls
End Sub
|