添加 Test 工程 由于 .ocx 工程不能独立运行,所以我们将添加一个Test工程。在同一工程组中装入设计工程(menus工程)和测试工程(Test工程)将十分有利于调试运行中的部件。 1.在"文件"菜单上单击"添加工程"以打开"添加工程"对话框。 注意: 不要单击"打开工程" 或"新建工程",因为这样会关闭控件工程。 2.双击"Standard EXE"图标以添加普通的 .exe 工程(即我们的Test工程)。 该新工程立即成为这个工程组的"启动"工程。"工程资源管理器"窗口以黑体名字的显示来标识"启动"工程。 注意: ActiveX 控件工程是不能作为启动工程的。 3. 在"文件"菜单上单击"保存工程组"用以保存测试工程和工程组。 & ocx代码设计 & 1. 在menus工程的module模块中增加如下代码: Type menuRect '创建用户自定义类型 left As Integer top As Integer right As Integer bottom As Integer End Type 2. 在控件设计窗口中加入一个picturebox控件,取名为bmpmenu。 在bmpmenu中加入另一个picturebox控件,取名为menuitem。 注意: menuitem是bmpmenu的子控件。并且,这两个picturebox的 AutoSize属性皆设为True,BorderStyle属性皆设为None。 3. 在控件设计窗口中的General->Declarations中增加如下代码: Public g_selectedItem As String Dim g_fileName As String '菜单文件名 Dim g_height As Integer 'ocx控件的高度 Dim g_width As Integer 'ocx控件的宽度 Dim bmpRects() As menuRect 'bmpRects()用来记录所有菜单项的位置 Dim currentItemNumbers As Integer Dim currentItemNumber As Integer Public Event MENUITEMDOWN() 注意: MENUITEMDOWN是我们添加到控件中的一个事件,它的详细作用我们稍后会具体介绍。 4. 为我们的菜单控件添加两个属性: childs 和 BmpName childs属性表示菜单控件的菜单项数目, BmpName属性表示菜单控件的菜单文件名 首先,打开menuControl的代码窗口。在"工具"菜单上点击"添加过程"打开"添加过程"对话框。在"名称"框输入名字childs。单击"属性"和"公共的"后再单击"确定"。 你将看到如下的代码: Public Property Get childs() As Variant End Property
Public Property Let del1(ByVal vNewValue As Variant) End Property 然后,在代码窗口更改我们新创建的属性过程(修改的部份以黑体表示): Public Property Get childs() As Integer childs = currentItemNumbers End Property
Public Property Let childs(ByVal vNewValue As Integer) currentItemNumbers = vNewValue PropertyChanged "childs" PopAndResize ' 这是一个子过程 End Property 当你对childs属性赋新值时均执行 Property Let 过程, 而当你对childs属性检索该属性值时均执行 Property Get 过程。
对BmpName属性也同样处理,它的代码部份如下: Public Property Get BmpName() As String BmpName = g_fileName End Property
Public Property Let BmpName(ByVal vNewValue As String) g_fileName = vNewValue PropertyChanged "BmpName" PopAndResize End Property 5. 为了初始化ocx控件,在 UserControl_InitProperties 事件过程中 添加如下代码: Private Sub UserControl_InitProperties() BmpName = "" childs = 0 End Sub 在UserControl_Resize 事件过程中添加如下代码: Private Sub UserControl_Resize(