2012-03-14 31 views
6

Thông tin cơ bản: Tôi đang phát triển một bổ trợ Outlook 2007 trong VS2010 trong C#. Điều cụ thể mà tôi đang làm là thêm một mục trình đơn vào menu ngữ cảnh được liên kết với một email. Tôi làm điều này với mã sau:Bổ trợ Outlook 2007: Cách thêm biểu tượng vào msoControlButton

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"); 
} 

Vấn đề: Không thể đặt hình ảnh. Các ví dụ của Msdn dựa trên các hàm chuyển đổi AxHost mà tôi không có. Có cách nào đơn giản để chỉ đặt Hình ảnh hoặc Bản đồ Bitmap thành Ảnh không?

Cảm ơn.

Trả lời

6

Nếu bạn muốn có một hình ảnh tùy chỉnh, bạn phải dựa vào cách tiếp cận AxHost (see MSDN reference) hoặc PictureDispConverter đó là một cách tiếp cận dựa trên created by MicrosoftOleCreatePictureIndirect.

Nếu bạn muốn sử dụng biểu tượng tích hợp, bạn chỉ có thể đặt FaceId. Tải xuống Office Icons Gallery để xem giá trị Office 2007 FaceId.

+1

Bạn rất tuyệt vời. Cảm ơn. – kmarks2

+0

Office Icons Gallery dường như không còn FaceId nữa. Chỉ cần phong cách mới có tên là biểu tượng. Họ dường như có chúng trên http://www.outlookexchange.com/articles/toddwalker/BuiltInOLKIcons.asp mặc dù. –

3

Các mã sau đây sử dụng một System.Drawing.Bitmap (được lưu trữ như một tài nguyên) và chuyển nó đến một hình ảnh, đó là chuyển nhượng để 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); 
    } 
}  

Lưu ý: Thêm hình ảnh với tên Icon1 trong tài nguyên.

1

Chỉ cần thông tin, nếu bạn muốn áp dụng bất kỳ hình ảnh tích hợp văn phòng nào cho nút của bạn (xem thư viện hình ảnh trong here), bạn có thể chỉ cần gọi phương thức GetImageMso().

CommandBarButton.Picture = Application.CommandBars.GetImageMso("ImageMSO", 16, 16); 

Đây là một phương pháp thay thế để sử dụng FaceID tài sản.

+0

Tôi đã thử rằng một nhưng thấy rằng hình ảnh không thực sự hiển thị đúng, ít nhất là trong năm 2007. Nó dường như mất thuộc tính trong suốt, vì vậy bạn có được một phác thảo xung quanh hình ảnh. Ngoài ra, trong Outlook bạn cần thêm một cấp độ chuyển hướng để gọi hàm: Globals.Addin.Application.ActiveExplorer(). CommandBars.GetImageMso ("AcceptInvitation", 16, 16); –

Các vấn đề liên quan