在VB中,如果窗体改变大小,控件也跟着改变,代码该怎么写?

来源:百度知道 编辑:UC知道 时间:2024/05/21 13:13:34
比如说,我在FORM上添加一个label控件,FORM窗体的大小改变了,我想把label控体也一起改变,有能帮助我的请回答一下,谢谢!~

Private Sub Form_Resize()

If Me.Width < 3000 Then Me.Width = 3000 '控制最小尺寸,不过这是简单的做法,有闪动的
If Me.Height < 3000 Then Me.Height = 3000

Command1.Top = 2 * Me.ScaleHeight / 3 '在窗体的2/3处
Command1.Left = (Me.ScaleWidth - Command1.Width) / 2 '居中

Text1.Top = Me.ScaleHeight / 3 '在窗体的 1/3处
Text1.Left = (Me.ScaleWidth - Text1.Width) / 2 '居中

'还可以改变控件的高度,长度,等等参数

End Sub

把改变控件大小的代码写到窗体的resize事件里,只要窗体的大小发生变化就会触发该事件,千万不要写到mousemove事件中,不然每次鼠标移动都会触发的,不管窗体大小是否发生变化

'示例
Private Sub Form_Resize()
'判断窗口是否最小化,以免产生除零异常
If Me.WindowState = 1 Then
End
End If

'改变窗口内部控件大小
Text1.Left = 10
Text1.Top = 10
Text1.Width = Me.ScaleWidth - 30
Text1.Height = Me.ScaleHeight - 40 - Command1.Height
Command1.Left = (Me.ScaleWidth - Command1.Width) / 2
Command1.Top = Text1.Top