Option Explicit
Private ObjOldWidth As Long '保存窗体的原始宽度
Private ObjOldHeight As Long '保存窗体的原始高度
Private ObjOldFont As Single '保存窗体的原始字体比
Declare Function SetWindowPos Lib "user32" (ByVal hwnd As Long, ByVal hWndInsertAfter As Long, _
ByVal x As Long, ByVal y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long
'在调用ResizeForm前先调用本函数
Public Sub ResizeInit(FormName As Form)
Dim Obj As Control
ObjOldWidth = FormName.ScaleWidth
ObjOldHeight = FormName.ScaleHeight
ObjOldFont = FormName.Font.Size / ObjOldHeight
On Error Resume Next
' *******Each是一个关键字,作用是针对一个数组或集合中的每个元素,重复执行一组语句
For Each Obj In FormName
'*******Tag返回或设置一个表达式用来存储程序中需要的额外数据。
If TypeOf Obj Is ComboBox Then
Obj.Tag = Obj.Left & " " & Obj.Top & " " & Obj.Width & " " & Obj.FontSize & " "
ElseIf TypeOf Obj Is CommandButton Then
Obj.Tag = Obj.Left & " " & Obj.Top & " " & Obj.Width & " " & Obj.Height & " " & Obj.FontSize & " "
ElseIf TypeOf Obj Is Line Then
Obj.Tag = Obj.X1 & " " & Obj.X2 & " " & Obj.Y1 & " " & Obj.Y2 & " " & Obj.BorderWidth & " "
Else
If Obj.Left >= 0 Then Obj.Tag = Obj.Left & " " & Obj.Top & " " & Obj.Width & " " & Obj.Height & " "
'**************Obj.Left < 0是隐藏控件,+75000来保存其没隐藏的坐标记录
If Obj.Left < 0 Then Obj.Tag = Obj.Left + 75000 & " " & Obj.Top & " " & Obj.Width & " " & Obj.Height & " "
End If
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
Dim ScaleY As Double
ScaleX = FormName.ScaleWidth / ObjOldWidth '保存窗体宽度缩放比例
ScaleY = FormName.ScaleHeight / ObjOldHeight '保存窗体高度缩放比例
On Error Resume Next
For Each Obj In FormName
StartPos = 1
For i = 0 To 4
'读取控件的原始位置与大小
'InStr函数,返回 Variant (Long),指定一字符串在另一字符串中最先出现的位置。语法:InStr([start, ]string1, string2[, compare])
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
Next i
'根据控件的原始位置及窗体改变大小的比例对控件重新定位与改变大小
If TypeOf Obj Is ComboBox Then
Obj.FontSize = Pos(3) * ScaleY
Obj.Move Pos(0) * ScaleX, Pos(1) * ScaleY, Pos(2) * ScaleX
ElseIf TypeOf Obj Is CommandButton Then
Obj.FontSize = Pos(4) * ScaleY
Obj.Move Pos(0) * ScaleX, Pos(1) * ScaleY, Pos(2) * ScaleX, Pos(3) * ScaleY
ElseIf TypeOf Obj Is Line Then
Obj.X1 = Pos(0) * ScaleX
Obj.X2 = (Pos(1) - Pos(0)) * ScaleX + Obj.X1
Obj.Y1 = Pos(2) * ScaleY
Obj.Y2 = (Pos(3) - Pos(2)) * ScaleY + Obj.Y1
Obj.BorderWidth = Pos(4) * Sqr(ScaleX * ScaleX + ScaleY * ScaleY)
Else '*********解决SSTab隐藏属性
If Obj.Left >= 0 Then Obj.Move Pos(0) * ScaleX, Pos(1) * ScaleY, Pos(2) * ScaleX, Pos(3) * ScaleY
'**********Obj.Left < 0是隐藏控件,进行移动后-75000来隐藏【因为之前Obj.Tag记录的是未隐藏的坐标】
If Obj.Left < 0 Then Obj.Move Pos(0) * ScaleX - 75000, Pos(1) * ScaleY, Pos(2) * ScaleX, Pos(3) * ScaleY
Obj.Font.Size = ObjOldFont * FormName.ScaleHeight
End If
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