■ 메일 컨텍스트 메뉴에서 커스텀 컨텍스트 메뉴를 추가하는 방법을 보여준다.
▶ RibbonContextMenu.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
<?xml version="1.0" encoding="UTF-8"?> <customUI xmlns="http://schemas.microsoft.com/office/2009/07/customui" onLoad="customUI_Load"> <ribbon> </ribbon> <contextMenus> <contextMenu idMso="ContextMenuMailItem"> <button id="customContextMenu1" label="커스텀 컨텍스트 메뉴 1" getVisible="customContextMenu_GetVisible" onAction="customContextMenu1_Click"/> </contextMenu> <contextMenu idMso="ContextMenuMultipleItems"> <button id="customContextMenu2" label="커스텀 컨텍스트 메뉴 2" getVisible="customContextMenu_GetVisible" onAction="customContextMenu2_Click"/> </contextMenu> </contextMenus> </customUI> |
▶ RibbonContextMenu.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 |
using Microsoft.Office.Core; using Microsoft.Office.Interop.Outlook; using System; using System.IO; using System.Reflection; using System.Runtime.InteropServices; namespace TestProject { /// <summary> /// 리본 컨텍스트 메뉴 /// </summary> [ComVisible(true)] public class RibbonContextMenu : IRibbonExtensibility { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 리본 UI /// </summary> private IRibbonUI ribbonUI; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - RibbonContextMenu() /// <summary> /// 생성자 /// </summary> public RibbonContextMenu() { } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Public #region (IRibbonExtensibility) 커스텀 UI 구하기 - GetCustomUI(ribbonID) /// <summary> /// 커스텀 UI 구하기 /// </summary> /// <param name="ribbonID">리본 ID</param> /// <returns>커스텀 UI</returns> public string GetCustomUI(string ribbonID) { return GetResourceText("TestProject.RibbonContextMenu.xml"); } #endregion #region 커스텀 UI 로드시 처리하기 - customUI_Load(ribbonUI) /// <summary> /// 커스텀 UI 로드시 처리하기 /// </summary> /// <param name="ribbonUI">리본 UI 인터페이스 객체</param> public void customUI_Load(IRibbonUI ribbonUI) { this.ribbonUI = ribbonUI; } #endregion #region 커스텀 컨텍스트 메뉴 표시 여부 구하기 - customContextMenu_GetVisible(ribbonControl) /// <summary> /// 커스텀 컨텍스트 메뉴 표시 여부 구하기 /// </summary> /// <param name="ribbonControl">리본 컨트롤</param> /// <returns>처리 결과</returns> public bool customContextMenu_GetVisible(IRibbonControl ribbonControl) { string id = ribbonControl.Id; if(id == "customContextMenu1" || id == "customContextMenu2") { dynamic items = ribbonControl.Context; foreach(dynamic item in items) { if(item is MailItem mailItem && mailItem.Parent is Folder folder && folder.FullFolderPath.Contains("받은 편지함")) { return true; } break; } } return false; } #endregion #region 커스텀 컨텍스트 메뉴 1 클릭시 처리하기 - customContextMenu1Click(ribbonControl) /// <summary> /// 커스텀 컨텍스트 메뉴 1 클릭시 처리하기 /// </summary> /// <param name="ribbonControl">리본 컨트롤</param> public void customContextMenu1_Click(IRibbonControl ribbonControl) { if(ribbonControl.Id == "customContextMenu1") { System.Windows.Forms.MessageBox.Show("커스텀 컨텍스트 메뉴 1을 클릭했습니다."); } } #endregion #region 커스텀 컨텍스트 메뉴 2 클릭시 처리하기 - customContextMenu2_Click(ribbonControl) /// <summary> /// 커스텀 컨텍스트 메뉴 2 클릭시 처리하기 /// </summary> /// <param name="ribbonControl">리본 컨트롤</param> public void customContextMenu2_Click(IRibbonControl ribbonControl) { if(ribbonControl.Id == "customContextMenu2") { System.Windows.Forms.MessageBox.Show("커스텀 컨텍스트 메뉴 2를 클릭했습니다."); } } #endregion ////////////////////////////////////////////////////////////////////////////////////////// Private #region 리소스 텍스트 구하기 - GetResourceText(resourceName) /// <summary> /// 리소스 텍스트 구하기 /// </summary> /// <param name="resourceName">리소스명</param> /// <returns>리소스 텍스트</returns> private static string GetResourceText(string resourceName) { Assembly assembly = Assembly.GetExecutingAssembly(); string[] resourceNameArray = assembly.GetManifestResourceNames(); for(int i = 0; i < resourceNameArray.Length; ++i) { if(string.Compare(resourceName, resourceNameArray[i], StringComparison.OrdinalIgnoreCase) == 0) { using(StreamReader resourceReader = new StreamReader(assembly.GetManifestResourceStream(resourceNameArray[i]))) { if(resourceReader != null) { return resourceReader.ReadToEnd(); } } } } return null; } #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 |
using Microsoft.Office.Core; using System; namespace TestProject { /// <summary> /// 커스텀 애드인 /// </summary> public partial class CustomAddIn { //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Protected //////////////////////////////////////////////////////////////////////////////// Function #region 리본 확장성 객체 생성하기 - CreateRibbonExtensibilityObject() /// <summary> /// 리본 확장성 객체 생성하기 /// </summary> /// <returns>리본 확장성 인터페이스 객체</returns> protected override IRibbonExtensibility CreateRibbonExtensibilityObject() { return new RibbonContextMenu(); } #endregion ////////////////////////////////////////////////////////////////////////////////////////// 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) { } #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 } } |