添加一个模块
Option Explicit
Dim FormOldWidth As Long '窗体旧的宽度值
Dim FormOldHeight As Long '窗体旧的高度值
Public Sub ResizeInit(FormName As Form)
Dim pCtl As Control
'设置窗体旧的高度与宽度
FormOldWidth = FormName.ScaleWidth
FormOldHeight = FormName.ScaleHeight
On Error Resume Next
For Each pCtl In FormName
'设置窗体中控件的Tag值(根据空间的位置和大小来设置)
'返回或设置一个表达式,它存储程序需要的额外数据。
'与其它属性不同,Visual Basic 不使用 Tag 属性的值;
'可用该属性识别对象。
pCtl.Tag = pCtl.Left & " " & pCtl.Top & " " & pCtl.Width & " " & pCtl.Height & " "
Next pCtl
On Error GoTo 0
End Sub
Public Sub ResizeForm(FormName As Form)
Dim Pos(4) As Double
Dim i As Long, tmpPos As Long, staPos As Long
Dim pCtl As Control
Dim ScaleX As Double, ScaleY As Double
'保存窗体的宽度与高度缩放比例
ScaleX = FormName.ScaleWidth / FormOldWidth
ScaleY = FormName.ScaleHeight / FormOldHeight
On Error Resume Next
'变化窗体内的各控件
For Each pCtl In FormName
staPos = 1
For i = 0 To 4 '位置和大小
'取得控件的原始位置和大小
tmpPos = InStr(staPos, pCtl.Tag, " ", vbTextCompare)
If tmpPos > 0 Then
Pos(i) = Mid(pCtl.Tag, staPos, tmpPos - staPos)
staPos = tmpPos + 1
Else
Pos(i) = 0
End If
'根据控件的原始位置及窗体改变大小的
'比例对控件重新定位与改变大小
pCtl.Move Pos(0) * ScaleX, Pos(1) * ScaleY, Pos(2) * ScaleX, Pos(3) * ScaleY
Next i
Next pCtl
On Error GoTo 0
End Sub
在Form中添加代码
Private Sub Form_Load()
Call ResizeInit(Me)
End Sub
'窗体大小发生变化时,根据窗体改变大小的比例
'对窗体中各控件重新定位和改变大小。
Private Sub Form_Resize()
Call ResizeForm(Me)
End Sub
然后在窗体里放几个控件然后改变窗体大小试试
控件自适应窗体:
Dim oldheight&, oldwidth&
Private Sub Form_Load()
oldheight = Me.Height: oldwidth = Me.Width
End Sub
Private Sub Form_Resize()
Dim cnt As Control
For Each cnt In Controls
cnt.Width = cnt.Width * Me.Width / oldwidth
cnt.Height = cnt.Height * Me.Height / oldheight
Next
oldheight = Me.Height: oldwidth = Me.Width
End Sub
以上是VB控件大小自适应函数的类型不符合要求!的详细内容。更多信息请关注PHP中文网其他相关文章!