利用VB自制OCX控件

来源:互联网 发布:linux tail刷新 编辑:程序博客网 时间:2024/05/01 07:42
导读:
  如今OCX控件在编程中已占领了很重要的地位,我们可以利用OCX控件完成一些相当复杂的编程操作.同时OCX 控件还有利于主程序的简单化、功能的重用、隐常程序实现细节、便于升级、传播方便等优点。现在我们可以利用VB 5.0方便的制作出自己的OCX控件供我们在编程中使用同时还可以把它送给你周围喜欢编程的朋友!
  下面列出制作OCX控件的步骤:
  一:新建OCX
  打开VB 5.0选择新建工程在对话框中选择ActiveX 控件(如图一(map1.gif))打开后会见一空的文档这就是 OCX控件的初始界面。想看一看空OCX控件的效果吗?选择“添加工程”选中标准EXE,这时你就可以像调用其它控件一样在左边的工具栏里选择刚才新建的OCX控件图标放在标准的EXE文档中看一看有什么效果!(什么也没有!) 自然因为刚才的OCX文档是空的嘛(废话太多,数个痰盂向我飞来)!
  二:创建界面
  一般我们用VB创建OCX控件都是在我们的控件里添加其它的控件来组合成一个完整的控件(也可以让它只完成某种算法)比如:你可以在上面添加一按钮、编辑框这时你再用第一部的方法看一看效果,是不是控件上多了一个按钮和一个编辑框。
  三:OCX属性
  一个OCX控件有许多的属性,比如控件背景是否透明(BackStyle),控件是否可以获得焦点 (CanGetFocus)等。这些属性都可以在控件的“属性框”中找到。
  四:添加事件
  一个控件有很多事件如:Click、MouseDown、MouseUp、MouseMove等。要触发这些事件都需要你加入代码。在控件的声明处加入Public Event Click()就表明该控件有一“Click”事件。自己编写的控件有什么事件就在声明处加几条事件。关于触发事件是使用“RaiseEvent”语句来完成的如:RaiseEvent 事件名(参数)。
  
  五:用户属性
  一个控件应有许多属性供用户设置如:控件的背景色、控件要显示的图形等。它们通常用Property Get和 Property Let两条语句来完成。前者表示给用户显示一个属性的值,后者表示用户设置一个属性的值。 六:保存属性和读取属性
  当属性被用户更改后需要将该属性值保存,以便控件运行时读取更改后的属性值。它们分别用 ReadProperty和WriteProperty两种方法来完成。前者表示读取一个属性值,后者表示写入一个属性值。
  好了一个简单的OCX控件制作方法大概就需要以上几步就可完成。下面本人将编写一个简单的OCX控件供大家参考。此控件的功能是在控件中显示一个圆,当鼠标移到控件上的时候控件上的圆便会在鼠标不离开控件的前提下跟随鼠标移动。
  新建一OCX控件,将控件的BorderStyle属性改为1,再加入一SHAPE控件将其形状改为Circle(如图二(map2.gif)) 添加以下代码:
  Public Event Click()'定义该控件要产生的事件
  Dim CircleX As Integer, CircleY As Integer
  Private Sub UserControl_Click()
  RaiseEvent Click'触发Click事件
  End Sub
  Private Sub UserControl_Initialize()
  CircleX = Shape1.Width / 2
  CircleY = Shape1.Height / 2
  End Sub
  Private Sub UserControl_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
  Shape1.BackColor = RGB(0, 0, 255)
  End Sub
  Private Sub UserControl_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
  Dim MoveX As Integer, MoveY As Integer
  MoveX = X - Shape1.Width / 2
  MoveY = Y - Shape1.Height / 2
  If (MoveX <0) Or (MoveX + Shape1.Width >UserControl.ScaleWidth) Or _
  (MoveY <0) Or (MoveY + Shape1.Height >UserControl.ScaleHeight) Then Exit Sub
  Shape1.Move MoveX, MoveY
  End Sub
  Private Sub UserControl_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
  Shape1.BackColor = RGB(255, 0, 0)
  End Sub
  Property Get PosX() As Integer'取得CircleX的值显示给用户
  PosX = CircleX
  End Property
  Property Let PosX(ByVal New_X As Integer)'把用户写入的值设置到OCX控件内部
  If (New_X   (New_X >UserControl.ScaleWidth - Shape1.Width / 2) Then
  MsgBox ("圆的X值超出界限了")
  Else
  CircleX = New_X
  Call UserControl_Resize
  End If
  End Property
  Property Get PosY() As Integer
  PosY = CircleY
  End Property
  Property Let PosY(ByVal New_Y As Integer)
  If (New_Y   (New_Y >UserControl.ScaleHeight - Shape1.Height / 2) Then
  MsgBox ("圆的Y值超出界限了")
  Else
  CircleY = New_Y
  Call UserControl_Resize
  End If
  End Property
  Private Sub UserControl_ReadProperties(PropBag As PropertyBag)
  CircleX = PropBag.ReadProperty("CircleX", Shape1.Width / 2)'将用户设置的值读出来
  CircleY = PropBag.ReadProperty("CircleY", Shape1.Height / 2)'同上
  Call UserControl_Resize
  End Sub
  Private Sub UserControl_Resize()
  Shape1.Move CircleX, CircleY
  End Sub
  Private Sub UserControl_WriteProperties(PropBag As PropertyBag)
  Call PropBag.WriteProperty("CircleX", CircleX, Shape1.Width / 2)'将用户设置的值保存
  Call PropBag.WriteProperty("CircleY", CircleY, Shape1.Height / 2)'同上
  End Sub

本文转自
http://www.evget.com/article/read_1740.aspx
原创粉丝点击