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

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

问题描述

我试图使用 Folder.SetCustomIcon()方法来放在我编程方式创建一个保存搜索文件夹的自定义图标。在 SetCustomIcon()文档是很稀疏,但可以的这里找到适合的参考



另外,对象,预计是这里又一次的例子非常稀少。



请问有人知道如何设置自定义图标文件夹?下面的代码是我迄今:

  searchFolders = inboxFolder.Store.GetSearchFolders(); 
的foreach(在searchFolders Outlook.Folder文件夹)
{
如果(folder.Name ==即将到期保留策略邮件)
{
folder.ShowItemCount =微软.Office.Interop.Outlook.OlShowItemCount.olShowTotalItemCount;
folder.SetCustomIcon(新位图(32,32)); //< = - 这是行不通的,因为它预期stdPicture这对如何转换这种类型很稀疏的信息。
Globals.ThisAddIn.Application.ActiveExplorer()CurrentFolder =文件夹。
}
}


解决方案

您只需要使用一个 PictureDispConverter 从图像/图标 IPictureDisp 去。下面是从MSDN 的例子。在展望2010 +这只能。要查看自定义的文件夹图标中的 Outlook 2013中,您需要的查看文件夹列表 - 不是邮件视图



从MAPIFolder SetCustomIcon



<预类=郎-CS prettyprint-覆盖> 私有静态无效SetCustomIcon(Outlook.MAPIFolder文件夹)
{
图标图标= NULL;

{
图标= Properties.Resources.myCustomIcon_16x16;
stdole.StdPicture iconPictureDisp = PictureDispConverter.ToIPictureDisp(图标)作为stdole.StdPicture;
folder.SetCustomIcon(iconPictureDisp);
}
终于
{
icon.Dispose();
}
}



PictureDispConverter( Icon-> IPictureDisp,图像 - > IPictureDisp 的)



<预类=郎-CS prettyprint-覆盖> 公共静态类PictureDispConverter
{
// IPictureDisp GUID。
公共静态的Guid = iPictureDispGuid typeof运算(stdole.IPictureDisp).GUID;

//一个图标到IPictureDisp转换。
公共静态stdole.IPictureDisp ToIPictureDisp(ICON图标)
{
PICTDESC.Icon pictIcon =新PICTDESC.Icon(图标);
返回PictureDispConverter.OleCreatePictureIndirect(pictIcon,楼盘iPictureDispGuid,真正的);
}

//将图像转换成IPictureDisp。
公共静态stdole.IPictureDisp ToIPictureDisp(形象画像)
{
位图位图=(形象是位图)? (位图)图像:新位图(图片);
PICTDESC.Bitmap pictBit =新PICTDESC.Bitmap(位图);
返回PictureDispConverter.OleCreatePictureIndirect(pictBit,楼盘iPictureDispGuid,真正的);
}


函数[DllImport(OLEAUT32.DLL,入口点=OleCreatePictureIndirect,ExactSpelling = TRUE,
PreserveSig = FALSE)]
私人静态外部stdole.IPictureDisp OleCreatePictureIndirect(
[的MarshalAs(UnmanagedType.AsAny)对象picdesc,裁判的Guid IID,布尔fOwn);

私人只读静态HandleCollector handleCollector =
新HandleCollector(图标把手,1000);

//的WinForms注释:
// PICTDESC处于纯工会,所以我们只
//为不同类型的
定义不同的人//在未使用字段存在,使其正确的
//的大小,因为在本地的结构是一样大的最大
//工会。
私有静态类PICTDESC
{
//图像类型
公共常量短PICTYPE_UNINITIALIZED = -1;
公共常量短PICTYPE_NONE = 0;
公共常量短PICTYPE_BITMAP = 1;
公共常量短PICTYPE_METAFILE = 2;
公共常量短PICTYPE_ICON = 3;
公共常量短PICTYPE_ENHMETAFILE = 4;

[StructLayout(LayoutKind.Sequential)]
公共类图标
{
内部INT cbSizeOfStruct = Marshal.SizeOf(typeof运算(PICTDESC.Icon));
内部INT picType = PICTDESC.PICTYPE_ICON;
内部的IntPtr惠康= IntPtr.Zero;
内部INT unused1 = 0;
内部INT unused2 = 0;

内部图标(图标System.Drawing.Icon)
{
this.hicon = icon.ToBitmap()GetHicon()。
}
}

[StructLayout(LayoutKind.Sequential)]
公共类的位图
{
内部INT cbSizeOfStruct = Marshal.SizeOf( typeof运算(PICTDESC.Bitmap));
内部INT picType = PICTDESC.PICTYPE_BITMAP;
内部的IntPtr HBITMAP = IntPtr.Zero;
内部IntPtr的HPAL = IntPtr.Zero;
内部INT未使用= 0;
内部位图(System.Drawing.Bitmap位图)
{
this.hbitmap = bitmap.GetHbitmap();
}
}
}
}


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

解决方案

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.

SetCustomIcon from 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 (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天全站免登陆