using Microsoft.Office.Core; using Microsoft.Office.Interop.Word; using Microsoft.Office.Tools.Ribbon; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Reflection; using System.Runtime.InteropServices; using System.Text; using System.Windows.Forms; using Office = Microsoft.Office.Core; // TODO: 按照以下步骤启用功能区(XML)项: // 1. 将以下代码块复制到 ThisAddin、ThisWorkbook 或 ThisDocument 类中。 // protected override Microsoft.Office.Core.IRibbonExtensibility CreateRibbonExtensibilityObject() // { // return new LableRibbon(); // } // 2. 在此类的“功能区回调”区域中创建回调方法,以处理用户 // 操作(如单击某个按钮)。注意: 如果已经从功能区设计器中导出此功能区, // 则将事件处理程序中的代码移动到回调方法并修改该代码以用于 // 功能区扩展性(RibbonX)编程模型。 // 3. 向功能区 XML 文件中的控制标记分配特性,以标识代码中的相应回调方法。 // 有关详细信息,请参见 Visual Studio Tools for Office 帮助中的功能区 XML 文档。 namespace HTEXLabel { [ComVisible(true)] public class LableRibbon : Office.IRibbonExtensibility { private Office.IRibbonUI ribbon; public LableRibbon() { } #region IRibbonExtensibility 成员 public string GetCustomUI(string ribbonID) { // 获取 Office 应用程序的语言设置 LanguageSettings languageSettings = Globals.ThisAddIn.Application.LanguageSettings; // 获取主语言 ID int languageId = languageSettings.get_LanguageID(MsoAppLanguageID.msoLanguageIDUI); // 判断语言版本 switch (languageId) { case 2052: // 2052 是简体中文的语言 ID //System.Windows.Forms.MessageBox.Show("Office 语言版本:简体中文"); return GetResourceText("HTEXLabel.LableRibbon-cn.xml"); case 1028: // 1028 是繁体中文的语言 ID // System.Windows.Forms.MessageBox.Show("Office 语言版本:繁体中文"); return GetResourceText("HTEXLabel.LableRibbon-tw.xml"); case 1033: // 1033 是英语的语言 ID // System.Windows.Forms.MessageBox.Show("Office 语言版本:英语"); return GetResourceText("HTEXLabel.LableRibbon-en.xml"); default: // System.Windows.Forms.MessageBox.Show("未知的 Office 语言版本:" + languageId.ToString()); return GetResourceText("HTEXLabel.LableRibbon-en.xml"); } } #endregion #region 功能区回调 //在此处创建回叫方法。有关添加回叫方法的详细信息,请访问 https://go.microsoft.com/fwlink/?LinkID=271226 public void Ribbon_Load(IRibbonUI ribbonUI) { this.ribbon = ribbonUI; } #endregion #region 帮助器 private static string GetResourceText(string resourceName) { Assembly asm = Assembly.GetExecutingAssembly(); string[] resourceNames = asm.GetManifestResourceNames(); for (int i = 0; i < resourceNames.Length; ++i) { if (string.Compare(resourceName, resourceNames[i], StringComparison.OrdinalIgnoreCase) == 0) { using (StreamReader resourceReader = new StreamReader(asm.GetManifestResourceStream(resourceNames[i]))) { if (resourceReader != null) { return resourceReader.ReadToEnd(); } } } } return null; } public void OnButtonCheck(Office.IRibbonControl control) { Document currentDocument = Globals.ThisAddIn.Application.ActiveDocument; var a = currentDocument.XMLNodes; var ass = a.ToString(); // 创建一个新的 Open XML 文档 } public void OnButtonClick(Office.IRibbonControl control) { // 在这里根据按钮的标识执行相应的操作 string buttonId = control.Id; string buttonTag = control.Tag; // 获取当前文档的光标位置 Microsoft.Office.Interop.Word.Selection selection = Globals.ThisAddIn.Application.Selection; // 在光标位置插入内容 if (buttonId.Equals("compose")) { // 获取 Office 应用程序的语言设置 LanguageSettings languageSettings = Globals.ThisAddIn.Application.LanguageSettings; // 获取主语言 ID int languageId = languageSettings.get_LanguageID(MsoAppLanguageID.msoLanguageIDUI); // 判断语言版本 switch (languageId) { case 2052: // 2052 是简体中文的语言 ID selection.TypeText("{" + buttonTag + "}{结束}"); break; case 1028: // 1028 是繁体中文的语言 ID selection.TypeText("{" + buttonTag + "}{結束}"); break; case 1033: // 1033 是英语的语言 ID selection.TypeText("{" + buttonTag + "}{Ended}"); break; default: // System.Windows.Forms.MessageBox.Show("未知的 Office 语言版本:" + languageId.ToString()); selection.TypeText("{" + buttonTag + "}"); break; } } else { selection.TypeText("{" + buttonTag + "}"); } } public void ShowQRCodeForm(IRibbonControl control) { // 创建 Windows Forms 窗口 QRCodeForm qrCodeForm = new QRCodeForm(control) { MaximizeBox = false, MinimizeBox = false, // 设置窗体的边框样式 FormBorderStyle = FormBorderStyle.FixedSingle, Width=215, Height=240, // 显示窗口 }; CenterFormOnScreen(qrCodeForm); qrCodeForm.ShowDialog(); } public void CenterFormOnScreen(QRCodeForm qrCodeForm) { // 获取屏幕的工作区域 var workingArea = System.Windows.Forms.Screen.GetWorkingArea(qrCodeForm); // 计算窗体应该显示的位置 int x = (workingArea.Width - qrCodeForm.Width) / 2 + workingArea.Left; int y = (workingArea.Height - qrCodeForm.Height) / 2 + workingArea.Top; // 设置窗体位置 qrCodeForm.Location = new System.Drawing.Point(x,y); } #endregion } }