Outlook 2007 加载项:如何向 msoControlButton 添加图标
-
14-12-2019 - |
题
背景:我正在用 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
这是另一种方法 由微软创建 基于 OleCreatePictureIndirect
.
如果你想使用内置图标,只需设置 FaceId
. 。下载 办公室图标库 查看 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 的图像。
不隶属于 StackOverflow