technofantasy

博客园 首页 新随笔 联系 订阅 管理
  1Imports System.Reflection
  2
  3Public Class MyAxHost
  4
  5    Inherits System.Windows.Forms.AxHost
  6
  7    Public Sub New()
  8        MyBase.New("59EE46BA-677D-4d20-BF10-8D8067CB8B33")
  9    End Sub

 10
 11    Public Shared Function IPictureDisp(ByVal Image As System.Drawing.Image) As stdole.IPictureDisp
 12        IPictureDisp = CType(AxHost.GetIPictureDispFromPicture(Image), _
 13                stdole.IPictureDisp)
 14    End Function

 15
 16End Class

 17
 18public class ThisApplication
 19
 20    '定义工具栏对象
 21    Dim _commandBar As Office.CommandBar
 22
 23    Dim _commandBar_Name As String = "学院信息管理工具栏"
 24    Dim _commandBarButton_Name As String = "我的按钮"
 25
 26    '定义按钮对象
 27    Dim _commandBarButton As Office.CommandBarButton
 28    '定义下拉框对象
 29    Dim _commandBarCombo As Office.CommandBarComboBox
 30
 31    Dim _commandBarPopup As Office.CommandBarPopup
 32
 33    Dim myButton As Office.CommandBarButton
 34
 35    Private Sub Combo_Change(ByVal Ctrl As Microsoft.Office.Core.CommandBarComboBox)
 36        MsgBox("你选择的是: " & Ctrl.List(Ctrl.ListIndex), _
 37                MsgBoxStyle.OkOnly, "VSTO 2005 Addin")
 38    End Sub

 39
 40    Public Sub MyButton_Click(ByVal buttonControl As Office.CommandBarButton, ByRef Cancel As Boolean)
 41        MessageBox.Show("You clicked: " & buttonControl.Caption, _
 42                "Custom Menu", MessageBoxButtons.OK)
 43    End Sub

 44
 45    Private Sub AddCommandBar()
 46        '在Outlook中添加一个工具栏
 47        _commandBar = Me.ActiveExplorer().CommandBars.Add(_commandBar_Name, _
 48            Office.MsoBarPosition.msoBarTop, FalseTrue)
 49        _commandBar.Visible = True
 50
 51
 52        '在工具栏中添加一个按钮
 53        _commandBarButton = _commandBar.Controls.Add(Office.MsoControlType.msoControlButton, _
 54                Type.Missing, _
 55                Type.Missing, _
 56                Type.Missing, False)
 57        '设置按钮的风格
 58        _commandBarButton.Style = Microsoft.Office.Core.MsoButtonStyle.msoButtonIconAndCaption
 59        '设置按钮标题
 60        _commandBarButton.Caption = _commandBarButton_Name
 61
 62        '定义用于载入图片的MemoryStream对象
 63        Dim imgStreamPic As System.IO.Stream = New System.IO.MemoryStream()
 64        Dim imgStreamMask As System.IO.Stream = New System.IO.MemoryStream()
 65
 66        '定义按钮图片和掩码图片对象
 67        Dim bmpPic As Bitmap
 68        Dim bmpMask As Bitmap
 69
 70        '从资源中载入按钮图片到Bitmap对象
 71        bmpPic = My.Resources.MyResource.pic
 72        '保存按钮图片到MemoryStream中
 73        bmpPic.Save(imgStreamPic, System.Drawing.Imaging.ImageFormat.Bmp)
 74
 75        '从资源中载入掩码图片到Bitmap对象
 76        bmpMask = My.Resources.MyResource.mask
 77        '保存掩码图片到MemoryStream中
 78        bmpMask.Save(imgStreamMask, System.Drawing.Imaging.ImageFormat.Bmp)
 79
 80
 81        Dim ax As New MyAxHost
 82        Dim Pic As stdole.IPictureDisp
 83        Dim Mask As stdole.IPictureDisp
 84
 85        '从MemoryStream中获得IPictureDisp接口
 86        Pic = ax.IPictureDisp(Drawing.Image.FromStream(imgStreamPic))
 87        Mask = ax.IPictureDisp(Drawing.Image.FromStream(imgStreamMask))
 88
 89        '设置按钮的图片和掩码图片
 90        _commandBarButton.Picture = Pic
 91        _commandBarButton.Mask = Mask
 92
 93        _commandBarButton.Visible = True
 94
 95        '创建下拉框
 96        _commandBarCombo = _commandBar.Controls.Add(Office.MsoControlType.msoControlComboBox, _
 97                Type.Missing, _
 98                Type.Missing, _
 99                Type.Missing, False)
100
101        _commandBarCombo.AddItem("Test 01")
102        _commandBarCombo.AddItem("Test 02")
103        _commandBarCombo.ListIndex = 1
104        _commandBarCombo.Visible = True
105
106        '设置Change事件处理函数
107        AddHandler _commandBarCombo.Change, AddressOf Combo_Change
108
109
110        '设置工具栏上的弹出按钮
111        _commandBarPopup = _commandBar.Controls.Add(Office.MsoControlType.msoControlPopup, _
112                Type.Missing, _
113                Type.Missing, _
114                Type.Missing, False)
115        _commandBarPopup.Caption = "我的弹出菜单"
116
117        '在弹出按钮中添加菜单项
118        myButton = _commandBarPopup.Controls.Add(Office.MsoControlType.msoControlButton, _
119                                Type.Missing, _
120                                Type.Missing, _
121                                Type.Missing, _
122                                True)
123
124        '设置菜单项属性
125        myButton.Style = Microsoft.Office.Core.MsoButtonStyle.msoButtonIconAndCaption
126        myButton.Caption = "弹出菜单项"
127        myButton.Picture = Pic
128        myButton.Mask = Mask
129        myButton.Visible = True
130
131        '设置菜单项的事件处理函数
132        AddHandler myButton.Click, AddressOf MyButton_Click
133
134        _commandBarPopup.Visible = True
135 
136    End Sub

137    Private Sub ThisApplication_Startup(ByVal sender As ObjectByVal e As System.EventArgs) Handles Me.Startup
138        '创建工具栏并添加工具栏按钮
139        AddCommandBar()
140    End Sub

141
142    Private Sub ThisApplication_Shutdown(ByVal sender As ObjectByVal e As System.EventArgs) Handles Me.Shutdown
143
144    End Sub

145
146End class

147
posted on 2006-06-28 16:25  陈锐  阅读(789)  评论(0)    收藏  举报