求VB程序,实现的功能是:当窗体最大化时,其窗体中的控件可以随之变大。用的是VB6.0.
4个回答
展开全部
Private FormOldWidth As Long '保存窗体的原始宽度
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
If TypeOf Obj Is ComboBox Then
Obj.Tag = Obj.Left & " " & Obj.Top & " " & Obj.Width & " " & Obj.FontSize & " "
ElseIf TypeOf Obj Is CommandButton Then
Obj.Tag = Obj.Left & " " & Obj.Top & " " & Obj.Width & " " & Obj.Height & " " & Obj.FontSize & " "
ElseIf TypeOf Obj Is Line Then
Obj.Tag = Obj.X1 & " " & Obj.X2 & " " & Obj.Y1 & " " & Obj.Y2 & " " & Obj.BorderWidth & " "
Else
Obj.Tag = Obj.Left & " " & Obj.Top & " " & Obj.Width & " " & Obj.Height & " "
End If
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 5
'读取控件的原始位置与大小
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
'根据控件的原始位置及窗体改变大小的比例对控件重新定位与改变大小
Next i
If TypeOf Obj Is ComboBox Then
Obj.FontSize = Pos(3) * ScaleY
Obj.Move Pos(0) * ScaleX, Pos(1) * ScaleY, Pos(2) * ScaleX
ElseIf TypeOf Obj Is CommandButton Then
Obj.FontSize = Pos(4) * ScaleY
Obj.Move Pos(0) * ScaleX, Pos(1) * ScaleY, Pos(2) * ScaleX, Pos(3) * ScaleY
ElseIf TypeOf Obj Is Line Then
Obj.X1 = Pos(0) * ScaleX
Obj.X2 = (Pos(1) - Pos(0)) * ScaleX + Obj.X1
Obj.Y1 = Pos(2) * ScaleY
Obj.Y2 = (Pos(3) - Pos(2)) * ScaleY + Obj.Y1
Obj.BorderWidth = Pos(4) * Sqr(ScaleX * ScaleX + ScaleY * ScaleY)
Else
Obj.Move Pos(0) * ScaleX, Pos(1) * ScaleY, Pos(2) * ScaleX, Pos(3) * ScaleY
End If
Next Obj
On Error GoTo 0
End Sub
Private Sub Form_Initialize()
Call ResizeInit(Me)
End Sub
Private Sub Form_Resize()
Call ResizeForm(Me)
End Sub
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
If TypeOf Obj Is ComboBox Then
Obj.Tag = Obj.Left & " " & Obj.Top & " " & Obj.Width & " " & Obj.FontSize & " "
ElseIf TypeOf Obj Is CommandButton Then
Obj.Tag = Obj.Left & " " & Obj.Top & " " & Obj.Width & " " & Obj.Height & " " & Obj.FontSize & " "
ElseIf TypeOf Obj Is Line Then
Obj.Tag = Obj.X1 & " " & Obj.X2 & " " & Obj.Y1 & " " & Obj.Y2 & " " & Obj.BorderWidth & " "
Else
Obj.Tag = Obj.Left & " " & Obj.Top & " " & Obj.Width & " " & Obj.Height & " "
End If
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 5
'读取控件的原始位置与大小
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
'根据控件的原始位置及窗体改变大小的比例对控件重新定位与改变大小
Next i
If TypeOf Obj Is ComboBox Then
Obj.FontSize = Pos(3) * ScaleY
Obj.Move Pos(0) * ScaleX, Pos(1) * ScaleY, Pos(2) * ScaleX
ElseIf TypeOf Obj Is CommandButton Then
Obj.FontSize = Pos(4) * ScaleY
Obj.Move Pos(0) * ScaleX, Pos(1) * ScaleY, Pos(2) * ScaleX, Pos(3) * ScaleY
ElseIf TypeOf Obj Is Line Then
Obj.X1 = Pos(0) * ScaleX
Obj.X2 = (Pos(1) - Pos(0)) * ScaleX + Obj.X1
Obj.Y1 = Pos(2) * ScaleY
Obj.Y2 = (Pos(3) - Pos(2)) * ScaleY + Obj.Y1
Obj.BorderWidth = Pos(4) * Sqr(ScaleX * ScaleX + ScaleY * ScaleY)
Else
Obj.Move Pos(0) * ScaleX, Pos(1) * ScaleY, Pos(2) * ScaleX, Pos(3) * ScaleY
End If
Next Obj
On Error GoTo 0
End Sub
Private Sub Form_Initialize()
Call ResizeInit(Me)
End Sub
Private Sub Form_Resize()
Call ResizeForm(Me)
End Sub
展开全部
有一个专门的函数,放在公共库里面,能将就用,但是做的不是很好。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
我有代码,可以完全实现任何控件的自动缩放。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
什么控件?
我用Text1控件写一个:
Private Sub Form_Resize()
Text1.Width = Me.ScaleWidth
Text1.Height = Me.ScaleHeight
End Sub
text1可以改成别的代码可以直接复制,只需一个Text1
Me.ScaleWidth 和 Text1.Height = Me.ScaleHeight 后面可以减一些数字 ,如:
Private Sub Form_Resize()
Text1.Width = Me.ScaleWidth - 100
Text1.Height = Me.ScaleHeight -100
End Sub
就这么简单!
我用Text1控件写一个:
Private Sub Form_Resize()
Text1.Width = Me.ScaleWidth
Text1.Height = Me.ScaleHeight
End Sub
text1可以改成别的代码可以直接复制,只需一个Text1
Me.ScaleWidth 和 Text1.Height = Me.ScaleHeight 后面可以减一些数字 ,如:
Private Sub Form_Resize()
Text1.Width = Me.ScaleWidth - 100
Text1.Height = Me.ScaleHeight -100
End Sub
就这么简单!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询