请高手帮帮忙改一下以下代码

proustfox 2006-11-08 03:06:16
前段时间我从网上找到一段代码其主要功能是使VB的控件能随屏幕分辨率的大小而改变大小,此段代码COPY到每个窗体代码中非常好用,在FORM LOAD 时调用SAVESIZES,FORM RESIZE中调用ResizeControls,但随着窗体数目的增加,每个窗体都这样就很烦了,所以我希望能将其变为函数或者类模块,我试了很多方法但是不行,所以想请大家帮忙改改,非常感谢


Public Type ControlPositionType
Left As Single
Top As Single
Width As Single
Height As Single
FontSize As Single
CName As String
End Type
Public m_ControlPositions() As ControlPositionType
Public m_FormWid As Single
Public m_FormHgt As Single
Public Sub SaveSizes()
Dim i As Integer
Dim ctl As Control

' Save the controls' positions and sizes.
ReDim m_ControlPositions(1 To Controls.Count)
i = 1
For Each ctl In Controls
With m_ControlPositions(i)
If TypeOf ctl Is Line Then
.Left = ctl.X1
.Top = ctl.Y1
.Width = ctl.X2 - ctl.X1
.Height = ctl.Y2 - ctl.Y1
ElseIf TypeOf ctl Is CommonDialog Then

ElseIf TypeOf ctl Is ImageList Then

Else
.Left = ctl.Left
.Top = ctl.Top
.Width = ctl.Width
.Height = ctl.Height
On Error Resume Next
' .FontSize = ctl.Font.Size
On Error GoTo 0
End If
End With
i = i + 1
Next ctl

' Save the form's size.
m_FormWid = ScaleWidth
m_FormHgt = ScaleHeight
End Sub
Public Sub ResizeControls()
Dim i As Integer
Dim ctl As Control
Dim x_scale As Single
Dim y_scale As Single

' Don't bother if we are minimized.
If WindowState = vbMinimized Then Exit Sub

' Get the form's current scale factors.
x_scale = ScaleWidth / m_FormWid
y_scale = ScaleHeight / m_FormHgt

' Position the controls.
i = 1
For Each ctl In Controls
With m_ControlPositions(i)
If TypeOf ctl Is Line Then
ctl.X1 = x_scale * .Left
ctl.Y1 = y_scale * .Top
ctl.X2 = ctl.X1 + x_scale * .Width
ctl.Y2 = ctl.Y1 + y_scale * .Height
ElseIf TypeOf ctl Is CommonDialog Then

ElseIf TypeOf ctl Is ImageList Then

ElseIf TypeOf ctl Is MaskEdBox Then
ctl.Left = x_scale * .Left
ctl.Top = y_scale * .Top
ElseIf TypeOf ctl Is DTPicker Then
ctl.Left = x_scale * .Left
ctl.Top = y_scale * .Top
ElseIf TypeOf ctl Is Label Then
ctl.Left = x_scale * .Left
ctl.Top = y_scale * .Top
ElseIf TypeOf ctl Is CommandButton Then
ctl.Left = x_scale * .Left
ctl.Top = y_scale * .Top
ElseIf TypeOf ctl Is TextBox Then
ctl.Left = x_scale * .Left
ctl.Top = y_scale * .Top
Else
ctl.Left = x_scale * .Left
ctl.Top = y_scale * .Top
ctl.Width = x_scale * .Width
If Not (TypeOf ctl Is ComboBox) Then
' Cannot change height of ComboBoxes.
ctl.Height = y_scale * .Height
End If
On Error Resume Next
' ctl.Font.Size = y_scale * .FontSize
On Error GoTo 0
End If
End With
i = i + 1
Next ctl
End Sub
...全文
110 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
proustfox 2006-11-10
  • 打赏
  • 举报
回复
不会呀,请帮帮忙,能否说详细一点,我从来没有在VB中做过类
zq972 2006-11-09
  • 打赏
  • 举报
回复
做成类,然后用数组就可以了阿

1,451

社区成员

发帖
与我相关
我的任务
社区描述
VB 控件
社区管理员
  • 控件
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧