如何以编程方式在 Outlook 搜索文件夹上设置自定义图标?(即 Outlook 文件夹 SetCustomIcon) [英] How to programmatically set a custom icon on an Outlook search folder? (i.e. Outlook Folder SetCustomIcon)

查看:42
本文介绍了如何以编程方式在 Outlook 搜索文件夹上设置自定义图标?(即 Outlook 文件夹 SetCustomIcon)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Folder.SetCustomIcon() 方法在我以编程方式创建的已保存搜索文件夹上放置自定义图标.SetCustomIcon() 文档非常稀疏,但可以是 在这里找到以供参考.

I am attempting to use the Folder.SetCustomIcon() method to place a custom icon on a saved search folder I programmatically created. The SetCustomIcon() documentation is very sparse but can be found here for reference.

此外,它期望的 对象在这里 而且这些例子非常稀疏.

Also, the object it expects is here and again the examples are very sparse.

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

Would anybody know how to set the custom icon for a folder? The following is the code I have thus far:

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.下面是一个 来自 MSDN 的示例.这仅适用于 Outlook 2010+.要在 Outlook 2013 中查看自定义文件夹图标,您需要查看文件夹列表 - 而不是邮件视图.

You just need to use a PictureDispConverter to go from an image/icon to IPictureDisp. Below is an example from MSDN. This only works in Outlook 2010+. To view custom Folder Icons in Outlook 2013, you need to view the Folder List - not the Mail view.

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 (Icon->IPictureDisp, Image->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();
            }
        }
    }
} 

这篇关于如何以编程方式在 Outlook 搜索文件夹上设置自定义图标?(即 Outlook 文件夹 SetCustomIcon)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆