请教 VB 自动调整控件位置但不改变控件大小的代码
我在网上查了很久,一直都是随窗体变化而自动调整窗体内的控件位置及控件大小的代码,苦于本人也是初学者,请各位高手能帮一下下忙,在下感激不尽.我的要求是:只随窗体的变化自动调...
我在网上查了很久,一直都是随窗体变化而自动调整窗体内的控件位置及控件大小的代码,苦于本人也是初学者,请各位高手能帮一下下忙,在下感激不尽.
我的要求是:
只随窗体的变化自动调整窗体内的控件位置,就是按最初的比例在窗体变化后自动调整控件的位置,但不改变控件的大小.
'窗体部分
Private Sub Form_Resize()
'确保窗体改变时控件随之改变
Call ResizeForm(Me)
End Sub
Private Sub Form_Load()
'在程序装入时必须加入
Call ResizeInit(Me)
End Sub
'模块部分
Option Explicit
Private ObjOldWidth As Long '保存窗体的原始宽度
Private ObjOldHeight As Long '保存窗体的原始高度
Private ObjOldFont As Single '保存窗体的原始字体比
' '在调用ResizeForm前先调用本函数
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
'按比例改变表单内各元件的大小,
'在调用ReSizeForm前先调用ReSizeInit函数
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
'根据控件的原始位置及窗体改变大
'小的比例对控件重新定位与改变大小
Obj.Move Pos(0) * ScaleX, Pos(1) * ScaleY, Pos(2) * ScaleX, Pos(3) * ScaleY
Obj.Font.Size = ObjOldFont * FormName.ScaleHeight
Next i
Next Obj
End Sub
按照这段代码的话,这就是随窗体自动改变控件的(left,top,width,height,font)属性,现在我只要求改(left,top)的属性.
谢谢各位高手了 展开
我的要求是:
只随窗体的变化自动调整窗体内的控件位置,就是按最初的比例在窗体变化后自动调整控件的位置,但不改变控件的大小.
'窗体部分
Private Sub Form_Resize()
'确保窗体改变时控件随之改变
Call ResizeForm(Me)
End Sub
Private Sub Form_Load()
'在程序装入时必须加入
Call ResizeInit(Me)
End Sub
'模块部分
Option Explicit
Private ObjOldWidth As Long '保存窗体的原始宽度
Private ObjOldHeight As Long '保存窗体的原始高度
Private ObjOldFont As Single '保存窗体的原始字体比
' '在调用ResizeForm前先调用本函数
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
'按比例改变表单内各元件的大小,
'在调用ReSizeForm前先调用ReSizeInit函数
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
'根据控件的原始位置及窗体改变大
'小的比例对控件重新定位与改变大小
Obj.Move Pos(0) * ScaleX, Pos(1) * ScaleY, Pos(2) * ScaleX, Pos(3) * ScaleY
Obj.Font.Size = ObjOldFont * FormName.ScaleHeight
Next i
Next Obj
End Sub
按照这段代码的话,这就是随窗体自动改变控件的(left,top,width,height,font)属性,现在我只要求改(left,top)的属性.
谢谢各位高手了 展开
3个回答
展开全部
Obj.Move Pos(0) * ScaleX, Pos(1) * ScaleY, Pos(2) * ScaleX, Pos(3) * ScaleY
改为
Obj.Move Pos(0) * ScaleX, Pos(1) * ScaleY
就可以只改变位置不改变大小了
改为
Obj.Move Pos(0) * ScaleX, Pos(1) * ScaleY
就可以只改变位置不改变大小了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
Dim ww As Single, hh As Single
Private Sub Form_Load()
ww = Me.ScaleWidth
hh = Me.ScaleHeight
End Sub
Private Sub Form_Resize()
Dim nn As Control
Dim ww1 As Single, hh1 As Single
ww1 = Me.ScaleWidth / ww
hh1 = Me.ScaleHeight / hh
For Each nn In Me.Controls
nn.Move nn.Left * ww1, nn.Top * hh1
Next
ww = Me.ScaleWidth
hh = Me.ScaleHeight
End Sub
nn.Move nn.Left * ww1, nn.Top * hh1, nn.Width * ww1, nn.Height * hh1
就可以改变控件的大小了
Private Sub Form_Load()
ww = Me.ScaleWidth
hh = Me.ScaleHeight
End Sub
Private Sub Form_Resize()
Dim nn As Control
Dim ww1 As Single, hh1 As Single
ww1 = Me.ScaleWidth / ww
hh1 = Me.ScaleHeight / hh
For Each nn In Me.Controls
nn.Move nn.Left * ww1, nn.Top * hh1
Next
ww = Me.ScaleWidth
hh = Me.ScaleHeight
End Sub
nn.Move nn.Left * ww1, nn.Top * hh1, nn.Width * ww1, nn.Height * hh1
就可以改变控件的大小了
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
Private Sub Form_Resize()
t1.Width = Form1.Width * 0.2
t1.Height = Form1.Height * 0.2
End Sub
这个 0.2就是控件占form1的比例
改left top 一样
t1.Width = Form1.Width * 0.2
t1.Height = Form1.Height * 0.2
End Sub
这个 0.2就是控件占form1的比例
改left top 一样
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询