(1)控件的容器是UserDocument而不是Form。在VB的IDE环境中那些可链接的窗体,其实都是一个个的UserDocument,因此,不要在Form上开发,而是要在UserDocument上开发,我也很奇怪为什么Microsoft给的模板要是From呢,直接给好UserDocument该有多好。
(2)UserDocument的属性设置,其实UserDocument的属性没有什么特殊的要求,但是有几点要特殊注意,一个是最好把ScrollBasr的属性该为0,这样就不会出现滑块了,还有最好不要做UserDocuemtn启动位置的设置,因为在VB的IDE里边所有的链接窗体的启动位置是自动记忆的,当然也包括大小,因此最好不要设置这些代码,否则就不爽了。
(3)UserDocument的名字一定要特殊注意,因为代码中要用到的。
(4)最关键的当然就是Connect里边的代码编写了,这里边的代码其实是很模板化的,没有什么技巧,大家照着案例写就行,关键代码如下:
声明部分:
Public VbInsTance As VBIDE.VBE
Public WithEvents MenuHandler As CommandBarEvents
Public WithEvents CtlHandler As VBControlsEvents
Private mcbMenuCommandBar As Office.CommandBarControl
Private mCodeAddIn As udCodeDesign
Private mControlAddIn As udControlTemplate
Private mCodeWindow As VBIDE.Window
Private mControlWindow As VBIDE.Window
Private mCodeGuid As String
Private mControlGuid As String
Show、Hide方法:
On Error Resume Next
SetVisible False
End Sub
Public Sub Show()
On Error Resume Next
Set mCodeAddIn.VbInsTance = VbInsTance
Set mCodeAddIn.Connect = Me
Set mControlAddIn.VbInsTance = VbInsTance
Set mControlAddIn.Connect = Me
SetVisible True
End Sub
AddinInstance_OnConnection事件
' 这个方法添加外接程序到 VB
' ------------------------------------------------------
Private Sub AddinInstance_OnConnection(ByVal Application As Object , ByVal ConnectMode As AddInDesignerObjects.ext_ConnectMode, ByVal AddInInst As Object , custom() As Variant)
On Error GoTo error_handler
Dim GuidHelper As New Helper.Guid
' 保存 vb 实例
Set VbInsTance = Application
If ConnectMode = ext_cm_External Then
' 用于让向导工具栏来启动此向导
Me.Show
Else
Set mcbMenuCommandBar = AddToAddInCommandBar( " DeveloperHelper " )
' 吸取事件
Set Me.MenuHandler = VbInsTance.Events.CommandBarEvents(mcbMenuCommandBar)
End If
' 挂接控件管理事件
Set Me.CtlHandler = VbInsTance.Events.VBControlsEvents( Nothing , Nothing )
' 将窗体嵌入Vb中
If GetSetting(App.Title, " Settings " , " CodeGUID " , " 0 " ) = " 0 " Then
mCodeGuid = GuidHelper.NewGuid()
SaveSetting App.Title, " Settings " , " CodeGUID " , mControlGuid
Else
mCodeGuid = GetSetting(App.Title, " Settings " , " CodeGUID " , " 0 " )
End If
Set mCodeWindow = VbInsTance.Windows.CreateToolWindow(AddInInst, " DeveloperHelper.udCodeDesign " , " CodeDesign " , mCodeGuid, mCodeAddIn)
If GetSetting(App.Title, " Settings " , " ControlGUID " , " 0 " ) = " 0 " Then
mControlGuid = GuidHelper.NewGuid()
SaveSetting App.Title, " Settings " , " ControlGUID " , mControlGuid
Else
mControlGuid = GetSetting(App.Title, " Settings " , " ControlGUID " , " 0 " )
End If
Set mControlWindow = VbInsTance.Windows.CreateToolWindow(AddInInst, " DeveloperHelper.udControlTemplate " , " ControlTemplate " , mControlGuid, mControlAddIn)
If ConnectMode = ext_cm_AfterStartup Then
If GetSetting(App.Title, " Settings " , " DisplayOnConnect " , " 0 " ) = " 1 " Then
' 设置这个到连接显示的窗体
Me.Show
End If
End If
Set GuidHelper = Nothing
Exit Sub
error_handler:
MsgBox Err.Description
End Sub
关键的代码是这句:
Set mCodeWindow = VbInsTance.Windows.CreateToolWindow(AddInInst, "DeveloperHelper.udCodeDesign", "CodeDesign", mCodeGuid, mCodeAddIn)
语法如下:
CreateToolWindow 方法
创建一个包含指定 UserDocument 对象的新工具窗口。
语法
object.CreateToolWindow (AddInInst, ProgID, Caption, GuidPosition, DocObj) As Window
CreateToolWindow 方法的语法包含下面部分:
部分 描述
object 对象表达式 ,其值是“应用于”列表中的对象。
AddInInst 必需的。外接程序实例变量,表示在开发环境中的外接程序。
ProgID 必需的。串 表示 UserDocument 对象的 progID。
Caption 必需的。串 包括窗口标题。
GuidPosition 必需的。串 包括窗口的唯一标识符。
DocObj 必需的。对象表示 UserDocument 对象。对象将被设置在这个函数的调用中。
记住这些基本上就差不多了,最后完成的效果如下图:
最终完成效果如下图:
在开发过程中,国内与IDE的插件开发相关的资料不是很多,其实IDE的开发其实是很重要的,对公司和个人来说都是很重要的,相关的技术资料如下:
1:http://www.activevb.de/tutorials/tut_addin/addin6.html
2:http://www.mysticboy.com.cn/docs/addin/addin3.htm