ThisAddIn.cs 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Xml.Linq;
  6. using Word = Microsoft.Office.Interop.Word;
  7. using Office = Microsoft.Office.Core;
  8. using Microsoft.Office.Tools.Word;
  9. using Microsoft.Office.Core;
  10. namespace HTEXLabel
  11. {
  12. public partial class ThisAddIn
  13. {
  14. private Office.CommandBar customNavBar;
  15. private LableRibbon myRibbon;
  16. private void ThisAddIn_Startup(object sender, System.EventArgs e)
  17. {
  18. //AddCustomNavBar();
  19. LoadRibbon();
  20. }
  21. private void LoadRibbon()
  22. {
  23. // 在这里加载自定义 Ribbon
  24. }
  25. private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
  26. {
  27. }
  28. protected override Microsoft.Office.Core.IRibbonExtensibility CreateRibbonExtensibilityObject()
  29. {
  30. return new LableRibbon();
  31. }
  32. #region VSTO 生成的代码
  33. /// <summary>
  34. /// 设计器支持所需的方法 - 不要修改
  35. /// 使用代码编辑器修改此方法的内容。
  36. /// </summary>
  37. private void InternalStartup()
  38. {
  39. this.Startup += new System.EventHandler(ThisAddIn_Startup);
  40. this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
  41. }
  42. #endregion
  43. private void AddCustomNavBar()
  44. {
  45. customNavBar = this.Application.CommandBars.Add("CustomNavBar", MsoBarPosition.msoBarTop, System.Type.Missing, true);
  46. customNavBar.Visible = true;
  47. // 添加按钮和分组,可以根据需要添加更多的按钮和分组
  48. AddButton("单选题", "插入单选题");
  49. AddButton("多选题", "插入多选题");
  50. AddButton("判断题", "插入判断题");
  51. AddButton("记忆", "插入记忆");
  52. AddButton("创造", "插入创造");
  53. AddButton("应用", "插入应用");
  54. AddButton("答案", "插入答案");
  55. AddButton("解析", "插入解析");
  56. AddButton("知识点", "插入知识点");
  57. }
  58. private void AddButton(string buttonName, string buttonCaption)
  59. {
  60. CommandBarButton button = (CommandBarButton)customNavBar.Controls.Add(MsoControlType.msoControlButton, System.Type.Missing, System.Type.Missing, 1, true);
  61. button.Style = MsoButtonStyle.msoButtonCaption;
  62. button.Caption = buttonCaption;
  63. button.Tag = buttonName;
  64. button.Click += new Microsoft.Office.Core._CommandBarButtonEvents_ClickEventHandler(Button_Click);
  65. }
  66. private void Button_Click(CommandBarButton Ctrl, ref bool CancelDefault)
  67. {
  68. // 在这里根据按钮的 Tag 属性执行相应的操作
  69. string buttonName = Ctrl.Tag.ToString();
  70. InsertContent(buttonName);
  71. }
  72. private void InsertContent(string content)
  73. {
  74. // 获取当前文档的光标位置
  75. Word.Selection selection = this.Application.Selection;
  76. // 在光标位置插入内容
  77. selection.TypeText("{" + content + "}");
  78. }
  79. }
  80. }