Option Explicit
Private ObjOldWidth As Long
'保存窗體的原始寬度
Private ObjOldHeight As Long '保存窗體的原始高度
Private ObjOldFont As Single '保存窗體的原始字體比
'在調(diào)用ResizeForm前先調(diào)用本函數(shù)
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
For Each Obj In FormName
Obj.Tag = Obj.Left & " " & Obj.Top
& " " & Obj.Width &
" " & Obj.Height & " "
Next Obj
On Error GoTo 0
End Sub
'按比例改變表單內(nèi)各元件的大小,
'在調(diào)用ReSizeForm前先調(diào)用ReSizeInit函數(shù)
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 /
ObjOldWidth
'保存窗體寬度縮放比例
ScaleY = FormName.ScaleHeight
/ ObjOldHeight
'保存窗體高度縮放比例
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
'根據(jù)控件的原始位置及窗體改變大
'小的比例對控件重新定位與改變大小
Obj.Move Pos(0) * ScaleX, Pos(1) * ScaleY, Pos(2) * ScaleX, Pos(3)
* ScaleY
Obj.Font.Size = ObjOldFont * FormName.ScaleHeight
Next i
Next Obj
On Error GoTo 0
End Sub
Private Sub Form_Resize()
'確保窗體改變時控件隨之改變
Call ResizeForm(Me)
End Sub
Private Sub Form_Load()
'在程序裝入時必須加入
Call ResizeInit(Me)
End Sub
|