c# - How to programmatically set a custom icon on an Outlook search folder? (i.e. Outlook Folder SetCustomIcon) -
i attempting use folder.setcustomicon()
method place custom icon on saved search folder programmatically created. setcustomicon()
documentation sparse can found here reference.
also, object expects here , again examples sparse.
would know how set custom icon folder? following code have 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)); // <=-- isn't working because it's expecting stdpicture has sparse information on how convert type. globals.thisaddin.application.activeexplorer().currentfolder = folder; } }
you need use picturedispconverter
go image/icon ipicturedisp
. below example msdn. works in outlook 2010+. view custom folder icons in outlook 2013, need view folder list - not mail view.
setcustomicon mapifolder
private static void setcustomicon(outlook.mapifolder folder) { icon icon = null; try { icon = properties.resources.mycustomicon_16x16; stdole.stdpicture iconpicturedisp = picturedispconverter.toipicturedisp(icon) stdole.stdpicture; folder.setcustomicon(iconpicturedisp); } { icon.dispose(); } }
picturedispconverter (icon->ipicturedisp, image->ipicturedisp)
public static class picturedispconverter { //ipicturedisp guid. public static guid ipicturedispguid = typeof(stdole.ipicturedisp).guid; // converts icon ipicturedisp. public static stdole.ipicturedisp toipicturedisp(icon icon) { pictdesc.icon picticon = new pictdesc.icon(icon); return picturedispconverter.olecreatepictureindirect(picticon, ref ipicturedispguid, true); } // converts image ipicturedisp. public static stdole.ipicturedisp toipicturedisp(image image) { bitmap bitmap = (image 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 union in native, we'll // define different ones different types // "unused" fields there make right // size, since struct in native big 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(); } } } }
Comments
Post a Comment