Folder.SetCustomIcon 方法 (Outlook)

为文件夹设置由 Picture 指定的自定义图标。

语法

表达式SetCustomIcon (图片)

表达 一个代表 Folder 对象的变量。

参数

名称 必需/可选 数据类型 说明
Picture 必需 IPictureDisp 为文件夹指定自定义图标。

备注

Picture 指定的 IPictureDisp 对象必须具有等于 PICTYPE_ICONPICTYPE_BITMAPType 属性。 图标或位图资源可以具有的最大大小为 32 x 32。 图标 16x16 或 24x24 也支持,和Microsoft Outlook可以扩大 16 x 16 图标,如果在高点每英寸 (DPI) 模式下运行 Outlook。 其他大小的图标会导致 SetCustomIcon 返回一个错误。

可以为搜索文件夹以及所有不表示默认文件夹或特殊文件夹的文件夹设置自定义图标。 如果您尝试为属于下列组的文件夹的某个文件夹设置自定义图标,则 SetCustomIcon 将返回一个错误:

  • 默认文件夹(如 OlDefaultFolders 枚举所列)
  • 特殊文件夹(如 OlSpecialFolders 枚举所列)
  • Exchange 公共文件夹
  • Exchange 邮箱的根文件夹
  • 隐藏文件夹

您只能从代码,运行在进程内为 Outlook 调用 SetCustomIconIPictureDisp 对象不能跨进程边界封送。 如果您尝试从进程外的代码中调用 SetCustomIcon 时,将发生异常。

此方法提供的自定义文件夹图标不会在运行中 Outlook 会话外持久保留。 因此,加载项必须在每次 Outlook 启动时设置自定义文件夹图标。

自定义文件夹图标不会出现在其他 Exchange 客户端中(如 Outlook Web Access),也不会出现在 Windows Mobile 设备上运行的 Outlook 中。

示例

下面的托管代码是使用 C# 编写的。 若要运行需要调入组件对象模型 (COM) 的 .NET Framework 托管代码示例,您必须使用可定义托管接口并将其映射到对象模型类型库中的 COM 对象的互操作程序集。 对于 Outlook,您可以使用 Visual Studio 和 Outlook 主互操作程序集 (PIA)。 在您运行适用于 Outlook 2013 的托管代码示例之前,请确保您已安装了 Outlook 2013 PIA 并且已添加了对 Visual Studio 中的 Microsoft Outlook 15.0 对象库组件的引用。 应使用 Office Developer Tools for Visual Studio) 在 Outlook 外接程序 (类中使用以下代码 ThisAddIn 。 代码中的 应用程序对象必须是由 提供的受信任 Outlook ThisAddIn.Globals对象。 有关使用 Outlook PIA 开发托管 Outlook 解决方案的详细信息,请参阅欢迎使用 MSDN 上的 Outlook 主互操作程序集参考

下面的 C# 代码片段设置出现在 "解决方案" 模块中的文件夹的图标。 这段代码所依赖的 PictureDispConverter 类也在下面演示。

//Get the icons for the solution 
stdole.StdPicture rootPict = 
 PictureDispConverter.ToIPictureDisp( 
 Properties.Resources.BRIDGE) 
 as stdole.StdPicture; 
stdole.StdPicture calPict = 
 PictureDispConverter.ToIPictureDisp( 
 Properties.Resources.umbrella) 
 as stdole.StdPicture; 
stdole.StdPicture contactsPict = 
 PictureDispConverter.ToIPictureDisp( 
 Properties.Resources.group) 
 as stdole.StdPicture; 
stdole.StdPicture tasksPict = 
 PictureDispConverter.ToIPictureDisp( 
 Properties.Resources.SUN) 
 as stdole.StdPicture; 
 
//Set the icons for solution folders 
solutionRoot.SetCustomIcon(rootPict); 
solutionCalendar.SetCustomIcon(calPict); 
solutionContacts.SetCustomIcon(contactsPict); 
solutionTasks.SetCustomIcon(tasksPict);

下面是 类的定义 PictureDispConverter

using System; 
using System.Drawing; 
using System.Collections.Generic; 
using System.Runtime.InteropServices; 
 
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(); 
 } 
 } 
 } 
} 

支持和反馈

有关于 Office VBA 或本文档的疑问或反馈? 请参阅 Office VBA 支持和反馈,获取有关如何接收支持和提供反馈的指南。