功能区栏是我发现的最佳路径,但是我很难找到一个很好的开始到结束的项目,所以我会在这里写一个小的文字。
向功能区添加一个按钮,仅用于现有邮件消息,包括该按钮的图像。
使用VS 2010 新项目,Office,选择“Outlook 2007 add in”,为您的项目输入一个名称。 到新创建的项目,添加新项“功能区(XML)”的名字你想要的东西,我把它叫做CustomRibbon 打开新创建CustomRibbon.xml文件,并更改标签节点有以下
label = "Text For The Custom Button"
supertip="tip for the button hover"
onAction ="ButtonClicked"
size="large"
getImage="GetCustomButtonImage" />
然后对CustomRibbon.cs文件有两个回调函数,一个叫做GetCustomButtonImage,另一个是ButtonClicked。
开放CustomRibbon.cs填写,这一点,在功能区回调区域添加以下
public void ButtonClicked(Office.IRibbonControl Control)
{
//Do work here
}
还添加以下在同一节
public stdole.IPictureDisp GetCustomButtonImage(Office.IRibbonControl control)
{
System.Drawing.Image myImage;
myImage = OutlookAddIn.Properties.Resources.ImageName;
return AxHostConverter.ImageToPictureDisp(myImage);
}
这将会再展有一个我们将很快做到这一点,但首先我们要在CustomRibbon.cs中添加我们需要的最后一部分。在IRibbonExtensibility会员区,在GetCustomUI改变现有的代码
public string GetCustomUI(string ribbonID)
{
if (ribbonID == "Microsoft.Outlook.Mail.Read")
{
return GetResourceText("OutlookAddIn.CustomRibbon.xml");
}
else
{
return "";
}
}
添加一个新类到您的项目称之为AxHostConverter,添加此添加到使用System.Windows.Forms的顶部
; using System.Drawing;
然后更改类有下面的代码
class AxHostConverter : AxHost
{
private AxHostConverter() : base("") { }
static public stdole.IPictureDisp ImageToPictureDisp(Image image)
{
return (stdole.IPictureDisp)GetIPictureDispFromPicture(image);
}
static public Image PictureDispToImage(stdole.IPictureDisp pictureDisp)
{
return GetPictureFromIPicture(pictureDisp);
}
}
添加您的形象,为您按钮的项目,并更改GetCustomButtonImage功能使用该资源。我使用了PNG,并且很好的运行透明胶片。
最后,所有应该剩下的工作就是下面添加到ThisAddIn.cs
protected override Microsoft.Office.Core.IRibbonExtensibility CreateRibbonExtensibilityObject()
{
return new CustomRibbon();
}
添加你想要的任何代码ButtonClicked和你设置。
使用Clickonce进行部署,安装非常简单。