窗體的控件隨窗體變化自動(dòng)調(diào)整大小
發(fā)表時(shí)間:2024-06-14 來源:明輝站整理相關(guān)軟件相關(guān)文章人氣:
[摘要]在程序的使用中,如果用戶點(diǎn)擊最大化或調(diào)整窗體的時(shí)候,窗體的控件依然不變化,非常不好看,所以我將這段源碼貼上,供參考!非常方便!Option ExplicitPrivate ObjOldWidth As Long '保存窗體的原始寬度Private ObjOldHeight As Long...
在程序的使用中,如果用戶點(diǎn)擊最大化或調(diào)整窗體的時(shí)候,窗體的控件依然不變化,非常不好看,所以我將這段源碼貼上,供參考!
非常方便!
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ù)控件的原始位置及窗體改變大
'小的比例對(duì)控件重新定位與改變大小
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()
'確保窗體改變時(shí)控件隨之改變
Call ResizeForm(Me)
End Sub
Private Sub Form_Load()
'在程序裝入時(shí)必須加入
Call ResizeInit(Me)
End Sub