LableRibbon.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. using Microsoft.Office.Core;
  2. using Microsoft.Office.Interop.Word;
  3. using Microsoft.Office.Tools.Ribbon;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Reflection;
  9. using System.Runtime.InteropServices;
  10. using System.Text;
  11. using System.Windows.Forms;
  12. using Office = Microsoft.Office.Core;
  13. // TODO: 按照以下步骤启用功能区(XML)项:
  14. // 1. 将以下代码块复制到 ThisAddin、ThisWorkbook 或 ThisDocument 类中。
  15. // protected override Microsoft.Office.Core.IRibbonExtensibility CreateRibbonExtensibilityObject()
  16. // {
  17. // return new LableRibbon();
  18. // }
  19. // 2. 在此类的“功能区回调”区域中创建回调方法,以处理用户
  20. // 操作(如单击某个按钮)。注意: 如果已经从功能区设计器中导出此功能区,
  21. // 则将事件处理程序中的代码移动到回调方法并修改该代码以用于
  22. // 功能区扩展性(RibbonX)编程模型。
  23. // 3. 向功能区 XML 文件中的控制标记分配特性,以标识代码中的相应回调方法。
  24. // 有关详细信息,请参见 Visual Studio Tools for Office 帮助中的功能区 XML 文档。
  25. namespace HTEXLabel
  26. {
  27. [ComVisible(true)]
  28. public class LableRibbon : Office.IRibbonExtensibility
  29. {
  30. private Office.IRibbonUI ribbon;
  31. public LableRibbon()
  32. {
  33. }
  34. #region IRibbonExtensibility 成员
  35. public string GetCustomUI(string ribbonID)
  36. {
  37. // 获取 Office 应用程序的语言设置
  38. LanguageSettings languageSettings = Globals.ThisAddIn.Application.LanguageSettings;
  39. // 获取主语言 ID
  40. int languageId = languageSettings.get_LanguageID(MsoAppLanguageID.msoLanguageIDUI);
  41. // 判断语言版本
  42. switch (languageId)
  43. {
  44. case 2052:
  45. // 2052 是简体中文的语言 ID
  46. //System.Windows.Forms.MessageBox.Show("Office 语言版本:简体中文");
  47. return GetResourceText("HTEXLabel.LableRibbon-cn.xml");
  48. case 1028:
  49. // 1028 是繁体中文的语言 ID
  50. // System.Windows.Forms.MessageBox.Show("Office 语言版本:繁体中文");
  51. return GetResourceText("HTEXLabel.LableRibbon-tw.xml");
  52. case 1033:
  53. // 1033 是英语的语言 ID
  54. // System.Windows.Forms.MessageBox.Show("Office 语言版本:英语");
  55. return GetResourceText("HTEXLabel.LableRibbon-en.xml");
  56. default:
  57. // System.Windows.Forms.MessageBox.Show("未知的 Office 语言版本:" + languageId.ToString());
  58. return GetResourceText("HTEXLabel.LableRibbon-en.xml");
  59. }
  60. }
  61. #endregion
  62. #region 功能区回调
  63. //在此处创建回叫方法。有关添加回叫方法的详细信息,请访问 https://go.microsoft.com/fwlink/?LinkID=271226
  64. public void Ribbon_Load(IRibbonUI ribbonUI)
  65. {
  66. this.ribbon = ribbonUI;
  67. }
  68. #endregion
  69. #region 帮助器
  70. private static string GetResourceText(string resourceName)
  71. {
  72. Assembly asm = Assembly.GetExecutingAssembly();
  73. string[] resourceNames = asm.GetManifestResourceNames();
  74. for (int i = 0; i < resourceNames.Length; ++i)
  75. {
  76. if (string.Compare(resourceName, resourceNames[i], StringComparison.OrdinalIgnoreCase) == 0)
  77. {
  78. using (StreamReader resourceReader = new StreamReader(asm.GetManifestResourceStream(resourceNames[i])))
  79. {
  80. if (resourceReader != null)
  81. {
  82. return resourceReader.ReadToEnd();
  83. }
  84. }
  85. }
  86. }
  87. return null;
  88. }
  89. public void OnButtonCheck(Office.IRibbonControl control)
  90. {
  91. Document currentDocument = Globals.ThisAddIn.Application.ActiveDocument;
  92. var a = currentDocument.XMLNodes;
  93. var ass = a.ToString();
  94. // 创建一个新的 Open XML 文档
  95. }
  96. public void OnButtonClick(Office.IRibbonControl control)
  97. {
  98. // 在这里根据按钮的标识执行相应的操作
  99. string buttonId = control.Id;
  100. string buttonTag = control.Tag;
  101. // 获取当前文档的光标位置
  102. Microsoft.Office.Interop.Word.Selection selection = Globals.ThisAddIn.Application.Selection;
  103. // 在光标位置插入内容
  104. if (buttonId.Equals("compose"))
  105. {
  106. // 获取 Office 应用程序的语言设置
  107. LanguageSettings languageSettings = Globals.ThisAddIn.Application.LanguageSettings;
  108. // 获取主语言 ID
  109. int languageId = languageSettings.get_LanguageID(MsoAppLanguageID.msoLanguageIDUI);
  110. // 判断语言版本
  111. switch (languageId)
  112. {
  113. case 2052:
  114. // 2052 是简体中文的语言 ID
  115. selection.TypeText("{" + buttonTag + "}{结束}"); break;
  116. case 1028:
  117. // 1028 是繁体中文的语言 ID
  118. selection.TypeText("{" + buttonTag + "}{結束}"); break;
  119. case 1033:
  120. // 1033 是英语的语言 ID
  121. selection.TypeText("{" + buttonTag + "}{Ended}"); break;
  122. default:
  123. // System.Windows.Forms.MessageBox.Show("未知的 Office 语言版本:" + languageId.ToString());
  124. selection.TypeText("{" + buttonTag + "}");
  125. break;
  126. }
  127. }
  128. else
  129. {
  130. selection.TypeText("{" + buttonTag + "}");
  131. }
  132. }
  133. public void ShowQRCodeForm(IRibbonControl control)
  134. {
  135. // 创建 Windows Forms 窗口
  136. QRCodeForm qrCodeForm = new QRCodeForm(control)
  137. {
  138. MaximizeBox = false,
  139. MinimizeBox = false,
  140. // 设置窗体的边框样式
  141. FormBorderStyle = FormBorderStyle.FixedSingle,
  142. Width=215,
  143. Height=240,
  144. // 显示窗口
  145. };
  146. CenterFormOnScreen(qrCodeForm);
  147. qrCodeForm.ShowDialog();
  148. }
  149. public void CenterFormOnScreen(QRCodeForm qrCodeForm)
  150. {
  151. // 获取屏幕的工作区域
  152. var workingArea = System.Windows.Forms.Screen.GetWorkingArea(qrCodeForm);
  153. // 计算窗体应该显示的位置
  154. int x = (workingArea.Width - qrCodeForm.Width) / 2 + workingArea.Left;
  155. int y = (workingArea.Height - qrCodeForm.Height) / 2 + workingArea.Top;
  156. // 设置窗体位置
  157. qrCodeForm.Location = new System.Drawing.Point(x,y);
  158. }
  159. #endregion
  160. }
  161. }