7,763
社区成员
发帖
与我相关
我的任务
分享
Option Explicit
Private Type TControlInfo
ctrl As Control
Left As Single
Top As Single
Width As Single
Height As Single
FontSize As Single
End Type
Private mlngW As Long
Private mlngh As Long
Dim SaveCtrl() As TControlInfo
Private Sub Form_Initialize()
On Error Resume Next
mlngW = Me.Width
mlngh = Me.Height
'保存控件的状态
ReDim SaveCtrl(Me.Controls.Count - 1) As TControlInfo
Dim i As Integer
Dim ctrl As Control
For i = 0 To Me.Controls.Count - 1
Set ctrl = Me.Controls(i)
With SaveCtrl(i)
Set .ctrl = ctrl
.Left = ctrl.Left
.Top = ctrl.Top
.Width = ctrl.Width
.Height = ctrl.Height
.FontSize = ctrl.Font.Size
End With
Next
End Sub
Private Sub Form_Resize()
On Error Resume Next
Dim lngW As Long
Dim lngh As Long
Dim lngf As Single
Dim i As Integer
lngW = Me.Width
lngh = Me.Height
If (lngW / mlngW) < (lngh / mlngh) Then
lngf = (lngW / mlngW)
Else
lngf = (lngh / mlngh)
End If
For i = 0 To UBound(SaveCtrl)
With SaveCtrl(i)
If .ctrl.Left < 0 Then
.ctrl.Left = ((.ctrl.Left + 75000) * (lngW / mlngW)) - 75000
ElseIf .Left < 0 Then
.ctrl.Left = (.Left + 75000) * (lngW / mlngW)
Else
.ctrl.Left = .Left * (lngW / mlngW)
End If
.ctrl.Top = .Top * (lngh / mlngh)
.ctrl.Width = .Width * (lngW / mlngW)
.ctrl.Height = .Height * (lngh / mlngh)
'控件字体自适应大小
If .FontSize > 0 Then
If (.FontSize * lngf) < 8 Then
If (.FontSize * lngf) > 7 Then
.ctrl.Font.Size = 7
Else
.ctrl.Font.Size = .FontSize * lngf
End If
Else
.ctrl.Font.Size = .FontSize * lngf
End If
End If
End With
Next
End Sub