VB 控件自适应大小 随窗体高宽度变化,VB 控件自适应大小 随窗体高宽变化,VB 控件自适应大小 随高宽度变化,VB 控件自适应大小 随窗体变化,VB 控件自适应窗体大小,VB 控件自适应窗体,VB 控件自适应大小,VB 控件适应窗体大小,VB 控件适应窗体,VB 控件适应大小,VB 控件自适应,VB 自适应。

VB 控件自适应大小,随窗体高宽度变化:

Option Explicit

Private FormOldWidth   As Long                                                  '保存窗体的原始宽度
Private FormOldHeight  As Long                                                  '保存窗体的原始高度

Public Sub ResizeInit(FormName As Form)                                         '在调用ResizerForm前先调用本函数
    'On Error Resume Next
    Dim Obj As Control
    FormOldWidth = FormName.ScaleWidth
    FormOldHeight = FormName.ScaleHeight
    On Error Resume Next
    For Each Obj In FormName
        Obj.Tag = Obj.Left & " " & Obj.Top & " " & Obj.Width & " " & Obj.Height & " "
    Next Obj
End Sub

Public Sub ResizeForm(FormName As Form)                                         '按比例改变表单内各元件的大小,在调用ResizerForm前先调用ResizerInit函数
    'On Error Resume Next
    Dim Pos() As String
    Dim Obj As Control
    Dim SX As Double, SY As Double
    SX = FormName.ScaleWidth / FormOldWidth                                     '保存窗体宽度缩放比例
    SY = FormName.ScaleHeight / FormOldHeight                                   '保存窗体高度缩放比例
    For Each Obj In FormName
        If Len(Obj.Tag) > 0 Then
            Pos = Split(Obj.Tag, " ")
            If TypeOf Obj Is ComboBox Or TypeOf Obj Is DriveListBox Then
                Obj.Move Pos(0) * SX, Pos(1) * SY, Pos(2) * SX
            Else
                Obj.Move Pos(0) * SX, Pos(1) * SY, Pos(2) * SX, Pos(3) * SY     '根据控件的原始位置及窗体改变大小的比例对控件重新定位与改变大小
            End If
        End If
    Next Obj
End Sub

Private Sub Form_Resize()
    Call ResizeForm(Me)                                                         '确保窗体改变时控件随之改变
End Sub

Private Sub Form_Load()
    Call ResizeInit(Me)                                                         '在程序装入时必须加入
End Sub