做一个画面,画面里放置一个趋势控件,趋势控件的宽度和高度自动跟随画面的宽度和高度,画面最大时,趋势控件也最大,问一下VB程序怎么写?
最佳答案
Option Explicit
Private FormOldWidth As Long
''保存窗体的原始宽度
Private FormOldHeight As Long
''保存窗体的原始高度
Public Sub ResizeInit(FormName As Form)
Dim Obj As Control
FormOldWidth = FormName.ScaleWidth
FormOldHeight = FormName.ScaleHeight
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
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 / FormOldWidth
ScaleY = FormName.ScaleHeight / FormOldHeight
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
Obj.Move Pos(0) * ScaleX, Pos(1) * ScaleY, Pos(2) * ScaleX, Pos(3) * ScaleY
Next i
Next Obj
On Error GoTo 0
End Sub
Private Sub Form_Resize()
Call ResizeForm(Me) ''确保窗体改变时控件随之改变
End Sub
Private Sub Form_Load()
Call ResizeInit(Me) ''在程序装入时加入
''以下四句是运行使窗体最大化
Me.Top = 0
Me.Left = 0
Me.Width = Screen.Width
Me.Height = Screen.Height
End Sub
提问者对于答案的评价:
谢谢
原创文章,作者:ximenziask,如若转载,请注明出处:https://www.zhaoplc.com/plc358063.html