■ MAPIFolder 인터페이스를 사용해 폴더 아이콘을 설정하는 방법을 보여준다.
▶ PictureConverter.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 |
using System; using System.Drawing; using System.Runtime.InteropServices; namespace TestProject { /// <summary> /// PICTURE 변환자 /// </summary> public static class PictureConverter { //////////////////////////////////////////////////////////////////////////////////////////////////// Class ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Private #region 그림 설명 - PICTDESC /// <summary> /// 그림 설명 /// </summary> private static class PICTDESC { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Public #region Field /// <summary> /// PICTYPE_UNINITIALIZED /// </summary> public const short PICTYPE_UNINITIALIZED = -1; /// <summary> /// PICTYPE_NONE /// </summary> public const short PICTYPE_NONE = 0; /// <summary> /// PICTYPE_BITMAP /// </summary> public const short PICTYPE_BITMAP = 1; /// <summary> /// PICTYPE_METAFILE /// </summary> public const short PICTYPE_METAFILE = 2; /// <summary> /// PICTYPE_ICON /// </summary> public const short PICTYPE_ICON = 3; /// <summary> /// PICTYPE_ENHMETAFILE /// </summary> public const short PICTYPE_ENHMETAFILE = 4; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Class ////////////////////////////////////////////////////////////////////////////////////////// Public #region 아이콘 - Icno /// <summary> /// 아이콘 /// </summary> [StructLayout(LayoutKind.Sequential)] public class Icon { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Public #region Field /// <summary> /// 크기 /// </summary> public int Size = Marshal.SizeOf(typeof(PICTDESC.Icon)); /// <summary> /// 그림 타입 /// </summary> public int PictureType = PICTDESC.PICTYPE_ICON; /// <summary> /// 아이콘 핸들 /// </summary> public IntPtr IconHandle = IntPtr.Zero; /// <summary> /// 미사용 1 /// </summary> public int Unused1 = 0; /// <summary> /// 미사용 2 /// </summary> public int Unused2 = 0; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - Icon(icon) /// <summary> /// 생성자 /// </summary> /// <param name="icon">아이콘</param> public Icon(System.Drawing.Icon icon) { IconHandle = icon.ToBitmap().GetHicon(); } #endregion } #endregion #region 비트맵 - Bitmap /// <summary> /// 비트맵 /// </summary> [StructLayout(LayoutKind.Sequential)] public class Bitmap { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Public #region Field /// <summary> /// 크기 /// </summary> public int Size = Marshal.SizeOf(typeof(PICTDESC.Bitmap)); /// <summary> /// 그림 타입 /// </summary> public int PictureType = PICTDESC.PICTYPE_BITMAP; /// <summary> /// 비트맵 핸들 /// </summary> public IntPtr BitmapHandle = IntPtr.Zero; /// <summary> /// 팔레트 핸들 /// </summary> public IntPtr PaletteHandle = IntPtr.Zero; /// <summary> /// 미사용 /// </summary> public int Unused = 0; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - Bitmap(bitmap) /// <summary> /// 생성자 /// </summary> public Bitmap(System.Drawing.Bitmap bitmap) { BitmapHandle = bitmap.GetHbitmap(); } #endregion } #endregion } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Import ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Private #region OLE 그림 생성하기 (간접) - OleCreatePictureIndirect(pictureDescription, interfaceID, own); /// <summary> /// OLE 그림 생성하기 (간접) /// </summary> /// <param name="pictureDescription">그림 설명</param> /// <param name="interfaceID">인터페이스 ID</param> /// <param name="own">소유 여부</param> /// <returns>IPictureDisp 인터페이스 객체</returns> [DllImport("OleAut32.dll", EntryPoint = "OleCreatePictureIndirect", ExactSpelling = true, PreserveSig = false)] private static extern stdole.IPictureDisp OleCreatePictureIndirect([MarshalAs(UnmanagedType.AsAny)] object pictureDescription, ref Guid interfaceID, bool own); #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// IPictureDisp 인터페이스 GUID /// </summary> private static Guid _iPictureDispGUID = typeof(stdole.IPictureDisp).GUID; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Public #region IPictureDisp 인터페이스 객체 구하기 - ToPicture(icon) /// <summary> /// IPictureDisp 인터페이스 객체 구하기 /// </summary> /// <param name="icon">아이콘</param> /// <returns>IPictureDisp 인터페이스 객체</returns> public static stdole.IPictureDisp ToPicture(Icon icon) { PICTDESC.Icon pictDescIcon = new PICTDESC.Icon(icon); return OleCreatePictureIndirect(pictDescIcon, ref _iPictureDispGUID, true); } #endregion #region IPictureDisp 인터페이스 객체 구하기 - ToPicture(image) /// <summary> /// IPictureDisp 인터페이스 객체 구하기 /// </summary> /// <param name="image">이미지</param> /// <returns>IPictureDisp 인터페이스 객체</returns> public static stdole.IPictureDisp ToPicture(Image image) { Bitmap bitmap = (image is Bitmap) ? (Bitmap)image : new Bitmap(image); PICTDESC.Bitmap pictDescBitmap = new PICTDESC.Bitmap(bitmap); return OleCreatePictureIndirect(pictDescBitmap, ref _iPictureDispGUID, true); } #endregion } } |
▶ CustomAddIn.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
using Microsoft.Office.Interop.Outlook; using System; using System.Drawing; namespace TestProject { /// <summary> /// 커스텀 애드인 /// </summary> public partial class CustomAddIn { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 애플리케이션 /// </summary> private Application application = null; /// <summary> /// 타겟 폴더 /// </summary> private Folder targetFolder = null; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private //////////////////////////////////////////////////////////////////////////////// Event #region 커스텀 애드인 시작시 처리하기 - CustomAddIn_Startup(sender, e) /// <summary> /// 커스텀 애드인 시작시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void CustomAddIn_Startup(object sender, EventArgs e) { this.application = Application; // Outlook에서 계정 폴더 아래에 Parent\Child 폴더가 사전에 생성되어 있어야 한다. Folder parentFolder = this.application.ActiveExplorer().Session.Folders[1].Folders["Parent"] as Folder; this.targetFolder = parentFolder.Folders["Child"] as Folder; SetCustomIcon(this.targetFolder); } #endregion #region 커스텀 애드인 셧다운시 처리하기 - CustomAddIn_Shutdown(sender, e) /// <summary> /// 커스텀 애드인 셧다운시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void CustomAddIn_Shutdown(object sender, EventArgs e) { } #endregion //////////////////////////////////////////////////////////////////////////////// Function #region VSTO에서 생성한 코드 /// <summary> /// 디자이너 지원에 필요한 메서드입니다. /// 이 메서드의 내용을 코드 편집기로 수정하지 마세요. /// </summary> private void InternalStartup() { this.Startup += new System.EventHandler(CustomAddIn_Startup ); this.Shutdown += new System.EventHandler(CustomAddIn_Shutdown); } #endregion #region 커스텀 아이콘 설정하기 - SetCustomIcon(folder) /// <summary> /// 커스텀 아이콘 설정하기 /// </summary> /// <param name="folder">폴더</param> private void SetCustomIcon(MAPIFolder folder) { Bitmap bitmap = null; try { bitmap = Properties.Resources.sample; stdole.StdPicture picture = PictureConverter.ToPicture(bitmap) as stdole.StdPicture; folder.SetCustomIcon(picture); } finally { bitmap.Dispose(); } } #endregion } } |