鱼C论坛

 找回密码
 立即注册
查看: 2549|回复: 3

[技术交流] [VB]手把手教你制作ActiveX控件

[复制链接]
发表于 2013-5-19 23:16:53 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
首先要做基本设置。
1.添加工程,类型[ActiveX 控件].
2.把工程的Name属性改成Label.(范例是一个能垂直居中的标签(Label)控件)
3.设定控件外观:
(1)添加一个Label控件.
(2)设置Label的Alignment为2(Center).
(3)把Label置于窗体中间.
接下来为控件执行初始化事件设置。
4.添加控件的基本事件:
(1)读取属性事件:
Private Sub UserControl_ReadProperties(PropBag As PropertyBag)
On Error Resume Next
m_backstyle = 0
Set Label1.Font = PropBag.ReadProperty("Font", Ambient.Font)
Label1.ForeColor = PropBag.ReadProperty("ForeColor", &H80000008)
Label1.Caption = PropBag.ReadProperty("Caption", "")
Label1.BackColor = PropBag.ReadProperty("BackColor", "")
End Sub
(2)写属性事件:
Private Sub UserControl_WriteProperties(PropBag As PropertyBag)
On Error Resume Next
Call PropBag.WriteProperty("BackColor", UserControl.BackColor, &H8000000F)
Call PropBag.WriteProperty("Font", Label1.Font, Ambient.Font)
Call PropBag.WriteProperty("ForeColor", Label1.ForeColor, &H80000008)
Call PropBag.WriteProperty("Caption", Label1.Caption, "CMX")
End Sub
(3)重设大小事件(在改变Width和Height属性时被触发)
Private Sub UserControl_Resize()
With Label1
.AutoSize = True
.AutoSize = False
.Left = 0
.Top = (UserControl.Height - .Height) / 2
.Width = UserControl.Width
End With
End Sub
接下来我们为ActiveX控件添加属性。
5.设定属性:
(1)选择[工具]菜单,单击[添加过程].
(2)输入属性名,选择类型为[属性].
(3)单击[确定],完成属性添加.
然后开始添加各种属性。
(4)设定Font属性:
Public Property Get Font() As Font
Set Font = Label1.Font '将用户控件的font属性设置为控件上label1控件的font属性
End Property
Public Property Set Font(ByVal New_Font As Font)
Set Label1.Font = New_Font '将用户修改后的font属性赋给label1.font
UserControl_Resize'由于字体会改变显示大小,所以重设控件大小
PropertyChanged "Font" '修改用户控件的font属性为最新的属性
End Property
然后开始添加各种属性。
(4)设定Font属性:
Public Property Get Font() As Font
Set Font = Label1.Font '将用户控件的font属性设置为控件上label1控件的font属性
End Property
Public Property Set Font(ByVal New_Font As Font)
Set Label1.Font = New_Font '将用户修改后的font属性赋给label1.font
UserControl_Resize'由于字体会改变显示大小,所以重设控件大小
PropertyChanged "Font" '修改用户控件的font属性为最新的属性
End Property

(5)设定BackColor属性:
Public Property Get BackColor() As Long
BackColor = Label1.BackColor
End Property
Public Property Let BackColor(ByVal New_BackColor As Long)
If Abs(New_BackColor) < 16777216 Then
'Label1.BackColor = Abs(New_BackColor)
UserControl.BackColor = Abs(New_BackColor)
PropertyChanged "BackColor"
End If
End Property
(6)设定ForeColor属性:Public Property Get ForeColor() As Long
ForeColor = Label1.ForeColor
End Property
Public Property Let ForeColor(ByVal New_ForeColor As Long)
If Abs(New_ForeColor) < 16777216 Then
Label1.ForeColor = Abs(New_ForeColor)
PropertyChanged "ForeColor"
End If
End Property

(7)设定Caption属性:
Public Property Get Caption() As String
Caption = Label1.Caption
End Property
Public Property Let Caption(ByVal New_Caption As String)
Label1.Caption = New_Caption
PropertyChanged "Caption"
End Property


最后一步是设置控件相应的事件。
添加事件使用Event语句。
这个控件需要定义三个事件。
6.设定控件事件:
Event Click()'设定单击事件,它应该在Label单击时被触发
Event Change()'设定更改事件,它应该在Label双击时被触发
Event DblClick()'设定双击事件,它应该在Label更改时被触发
Private Sub Label1_Change()
RaiseEvent Change'触发Change事件
End Sub
Private Sub Label1_Click()
RaiseEvent Click'触发Click事件
End Sub
Private Sub Label1_DblClick()
RaiseEvent DblClick'触发DblClick事件
End Sub
设定控件事件很简单吧?我学的时候也很惊奇




7.控件制作完了,编译出来就可以用了!



想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2013-5-20 10:05:12 | 显示全部楼层
真是难得给力的帖子啊。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2013-5-20 10:10:03 | 显示全部楼层
激动人心,无法言表!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2013-5-20 12:03:49 | 显示全部楼层
激动人心,无法言表!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-3-29 18:28

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表