用Visual Basic6类模块打造控件(二)

翻译|其它|编辑:郝浩|2005-12-05 09:31:00.000|阅读 1665 次

概述:

# 界面/图表报表/文档/IDE等千款热门软控件火热销售中 >>


3、类模块方法

’给分割条控件指定所在的窗体、Label控件、分割条类型等
Public Sub Attach(f As Form, sp As Label, hv As Integer,min As Long, max As Long)
 Set mForm = f ’设置窗体变量
 ’设置分割条控件变量为传入的Label控件
 Set SplitBar = sp
 ’给分割条做个标记,表明这个Label是分割条
 SplitBar.Tag = "SPLIT"
 If hv = 0 Then ’如果是水平分割条
  HorV = 0 ’设置分割条类型
  ’ 设置Label控件的鼠标光标为左右箭头
  SplitBar.MousePointer = 9
  ’最小位置与最大位置设置

 If max < min + SplitBar.Width Then
  iMin = 100
  iMax = mForm.ScaleWidth - SplitBar.Width - 100
 Else
  iMin = min
  iMax = max
 End If
Else
 HorV = 1 ’如果是水平分割条
 ’设置Label控件的鼠标光标为上下箭头
 SplitBar.MousePointer = 7
 If max < min + SplitBar.Height Then
  iMin = 100
  iMax = mForm.ScaleWidth - SplitBar.Height - 100
 Else
  iMin = min
  iMax = max
 End If
End If
End Sub

’添加分割条左侧的控件 如果不是水平分割条则退出
Public Sub SetLeftBind(c As Control)
 If HorV = 1 Then Exit Sub
 AddBindControl c, 1
End Sub

’添加分割条上方的控件 如果不是垂直分割条则退出
Public Sub SetUpBind(c As Control)
 If HorV = 0 Then Exit Sub
 AddBindControl c, 2
End Sub

’添加分割条下方的控件 如果不是垂直分割条则退出
Public Sub SetDownBind(c As Control)
 If HorV = 0 Then Exit Sub
 AddBindControl c, 3
End Sub

’帮助函数 私有 往控件数组里加入一个控件
Private Sub AddBindControl(c As Control, ipos As Integer)
 If numControls < 10 Then ’确保控件数组不溢出
  numControls = numControls + 1
  Set myBindControls(numControls - 1).binControl = c
  myBindControls(numControls - 1).pos = ipos
 End If
End Sub

’计算控件位置

Public Sub ArrangePosition()
 On Error GoTo err
 Dim i As Integer
 If HorV = 0 Then
  ’水平分割条 设置高度为窗体的高度
  SplitBar.Height = mForm.ScaleHeight - _
    SplitBar.Top - 10
 Else
 ’垂直分割条 设置宽度为窗体的宽度 如果要将垂直分割条嵌入水平分割条中 则将此分支去掉(见本文例图)
 ’SplitBar.Width = mForm.ScaleWidth - SplitBar.
 Left - 10
End If

Dim i1 As Integer
Dim i2 As Integer
Dim lf1 As Integer ’控件右侧或底部的边界
Dim lf2 As Integer ’控件右侧或底部的边界
’垂直分割 找到最右端的控件 上方为i1,下方为i2
If HorV = 1 Then
 For i = 0 To numControls - 1
  With myBindControls(i)
   If .pos = 2 Then
    If .binControl.Left + .binControl.Width > lf1 Then
     lf1 = .binControl.Left + .binControl.Width
     i1 = i
    End If
   ElseIf .pos = 3 Then
    If .binControl.Left + .binControl.Width > lf2 Then
     lf2 = .binControl.Left + .binControl.Width
     i2 = i
    End If
   End If
  End With
 Next i
Else ’水平分割 找到最底部的控件 左边为i1,右边为i2
 For i = 0 To numControls - 1
  With myBindControls(i)
   If .pos = 0 Then
    If .binControl.Top + .binControl.Height > lf1 Then
     lf1 = .binControl.Top + .binControl.Height
     i1 = i
    End If
   ElseIf .pos = 1 Then
    If .binControl.Top + .binControl.Height > lf2 Then
     lf2 = .binControl.Top + .binControl.Height
     i2 = i
    End If
   End If
  End With
 Next i
End If

’遍历控件数组进行位置计算
For i = 0 To numControls - 1
 With myBindControls(i) .binControl
  Select Case myBindControls(i).pos
   Case 0 ’左侧控件
    .Width = SplitBar.Left - .Left - 10
    If i = i1 Then ’如果是最底部的控件
     .Height = SplitBar.Top + SplitBar.Height - .Top
    End If
   Case 1 ’右侧控件
    .Left = SplitBar.Left + SplitBar.Width + 10
    .Width = mForm.ScaleWidth - SplitBar.Left - SplitBar.Width - 10
    If i = i2 Then ’如果是最底部的控件
     .Height = SplitBar.Top + SplitBar.Height - .Top
    End If
   Case 2 ’上方控件
    .Height = SplitBar.Top - .Top - 10
    If i = i1 Then ’如果是最右侧的控件
     .Width = SplitBar.Left + SplitBar.Width - .Left
    End If
   Case 3 ’下方控件
    .Top = SplitBar.Top + SplitBar.Height + 10
    .Height = mForm.ScaleHeight - SplitBar.Top- SplitBar.Height - 10
    If i = i2 Then ’如果是最右侧的控件
     .Width = SplitBar.Left + SplitBar.Width - .Left
    End If
   End Select
  End With
 Next i
err:
End Sub


4、类模块及控件事件

’类模块初始化
Private Sub Class_Initialize()
 numControls = 0 ’控件数设为0
 Resizing = False ’鼠标调整设为假
End Sub

’鼠标在Label控件上按下左键,开始调整
Private Sub SplitBar_MouseDown(Button As Integer, Shift
 As Integer, X As Single, Y As Single)
 If Button = vbLeftButton Then Resizing = True
End Sub

’鼠标在Label控件上抬起左键,结束调整

Private Sub SplitBar_MouseUp(Button As Integer, Shift As
 Integer, X As Single, Y As Single)
 If Button = vbLeftButton Then Resizing = False
End Sub

’鼠标移动事件
Private Sub SplitBar_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)

 ’得到鼠标位置
 
 GetCursorPos pot
 ’屏幕坐标转为窗体坐标
 ScreenToClient mForm.hwnd, pot
 ’如果鼠标不在调整则退出
 If Not Resizing Then Exit Sub
 If HorV = 0 Then ’如果是水平分割条
 ’如果鼠标在窗体上的水平位置超过最小值

 If pot.X * Screen.TwipsPerPixelX < iMin Then
 ’设置鼠标位置为窗体上水平位置最小值 退出
 pot.X = iMin / Screen.TwipsPerPixelX
 ClientToScreen mForm.hwnd, pot
 SetCursorPos pot.X, pot.Y
Exit Sub
 ’如果鼠标在窗体上的水平位置超过最大值
 ElseIf pot.X * Screen.TwipsPerPixelX > iMax Then
 ’设置鼠标位置为窗体上水平位置最大值 退出
 pot.X = iMax / Screen.TwipsPerPixelX
 ClientToScreen mForm.hwnd, pot
 SetCursorPos pot.X, pot.Y
Exit Sub
Else
 ’设置分割条的左侧位置为鼠标水平位置减去
 ’分割条宽度的二分之一

 SplitBar.Left = pot.X * Screen.TwipsPerPixelXSplitBar.Width / 2
End If
Else ’如果是垂直分割条
’如果鼠标在窗体上的水平位置超过最小值

If pot.Y * Screen.TwipsPerPixelY < iMin Then
 ’设置鼠标位置为窗体上水平位置最小值 退出
 pot.Y = iMin / Screen.TwipsPerPixelY
 ClientToScreen mForm.hwnd, pot
 SetCursorPos pot.X, pot.Y
 Exit Sub
 ’如果鼠标在窗体上的水平位置超过最大值
ElseIf pot.Y * Screen.TwipsPerPixelY > iMax Then
 ’设置鼠标位置为窗体上水平位置最大值 退出
 pot.Y = iMax / Screen.TwipsPerPixelY
 ClientToScreen mForm.hwnd, pot
 SetCursorPos pot.X, pot.Y
 Exit Sub
Else
 ’设置分割条的顶部位置为鼠标垂直位置
 ’减去分割条高度的二分之一

 SplitBar.Top = pot.Y * Screen.TwipsPerPixelY - SplitBar.Height / 2
 End If
End If
’调用子程序计算控件位置
ArrangePosition
End Sub

  至此分割条类模块编写完毕,下面就是实际使用测试。

  分割条的使用

  新建一窗体,在上面放两个文本框Text1、Text2,一个标签Label1,如图4:


图4 测试分隔条

  定义一个分割条实例:

Dim sp As New clsSplitBar

  1、窗体Load事件

Private Sub Form_Load()
sp.Attach Me, Label1, 0, 1000, 5000
sp.SetLeftBind Text1
sp.SetRightBind Text2
End Sub

  2、窗体Resize事件

Private Sub Form_Resize()
sp.ArrangePosition
End Sub

  使用类模块的优点

  相比ActiveX控件,类模块不需要编译控件,不需要控件注册。它是将类模块直接编译到应用程序中的,所以不会被别人非法使用。提高了代码重用性。而且由于是源码级的重用,可以方便的进行修改,从而更加灵活,可以适用不同的要求。笔者还用类模块写了一个语法着色控件,使用普通的RichTextBox控件,可以定义多种文字样式(每条样式包括字体、颜色、大小、下划线、粗体、斜体),最多可定义10组样式。每组样式都可以规定采用该样式的文字组。相信大家会做出更好的控件!

  在VB.net中,要创建带分割条的窗体非常简单。VB.net提供了一个分割条控件:Splitter。假设要创建一个可以左右调整大小的窗格,先在窗体上放一个面板Panel控件,设置其Dock 属性为Left,再从工具箱中拖动一个Splitter控件到窗体上,它会自动依靠在Panel控件的边缘,然后,再从工具箱中拖入第2 个Panel,这回,设置它的Dock属性为Fill。好了,运行看看,是不是就可以调整左右窗格的大小了?
 


标签:

本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,如果存在内容上的异议请邮件反馈至chenjj@evget.com


为你推荐

  • 推荐视频
  • 推荐活动
  • 推荐产品
  • 推荐文章
  • 慧都慧问
扫码咨询


添加微信 立即咨询

电话咨询

客服热线
023-68661681

TOP