1
Imports System.Reflection
2
3
Public 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
16
End Class
17
18
public 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, False, True)
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 Object, ByVal e As System.EventArgs) Handles Me.Startup
138
'创建工具栏并添加工具栏按钮
139
AddCommandBar()
140
End Sub
141
142
Private Sub ThisApplication_Shutdown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shutdown
143
144
End Sub
145
146
End class
147
Imports System.Reflection2

3
Public Class MyAxHost4

5
Inherits System.Windows.Forms.AxHost6

7
Public Sub New()8
MyBase.New("59EE46BA-677D-4d20-BF10-8D8067CB8B33")9
End Sub10

11
Public Shared Function IPictureDisp(ByVal Image As System.Drawing.Image) As stdole.IPictureDisp12
IPictureDisp = CType(AxHost.GetIPictureDispFromPicture(Image), _13
stdole.IPictureDisp)14
End Function15

16
End Class17

18
public class ThisApplication19

20
'定义工具栏对象21
Dim _commandBar As Office.CommandBar22

23
Dim _commandBar_Name As String = "学院信息管理工具栏"24
Dim _commandBarButton_Name As String = "我的按钮"25

26
'定义按钮对象27
Dim _commandBarButton As Office.CommandBarButton28
'定义下拉框对象29
Dim _commandBarCombo As Office.CommandBarComboBox30

31
Dim _commandBarPopup As Office.CommandBarPopup32

33
Dim myButton As Office.CommandBarButton34

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 Sub39

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 Sub44

45
Private Sub AddCommandBar()46
'在Outlook中添加一个工具栏47
_commandBar = Me.ActiveExplorer().CommandBars.Add(_commandBar_Name, _48
Office.MsoBarPosition.msoBarTop, False, True)49
_commandBar.Visible = True50

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.msoButtonIconAndCaption59
'设置按钮标题60
_commandBarButton.Caption = _commandBarButton_Name61

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 Bitmap68
Dim bmpMask As Bitmap69

70
'从资源中载入按钮图片到Bitmap对象71
bmpPic = My.Resources.MyResource.pic72
'保存按钮图片到MemoryStream中73
bmpPic.Save(imgStreamPic, System.Drawing.Imaging.ImageFormat.Bmp)74

75
'从资源中载入掩码图片到Bitmap对象76
bmpMask = My.Resources.MyResource.mask77
'保存掩码图片到MemoryStream中78
bmpMask.Save(imgStreamMask, System.Drawing.Imaging.ImageFormat.Bmp)79

80

81
Dim ax As New MyAxHost82
Dim Pic As stdole.IPictureDisp83
Dim Mask As stdole.IPictureDisp84

85
'从MemoryStream中获得IPictureDisp接口86
Pic = ax.IPictureDisp(Drawing.Image.FromStream(imgStreamPic))87
Mask = ax.IPictureDisp(Drawing.Image.FromStream(imgStreamMask))88

89
'设置按钮的图片和掩码图片90
_commandBarButton.Picture = Pic91
_commandBarButton.Mask = Mask92

93
_commandBarButton.Visible = True94

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 = 1104
_commandBarCombo.Visible = True105

106
'设置Change事件处理函数107
AddHandler _commandBarCombo.Change, AddressOf Combo_Change108

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.msoButtonIconAndCaption126
myButton.Caption = "弹出菜单项"127
myButton.Picture = Pic128
myButton.Mask = Mask129
myButton.Visible = True130

131
'设置菜单项的事件处理函数132
AddHandler myButton.Click, AddressOf MyButton_Click133

134
_commandBarPopup.Visible = True135
136
End Sub137
Private Sub ThisApplication_Startup(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Startup138
'创建工具栏并添加工具栏按钮139
AddCommandBar()140
End Sub141

142
Private Sub ThisApplication_Shutdown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shutdown143

144
End Sub145

146
End class147


浙公网安备 33010602011771号