--当窗体大小改变时,如何动态的改变控件的大小是许多VB程序员头痛的事。有的人设置窗体Resizable但却不改变控件的大小;有的人则根据控件的绝对位置与窗口大小相加减的办法来重新定位控件与改变大小,这种办法比较繁琐,且不可重用;当然也有人则限定窗口干脆不让改变。有没有一种简便易行的办法?答案是肯定的,下面给出一个一劳永逸的办法,源程序如下: Option Explicit Private FormOldWidth As Long `保存窗体的原始宽度 Private FormOldHeight As Long `保存窗体的原始高度 `在调用ResizeForm前先调用本函数 Public Sub ResizeInit(FormName As Form) 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 On Error GoTo 0 End Sub `按比例改变表单内各元件的大小, 在调用ReSizeForm前先调用ReSizeInit函数 Public Sub ResizeForm(FormName As Form) Dim Pos(4) As Double Dim I As Long,TempPos As Long,StartPos As Long Dim Obj As Control Dim ScaleX As Double,ScaleY As Double ScaleX = FormName.ScaleWidth / FormOldWidth `保存窗体宽度缩放比例 ScaleY = FormName.ScaleHeight / FormOldHeight `保存窗体高度缩放比例 On Error Resume Next For Each Obj In FormName StartPos = 1 For I = 0 To 4 `读取控件的原始位置与大小 TempPos = InStr(StartPos,Obj.Tag," ",vbTextCompare) If TempPos > 0 Then Pos(I) = Mid(Obj.Tag,StartPos,TempPos - StartPos) StartPos = TempPos + 1 Else Pos(I) = 0 End If `根据控件的原始位置及窗体改变大小 的比例对控件重新定位与改变大小 Obj.Move Pos(0) * ScaleX,Pos(1) * ScaleY,Pos(2) * ScaleX,Pos(3) * ScaleY Next I Next Obj On Error GoTo 0 End Sub Private Sub Form_Load() Call ResizeInit(Me) `在程序装入时必须加入 End Sub Private Sub Form_Resize() Call ResizeForm(Me) `确保窗体改变时控件随之改变 End Sub ----本例中给出了二个函数:ResizeInit和ResizeForm,在调用ResizeForm之前必须先调用ResizeInit。你可以将本程序拷到窗体代码段里,然后在窗体里加入任意控件即可进行测试
原文链接:https://www.f2er.com/vb/263126.html