窗體自動調整大小
关于窗体自动调整大小,网上资料一搜一大片,大部分雷同,我使用后发现都有些小毛病就是多托拉几次后,再拉回原来大小,画面显示效果和开始的时候有点不一样。所以针对网上的代码做了些修改,呵呵,不过变量名什么的没改。
Private ObjOldWidth As Long
Private ObjOldHeight As Long
两个全局变量保存原始的时候窗体大小
Private Sub MemScaler()
ObjOldWidth = Me.Width
ObjOldHeight = Me.Height
Dim objCtl As Control
For Each objCtl In Me.Controls
objCtl.Tag = objCtl.Left & "," & objCtl.Top & "," & objCtl.Width & "," & objCtl.Height & "," & objCtl.Font.Size
Next
End Sub
该函数记录每个控件的一些属性,在Form Load事件里调用一次就可以了。
在Form_Resize加入下边代码
Private Sub Form_Resize()
Dim nPosArr() As String
Dim sig1 As Single
Dim sig2 As Single
sig1 = Me.Width / ObjOldWidth
sig2 = Me.Height / ObjOldHeight
Dim objCtl As Control
For Each objCtl In Me.Controls
nPosArr = Split(objCtl.Tag, ",")
With objCtl
.Left = Int(CDbl(nPosArr(0)) * sig1)
.Top = Int(CDbl(nPosArr(1)) * sig2)
.Width = Int(CDbl(nPosArr(2)) * sig1)
.Height = Int(CDbl(nPosArr(3)) * sig2)
.Font.Size = Int(CDbl(nPosArr(4)) * sig2)
End With
Next
End Sub
OK,这就可以了。 写到这里突然发现上边转的是int,会不会出问题呢?估计宽度之类的应该是double吧,姑且这样吧。