Outlook 2007加载项:如何将图标添加到msoControlButton
本文关键字:图标 添加 msoControlButton 2007 加载项 Outlook | 更新日期: 2023-09-27 18:27:42
背景:我正在C#中的VS2010中开发Outlook 2007外接程序。我正在做的具体事情是将一个菜单项添加到与电子邮件关联的上下文菜单中。我用以下代码来做这件事:
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
Application.ItemContextMenuDisplay += Application_ItemContextMenuDisplay;
}
private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
{
}
private void Application_ItemContextMenuDisplay(Office.CommandBar commandBar, Outlook.Selection selection)
{
var cmdButtonCallContact = (Office.CommandBarButton)commandBar.Controls.Add(Office.MsoControlType.msoControlButton, 1, System.Reflection.Missing.Value, 6, System.Reflection.Missing.Value);
cmdButtonCallContact.Caption = "&Foo";
//cmdButtonCallContact.Picture = ?
cmdButtonCallContact.Click += cmdButtonCopy_Click;
}
private void cmdButtonCopy_Click(Office.CommandBarButton ctrl, ref bool canceldefault)
{
System.Windows.Forms.MessageBox.Show("Bar");
}
问题:似乎无法设置图片。Msdn示例依赖于我没有的AxHost转换函数。有没有一种简单的方法可以将图像或位图设置为图片?
谢谢。
如果您想要自定义映像,您必须依赖AxHost
方法(请参阅MSDN参考)或PictureDispConverter
,后者是Microsoft基于OleCreatePictureIndirect
创建的另一种方法。
如果您想使用内置图标,只需设置FaceId
即可。下载Office图标库以查看Office 2007 FaceId
值。
以下代码使用System.Drawing.Bitmap
(存储为资源)并将其转换为可分配给Office.CommandBarButton.Picture
的图像
private Office.CommandBarButton buttonOne;
void createbutton()
{
Office.CommandBar newMenuBar = Inspector.CommandBars.Add("EAD", Office.MsoBarPosition.msoBarTop, false, true);
buttonOne = (Office.CommandBarButton)newMenuBar.Controls.Add(Office.MsoControlType.msoControlButton, 1, missing, missing, true);buttonOne.Caption = "Ansari";
buttonOne.Style = Office.MsoButtonStyle.msoButtonIconAndWrapCaptionBelow;
buttonOne.Picture = getImage();
//Register send event handler
buttonOne.Click += buttonOne_Click;
newMenuBar.Visible = true;
}
void buttonOne_Click(Office.CommandBarButton Ctrl, ref bool CancelDefault)
{
MessageBox.Show("Hi");
}
private stdole.IPictureDisp getImage()
{
stdole.IPictureDisp tempImage = null;
try
{
System.Drawing.Bitmap newIcon = Properties.Resources.Icon1;
System.Windows.Forms.ImageList newImageList = new System.Windows.Forms.ImageList();
newImageList.Images.Add(newIcon);
tempImage = ConvertImage.Convert(newImageList.Images[0]);
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.Message);
}
return tempImage;
}
sealed public class ConvertImage : System.Windows.Forms.AxHost
{
private ConvertImage() : base(null)
{
}
public static stdole.IPictureDisp Convert(System.Drawing.Image image)
{
return (stdole.IPictureDisp)System.Windows.Forms.AxHost.GetIPictureDispFromPicture(image);
}
}
注意:在资源中添加名为Icon1的图像。
仅供参考,如果你想将任何office内置图像应用到你的按钮上(在这里查看图像库),你可以简单地调用GetImageMso()方法。
CommandBarButton.Picture = Application.CommandBars.GetImageMso("ImageMSO", 16, 16);
这是使用FaceID
属性的另一种方法。