65.9K
CodeProject 正在变化。 阅读更多。
Home

向 Outlook 添加 CommandBar 控件

starIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIconemptyStarIcon

1.40/5 (10投票s)

2007年2月2日

2分钟阅读

viewsIcon

57388

向 Outlook 应用程序添加按钮、事件

引言

ADD-IN 基本上是一个软件程序,用于扩展更大程序的功能。例如,我设计了许多 Outlook 加载项和 Excel 加载项,以提供 Microsoft Outlook 以及 Microsoft Excel 提供的更基本的功能。它为应用程序添加了图形和通信功能。我已经在 Visual Studio 2003 中创建了加载项,这些加载项访问 Outlook 2003 的对象模型,并使用 ThisApplication 类的成员。

ThisApplication 继承自 Microsoft.Office.Tools.Outlook.Application 类。 此类包装了 Microsoft.Office.Interop.Outlook.Application 类,该类表示 Outlook 应用程序。 这是 Outlook 对象模型中的最高级别类。 您可以使用 ThisApplication 类中的 this(在 C# 中)来访问 Microsoft.Office.Interop.Outlook.Application 类的成员。 ThisApplication 类之外的代码可以使用 Globals.ThisApplication 访问 ThisApplication 对象。 由于所有 Office 应用程序共享相同的 commandbars,因此本文也适用于其他 Office 产品。

概述

以下是 commandbar 的结构

CommandBars (CommandBar)
CommandBarControls (CommandBarControl)
CommandBarButton
CommandBarCombBox
CommandBarPopup
CommandBars 集合对象表示容器应用程序中的 command bar。

添加新的 command bars 集合。

applicationObject.ActiveExplorer().CommandBars.Add(object name, object position, object menubar, object temporary);
这里所有参数都接受一个对象。 让我们更详细地看一下。

object name:新 commandbar 的名称(字符串名称);

object position – msobarposition 位置,它指示 commandbar 的位置; msoBarLeft、msoBarTop、msoBarRight、msoBarBottom 指示新 command bar 的左、上、右和下坐标。 msoBarFloating 指示新的 command bar 不会被停靠。 msoBarPopup 指示新的 command bar 将是一个快捷菜单。 msoBarMenuBar 仅适用于 Macintosh。

object menubar:bool menubar; 将其设置为 true 以使用新的 commandbar 替换活动的 commandbar,默认值为 false。

object temporary:bool temporary; true 使 command bar 成为临时的。 临时 command bar 在容器应用程序关闭时被删除。 默认值为 false。

将 command bar 控件添加到 commandbar 集合

将 CommandBar 添加到 CommandBars 集合会返回对新创建的 command bar 的引用。

CommandBar myCommandBar = applicationObject.ActiveExplorer().CommandBars.Add ("custom", MsoBarPosition.msoBarTop,false,true); myCommandBar.visible = true;

添加按钮控件

现在我们可以在所有已创建的 commandbar 控件 myCommandBar 上添加一个按钮控件。 以下代码基本上用于在此 command bar 上添加按钮。

CommandBarControl cmdBarControl2 = myCommandBar.Controls.Add(MsoControlType.msoControlButton,1,"",Missing.Value,true);
cmdBarControl2.Visible = true;
cmdBarControl2.Caption ="My Test button";

在此控件上添加添加按钮后,我们想在此按钮的点击上添加事件处理程序

CommandBarButton btnclickhandler = (CommandBarButton)cmdBarControl2;
btnclickhandler.Click +=new CommandBarButtonEvents_ClickEventHandler(btnclickhandler _Click); 

btnclickhandler _Click() 是一个将处理点击事件的函数。

添加弹出控件

之后,我们可以将弹出菜单添加到 commandbar 控件。 这些控件是 CommandBarControl 类型,并作为 CommandBarControl 对象上的弹出按钮添加。

CommandBarControl cmdBarControl = myCommandBar.Controls.Add(MsoControlType.msoControlPopup,1,"",1,true); 
cmdBarControl.Visible = true; 
cmdBarControl.Caption ="PopUp Control"; 
CommandBarPopup popup = (CommandBarPopup)cmdBarControl; 
CommandBar newbar = popup.CommandBar; 

在此弹出控件上添加弹出按钮

newbar.Controls.Add(MsoControlType.msoControlButton,1,"",1,true); 
mybarhost.Visible = true; 
mybarhost.Caption = "Popup button"; 

同样,如果我们想在此按钮的点击上添加事件处理程序,我们可以这样做

buttonhost.Click +=new _CommandBarButtonEvents_ClickEventHandler(buttonhost_Click); 

在我的下一篇文章中,我将讨论如何在按钮控件上添加图像。

关于 Kamlesh

在软件开发公司工作的 Dotnet 开发人员印度.
获得计算机应用硕士学位和商务学士学位,来自孟买大学, 印度.

© . All rights reserved.