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
Option Explicit
Private Type RECT
Left As Long
Top As Long
Width As Long
Height As Long
End Type
Private crlMsg() As RECT
Private sngWidth As Single
Private sngHeight As Single
Private sngScaleX As Single
Private sngScaleY As Single
Private intNum As Integer
Private Sub Form_Load()
'打开错误处理陷阱
On Error GoTo ErrGoto
'----------------------------------------------------
'代码正文
Dim i As Integer
intNum = Me.Controls.Count
ReDim Preserve crlMsg(intNum - 1) As RECT
For i = 0 To intNum - 1
crlMsg(i).Left = Me.Controls(i).Left
crlMsg(i).Top = Me.Controls(i).Top
crlMsg(i).Width = Me.Controls(i).Width
crlMsg(i).Height = Me.Controls(i).Height
Next
sngWidth = Me.Width
sngHeight = Me.Height
sngScaleX = 1
sngScaleY = 1
'----------------------------------------------------
Exit Sub
'-----------------------------
ErrGoto:
Resume Next
End Sub
Private Sub Form_Resize()
'打开错误处理陷阱
On Error GoTo ErrGoto
'----------------------------------------------------
'代码正文
Static oldX As Single
Static oldY As Single
Dim i As Integer
If Abs(sngScaleX - oldX) > 0.01 Or Abs(sngScaleY - oldY) > 0.01 Then
For i = 0 To intNum - 1
Me.Controls(i).Left = crlMsg(i).Left * sngScaleX
Me.Controls(i).Top = crlMsg(i).Top * sngScaleY
Me.Controls(i).Width = crlMsg(i).Width * sngScaleX
Me.Controls(i).Height = crlMsg(i).Height * sngScaleY
Next
End If
oldX = sngScaleX
oldY = sngScaleY
'----------------------------------------------------
Exit Sub
'-----------------------------
ErrGoto:
Resume Next