我是用以下这种方法改变的:
Option Explicit
Public Xtwips As Integer, Ytwips As Integer
Public Xpixels As Integer, Ypixels As Integer
Type FRMSIZE
Height As Long
Width As Long
End Type
Public RePosForm As Boolean
Public DoResize As Boolean
Dim MyForm As FRMSIZE
Dim DesignX As Integer
Dim DesignY As Integer
Dim ScaleFactorX As Single, ScaleFactorY As Single
Sub Resize_For_Resolution(ByVal SFX As Single, ByVal SFY As Single, MyForm As Form)
Dim i As Integer
Dim SFFont As Single
SFFont = (SFX + SFY) / 2
On Error Resume Next
With MyForm
For i = 0 To .Count - 1
If TypeOf .Controls(i) Is ComboBox Then
.Controls(i).Left = .Controls(i).Left * SFX
.Controls(i).Top = .Controls(i).Top * SFY
.Controls(i).Width = .Controls(i).Width * SFX
Else
.Controls(i).Move .Controls(i).Left * SFX, _
.Controls(i).Top * SFY, _
.Controls(i).Width * SFX, _
.Controls(i).Height * SFY
End If
.Controls(i).FontSize = .Controls(i).FontSize * SFFont
Next i
If RePosForm Then
.Move .Left * SFX, .Top * SFY, .Width * SFX, .Height * SFY
End If
End With
End Sub
Public Sub FormResize(TheForm As Form)
Dim ScaleFactorX As Single, ScaleFactorY As Single
If Not DoResize Then
DoResize = True
Exit Sub
End If
RePosForm = False
ScaleFactorX = TheForm.Width / MyForm.Width
ScaleFactorY = TheForm.Height / MyForm.Height
Resize_For_Resolution ScaleFactorX, ScaleFactorY, TheForm
MyForm.Height = TheForm.Height
MyForm.Width = TheForm.Width
End Sub
Public Sub AdjustForm(TheForm As Form)
Dim Res As String ' Returns resolution of system
' Put the design time resolution in here
DesignX = 640
DesignY = 480
RePosForm = True
DoResize = False
Xtwips = Screen.TwipsPerPixelX
Ytwips = Screen.TwipsPerPixelY
Ypixels = Screen.Height / Ytwips
Xpixels = Screen.Width / Xtwips
ScaleFactorX = (Xpixels / DesignX)
ScaleFactorY = (Ypixels / DesignY)
TheForm.ScaleMode = 1
Resize_For_Resolution ScaleFactorX, ScaleFactorY, TheForm
Res = Str$(Xpixels) + " by " + Str$(Ypixels)
Debug.Print Res
MyForm.Height = TheForm.Height
MyForm.Width = TheForm.Width
End Sub
Private Sub Form_Load()
Call AdjustForm(Me)
End Sub
Private Sub Form_Resize()
Call FormResize(Me)
End Sub
其他控件都可以,就是sstab控件第二个选行里的控件不行
Dim a(), g(1) As Single
Dim t As Integer, dt As Integer
Private Sub Check1_Click()
Form_Resize
End Sub
Private Sub Command1_Click()
If t = 0 Then t = 0: dt = 100: Timer1.Enabled = True
End Sub
Private Sub Form_Activate()
If g(0) = 0 Then '原始值只記錄一次
g(0) = Form1.ScaleWidth: g(1) = Form1.ScaleHeight '一開始表單的大小
ReDim a(Form1.Controls.Count - 1, 5)
j = 0
For Each i In Form1.Controls '記錄每個物件的資料
a(j, 0) = i.Name
On Error Resume Next '避免某些物件沒有指定的屬性而錯誤
a(j, 1) = i.Left: a(j, 2) = i.Top
a(j, 3) = i.Width: a(j, 4) = i.Height
a(j, 5) = i.FontSize
On Error GoTo 0 '取消錯誤處理
j = j + 1
Next i
t = 0: dt = 100: Timer1.Enabled = True
End If
End Sub
Private Sub Form_Resize()
If Form1.WindowState <> 1 And g(0) > 0 And g(1) > 0 Then
'重算物件的新位置
For i = 0 To Form1.Controls.Count - 1
Set b = Controls(a(i, 0))
On Error Resume Next
b.Left = a(i, 1) / g(0) * Form1.ScaleWidth
b.Top = a(i, 2) / g(1) * Form1.ScaleHeight
b.Width = a(i, 3) / g(0) * Form1.ScaleWidth
b.Height = a(i, 4) / g(1) * Form1.ScaleHeight
If Form1.ScaleWidth / g(0) < Form1.ScaleHeight / g(1) Then
b.FontSize = a(i, 5) / g(0) * Form1.ScaleWidth
Else
b.FontSize = a(i, 5) / g(1) * Form1.ScaleHeight
End If
On Error GoTo 0
If TypeOf b Is PictureBox Then
If Check1.Value = 1 Then b.PaintPicture b.Picture, 0, 0, b.ScaleWidth, b.ScaleHeight Else b.Cls
End If
Set b = Nothing
Next i
End If
End Sub
Private Sub Timer1_Timer()
t = t + Sgn(dt)
If t > 15 Then dt = -dt
If Form1.WindowState = 2 Then Form1.WindowState = 0: Timer1.Interval = 100: GoTo kk
If t = 16 Then Form1.WindowState = 2 - Form1.WindowState: Timer1.Interval = 1000: GoTo kk
Form1.Move (Screen.Width - Form1.Width - dt * 3) / 2, (Screen.Height - Form1.Height - dt) / 2
Form1.Move Form1.Left, Form1.Top, Form1.Width + dt * 3, Form1.Height + dt
kk:
If dt < 0 And t < 1 Then t = 0: Timer1.Enabled = False
End Sub