c# - 如何以编程方式在 Outlook 搜索文件夹上设置自定义图标? (即 Outlook 文件夹 SetCustomIcon)

标签 c# outlook vsto outlook-addin outlook-2010

我正在尝试使用 Folder.SetCustomIcon() 方法将自定义图标放置在我以编程方式创建的已保存搜索文件夹中。 SetCustomIcon() 文档非常稀少,但可以是 found here for reference .

另外,object it expects is here同样,这些例子非常稀少。

有人知道如何为文件夹设置自定义图标吗?以下是我到目前为止的代码:

searchFolders = inboxFolder.Store.GetSearchFolders();
foreach (Outlook.Folder folder in searchFolders)
{
    if (folder.Name == "Expiring Retention Policy Mail")
    {
        folder.ShowItemCount = Microsoft.Office.Interop.Outlook.OlShowItemCount.olShowTotalItemCount;
        folder.SetCustomIcon(new Bitmap(32, 32));   // <=-- this isn't working because it's expecting stdPicture which has very sparse information on how to convert to this type.
        Globals.ThisAddIn.Application.ActiveExplorer().CurrentFolder = folder;  
    }
}

最佳答案

您只需要使用 PictureDispConverter 将图像/图标转换为 IPictureDisp。下面是一个example from MSDN .这仅适用于 Outlook 2010+。要在 Outlook 2013 中查看自定义文件夹图标,您需要 view the Folder List - not the Mail view .

从 MAPIFolder 设置自定义图标

private static void SetCustomIcon(Outlook.MAPIFolder folder)
{
    Icon icon = null;
    try
    {
        icon = Properties.Resources.myCustomIcon_16x16;
        stdole.StdPicture iconPictureDisp = PictureDispConverter.ToIPictureDisp(icon) as stdole.StdPicture;
        folder.SetCustomIcon(iconPictureDisp);
    }
    finally
    {
        icon.Dispose();
    }
}

PictureDispConverter(图标->IPictureDisp,图像->IPictureDisp)

public static class PictureDispConverter
{
    //IPictureDisp GUID. 
    public static Guid iPictureDispGuid = typeof(stdole.IPictureDisp).GUID;

    // Converts an Icon into an IPictureDisp. 
    public static stdole.IPictureDisp ToIPictureDisp(Icon icon)
    {
        PICTDESC.Icon pictIcon = new PICTDESC.Icon(icon);
        return PictureDispConverter.OleCreatePictureIndirect(pictIcon, ref iPictureDispGuid, true);
    }

    // Converts an image into an IPictureDisp. 
    public static stdole.IPictureDisp ToIPictureDisp(Image image)
    {
        Bitmap bitmap = (image is Bitmap) ? (Bitmap)image : new Bitmap(image);
        PICTDESC.Bitmap pictBit = new PICTDESC.Bitmap(bitmap);
        return PictureDispConverter.OleCreatePictureIndirect(pictBit, ref iPictureDispGuid, true);
    }


    [DllImport("OleAut32.dll", EntryPoint = "OleCreatePictureIndirect", ExactSpelling = true,
    PreserveSig = false)]
    private static extern stdole.IPictureDisp OleCreatePictureIndirect(
    [MarshalAs(UnmanagedType.AsAny)] object picdesc, ref Guid iid, bool fOwn);

    private readonly static HandleCollector handleCollector =
    new HandleCollector("Icon handles", 1000);

    // WINFORMS COMMENT: 
    // PICTDESC is a union in native, so we'll just 
    // define different ones for the different types 
    // the "unused" fields are there to make it the right 
    // size, since the struct in native is as big as the biggest 
    // union. 
    private static class PICTDESC
    {
        //Picture Types 
        public const short PICTYPE_UNINITIALIZED = -1;
        public const short PICTYPE_NONE = 0;
        public const short PICTYPE_BITMAP = 1;
        public const short PICTYPE_METAFILE = 2;
        public const short PICTYPE_ICON = 3;
        public const short PICTYPE_ENHMETAFILE = 4;

        [StructLayout(LayoutKind.Sequential)]
        public class Icon
        {
            internal int cbSizeOfStruct = Marshal.SizeOf(typeof(PICTDESC.Icon));
            internal int picType = PICTDESC.PICTYPE_ICON;
            internal IntPtr hicon = IntPtr.Zero;
            internal int unused1 = 0;
            internal int unused2 = 0;

            internal Icon(System.Drawing.Icon icon)
            {
                this.hicon = icon.ToBitmap().GetHicon();
            }
        }

        [StructLayout(LayoutKind.Sequential)]
        public class Bitmap
        {
            internal int cbSizeOfStruct = Marshal.SizeOf(typeof(PICTDESC.Bitmap));
            internal int picType = PICTDESC.PICTYPE_BITMAP;
            internal IntPtr hbitmap = IntPtr.Zero;
            internal IntPtr hpal = IntPtr.Zero;
            internal int unused = 0;
            internal Bitmap(System.Drawing.Bitmap bitmap)
            {
                this.hbitmap = bitmap.GetHbitmap();
            }
        }
    }
} 

关于c# - 如何以编程方式在 Outlook 搜索文件夹上设置自定义图标? (即 Outlook 文件夹 SetCustomIcon),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16114566/

相关文章:

python - Outlook - 搜索来自特定发件人的附件

vba - Outlook 回复或回复全部电子邮件

vsto - 如何让我的应用程序读取应用程序配置而不是 machine.config?

Excel范围样式: specifying Borders via VSTO doesn't work

c# - String.IndexOf 的 IList<T> 版本(找到一个子 -'string' ,而不仅仅是单个对象)

c# - 以编程方式比较两种方法的 IL

python - 使用 win32com 设置属性

deployment - 是否可以在没有 Office 2007 的情况下部署 Office 2007 PIA?

c# - 如何快速创建此类子域,例如 Blogger.com

c# - 通过 linq 复制行