Option Explicit
Private Declare Function LockWindowUpdate Lib "user32" (ByVal hwndLock As Long) As Long
' if True, also fonts are resized
'是否缩放字体
Public ResizeFont As Boolean
' if True, form's height/width ratio is preserved
'是否保持比例
Public KeepRatio As Boolean
Private Type TcontrolInfo
ctrl As Control
Left As Single
Top As Single
Width As Single
Height As Single
FontSize As Single
End Type
' this array holds the original position
' and size of all controls on parent form
'保存父窗体内所有控件的初始位置与大小
Dim Controls() As TcontrolInfo
' a reference to the parent form
'父窗体事件
Private WithEvents ParentForm As Form
' parent form's size at load time
'载入时父窗体的大小
Private ParentWidth As Single
Private ParentHeight As Single
' ratio of original height/width
'初始高宽比例
Private HeightWidthRatio As Single
Private Sub ParentForm_Load()
' the ParentWidth variable works as a flag
ParentWidth = 0
' save original ratio
'保存初始比例
HeightWidthRatio = ParentForm.Height / ParentForm.Width
End Sub
Private Sub UserControl_ReadProperties(PropBag As PropertyBag)
ResizeFont = PropBag.ReadProperty("ResizeFont", False)
KeepRatio = PropBag.ReadProperty("KeepRatio", False)
If Ambient.UserMode = False Then Exit Sub
' store a reference to the parent form and
' start receiving events
Set ParentForm = Parent
End Sub
Private Sub UserControl_WriteProperties(PropBag As PropertyBag)
PropBag.WriteProperty "ResizeFont", ResizeFont, False
PropBag.WriteProperty "KeepRatio", KeepRatio, False
End Sub
Private Sub UserControl_Resize()
' refuse to resize
'限制大小
Size 420, 420
End Sub
' trap the parent form's Resize event
' this include the very first resize event
' that occurs soon after form's load
Private Sub ParentForm_Resize()
'MsgBox "A"
LockWindowUpdate ParentForm.hwnd
If ParentWidth = 0 Then
Rebuild
Else
Refresh
End If
LockWindowUpdate 0
'MsgBox "B"
End Sub
' save size and position of all controls on parent form
' you should manually invoke this method each time you
' add a new control to the form
' (through Load method of a control array)
'
Sub Rebuild()
' rebuild the internal table
Dim I As Integer, ctrl As Control
' this is necessary for controls that don't support
' all properties (e.g. Timer controls)
On Error Resume Next
If Ambient.UserMode = False Then Exit Sub
' save a reference to the parent form
' and its initial size
Set ParentForm = UserControl.Parent
ParentWidth = ParentForm.ScaleWidth
ParentHeight = ParentForm.ScaleHeight
' read the position of all controls on the parent form
ReDim Controls(ParentForm.Controls.count - 1) As TcontrolInfo
For I = 0 To ParentForm.Controls.count
With Controls(I)
Set .ctrl = ParentForm.Controls(I)
.Left = ParentForm.Controls(I).Left
.Top = ParentForm.Controls(I).Top
.Width = ParentForm.Controls(I).Width
.Height = ParentForm.Controls(I).Height
.FontSize = ctrl.Font.Size
End With
Next
End Sub
' update size and position of controls on parent form
'更新父窗体内控件的大小与位置
Sub Refresh()
Dim I As Integer, ctrl As Control
Dim widthFactor As Single, heightFactor As Single
Dim minFactor As Single '比例因子
' inhibits recursive calls if KeepRatio = True
Static executing As Boolean
If executing Then Exit Sub
'如果不是运行时,则退出
If Ambient.UserMode = False Then Exit Sub
If KeepRatio Then
executing = True
' we must keep original ratio
If ParentForm.WindowState <> 2 And ParentForm.WindowState <> 1 Then ParentForm.Height = HeightWidthRatio * ParentForm.Width
executing = False
End If
' this is necessary for controls that don't support
' all properties (e.g. Timer controls)
'错误处理,不能调整所有控件的大小,比如 Timer 控件
On Error Resume Next
widthFactor = ParentForm.ScaleWidth / ParentWidth
heightFactor = ParentForm.ScaleHeight / ParentHeight
' take the lesser of the two
If widthFactor < heightFactor Then
minFactor = widthFactor
Else
minFactor = heightFactor
End If
' this is a regular resize
For I = 0 To UBound(Controls)
With Controls(I)
' the change of font must occur *before* the
' resizing to account for companion scrollbar
' of listbox and other similar controls
If ResizeFont Then
.ctrl.Font.Size = .FontSize * minFactor
End If
' move and resize the controls - we can't use a
' Move method because some controls do not
' support the change of all the four properties
' (eg. Height with comboboxes)
'.ctrl.Left = .Left * widthFactor
'.ctrl.Top = .Top * heightFactor
'.ctrl.Width = .Width * widthFactor
'.ctrl.Height = .Height * heightFactor
If InStr(UCase(.ctrl.Name), UCase("combo")) <> 0 Then
.ctrl.Left = .Left * widthFactor
.ctrl.Top = .Top * heightFactor
Else
.ctrl.Move .Left * widthFactor, .Top * heightFactor, .Width * widthFactor, .Height * heightFactor
End If
End With
Next
End Sub