使用 C# 向 Outlook 2003 添加 CommandBars






4.52/5 (17投票s)
2004年11月29日
3分钟阅读

134867

787
如何使用 C# 为 Outlook 2003 添加自定义 CommandBar。
引言
我假设您已经有一个初步的插件正在运行,因为我不会在这篇文章中向您展示如何实际创建插件。有很多关于此主题的好文章。由于所有 Office 应用程序都共享相同的 CommandBar,因此本文也适用于其他 Office 产品。
CommandBar 对象概述
让我们简要看一下 CommandBar 对象的结构
CommandBars
(CommandBar
)CommandBarControls
(CommandBarControl
)CommandBarButton
CommandBarCombBox
CommandBarPopup
CommandBars 集合
CommandBars
集合对象代表容器应用程序中的命令栏,因此它自然包含 CommandBar
对象。
添加新的 CommandBar
非常直接
applicationObject.ActiveExplorer().CommandBars.Add(object name,
object position, object menubar, object temporary);
让我们头疼的是所有参数都接受对象。让我们更详细地检查参数以及它们期望的类型
object name
->string
name新命令栏的名称。
object position
->MsoBarPosition
position命令栏的位置。期望来自
MsoBarPosition
枚举的值。msoBarLeft
、msoBarTop
、msoBarRight
、msoBarBottom
指示新命令栏的左、上、右和下坐标。msoBarFloating
表示新命令栏不会停靠。msoBarPopup
表示新命令栏将是快捷菜单。msoBarMenuBar
仅适用于 Macintosh。object menubar
->bool
menubar设置为
true
以用新的命令栏替换活动菜单栏。默认值为false
。object temporary
->bool
temporary设置为
true
以使命令栏临时化。临时命令栏在容器应用程序关闭时删除。默认值为false
。
将新的 Command Bar 添加到 CommandBars 集合
添加新栏非常简单
applicationObject.ActiveExplorer().CommandBars.Add ("custom",
MsoBarPosition.msoBarTop,false,true);
CommandBar 对象
将 CommandBar
添加到 CommandBars
集合会返回对新创建的命令栏的引用。
CommandBar myCommandBar =
applicationObject.ActiveExplorer().CommandBars.Add ("custom",
MsoBarPosition.msoBarTop,false,true);
如果您执行了上述代码,您可能会注意到新命令栏不可见。您可以通过简单地设置 myCommandBar.visible = true;
来更改此设置。
如果您想将新控件添加到现有命令栏,CommandBar
对象非常重要。添加命令栏很简单
CommandBar myCommandBar =
applicationObject.ActiveExplorer().CommandBars["Menu Bar"];
这行代码可以获取 Outlook 的主菜单。向主菜单添加新项非常直接
myCommandBar.Controls.Add(object type, object id,
object parameter, object before, object temporary);
再次,我们需要更仔细地检查参数
object type
->MsoControlType
type控件的类型。期望来自
MsoControlType
枚举的值。object id
->int
ID指定内置控件的整数。如果值为 1,或被省略,则会在命令栏中添加指定类型的空白自定义控件。您可以使用
System.Reflection
命名空间中的Missing.Value
属性来省略值,否则直接使用 1;object parameter
->object
parameter此参数的作用类似于普通 Windows 控件的“TAG”属性。您可以在此处存储任何用户定义的数据,或者直接省略该参数。
object before
->int
before一个数字,指示新控件在命令栏上的位置。新控件将插入到此位置的控件之前。如果省略,则控件将添加到命令栏的末尾。
object temporary
->bool
temporary设置为
true
以使命令栏临时化。临时命令栏在容器应用程序关闭时删除。默认值为false
。
修改现有的命令栏
CommandBar myCommandBar =
applicationObject.ActiveExplorer).CommandBars["Menu Bar"];
CommandBar.Controls.Add(MsoControlType.msoControlPopup,
1,"",1,true);
将新控件添加到现有命令栏会返回对新 CommandBar
控件的引用。使用此引用,您可以使新创建的菜单项可见并设置其标题。
CommandBarControl myCommandBarControl =
myCommandBar.Controls.Add(MsoControlType.msoControlPopup,1,"",1,true);
myCommandBarControl.Visible = true;
myCommandBarControl.Caption ="DEMO";
修改现有的工具菜单项
现在,让我们尝试修改现有的工具菜单并添加一个 msoControlPopup
。msoControlPopup
是一个可以展开并显示更多选项的子菜单项。
CommandBar myCommandBar =
applicationObject.ActiveExplorer().CommandBars["Tools"];
CommandBarControl myCommandBarControll =
myCommandBar.Add(MsoControlType.msoControlPopup,1,"",1,true);
myCommandBar.Visible = true;
cmdBarPopup.Caption ="Popup";
现在,我们需要向弹出按钮添加一个子按钮。我们可以使用以下代码来完成此操作
CommandBarPopup popup = (CommandBarPopup) myCommandBarControll;
CommandBar bar = popup.CommandBar;
CommandBarControl cmdBarControl =
bar.Controls.Add(MsoControlType.msoControlButton,1,"",1,true);
cmdBarControl.Caption ="subbutton";
添加事件
一旦我们的 Command Bars 按照我们的需求安排好,我们就可以开始添加事件处理程序了。
CommandBarButton button = (CommandBarButton)cmdBarControl;
button.Click += new _CommandBarButtonsEvents_ClickEventHandler(button_click);
结论
希望这篇文章对您有所帮助。我期待您的反馈。