如图,VB6.0做这样的窗口,如何随窗口大小改变按钮的大小?
答案:2 悬赏:50 手机版
解决时间 2021-11-15 16:12
- 提问者网友:姑娘长的好罪过
- 2021-11-15 06:51
如图,VB6.0做这样的窗口,如何随窗口大小改变按钮的大小?
最佳答案
- 五星知识达人网友:举杯邀酒敬孤独
- 2021-11-15 07:23
Private Sub Form_Resize()
Dim i As Integer, w As Single, h As Single
w = Me.ScaleWidth / 5
h = Me.ScaleHeight / 5
For i = 0 To 8
Me.Controls("Command" & i + 1).Move (i Mod 3) * 2 * w, (i 3) * 2 * h, w, h
Next
End Sub代码适用于VB6
追问我做的是用户控件(如图),代码似乎无效(一直显示要求对象)。。要怎么修改?
追答Private Sub UserControl_Resize()
Dim i As Integer, w As Single, h As Single
w = ScaleWidth / 5
h = ScaleHeight / 5
For i = 0 To 8
Controls("Command" & i + 1).Move (i Mod 3) * 2 * w, (i 3) * 2 * h, w, h
Next
End Sub 另外建议你把那九个Command做成控件数组,这样代码可以简化,而且运行效率高:
Private Sub UserControl_Resize()
Dim i As Integer, w As Single, h As Single
w = ScaleWidth / 5
h = ScaleHeight / 5
For i = 0 To 8
Command1(i).Move (i Mod 3) * 2 * w, (i 3) * 2 * h, w, h
Next
End Sub
Dim i As Integer, w As Single, h As Single
w = Me.ScaleWidth / 5
h = Me.ScaleHeight / 5
For i = 0 To 8
Me.Controls("Command" & i + 1).Move (i Mod 3) * 2 * w, (i 3) * 2 * h, w, h
Next
End Sub代码适用于VB6
追问我做的是用户控件(如图),代码似乎无效(一直显示要求对象)。。要怎么修改?
追答Private Sub UserControl_Resize()
Dim i As Integer, w As Single, h As Single
w = ScaleWidth / 5
h = ScaleHeight / 5
For i = 0 To 8
Controls("Command" & i + 1).Move (i Mod 3) * 2 * w, (i 3) * 2 * h, w, h
Next
End Sub 另外建议你把那九个Command做成控件数组,这样代码可以简化,而且运行效率高:
Private Sub UserControl_Resize()
Dim i As Integer, w As Single, h As Single
w = ScaleWidth / 5
h = ScaleHeight / 5
For i = 0 To 8
Command1(i).Move (i Mod 3) * 2 * w, (i 3) * 2 * h, w, h
Next
End Sub
全部回答
- 1楼网友:神也偏爱
- 2021-11-15 08:32
这是我收藏的部分源代码,你试一下第一个看看是不是,因为我忘记那个是了!
一、VB控件与窗口的等比缩放实现方法:
Private FormOldHeight As Long
'原始高度
'在调用ResizeForm前先调用本函数
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
二、
按比例改变表单内各元件的大小
'在调用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 / 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_Load()
Call ResizeInit(Me) '在装入时必须加入
End Sub
Private Sub Form_Resize()
Call ResizeForm(Me) '控件随之改变
End Sub
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯