VSTO为Excel快捷菜单添加项
示例代碼:
public partial class Sheet4 ??? {
??????? Office.CommandBar menuBar;
???????? Office.CommandBarPopup menuCustom;
??????? Office.CommandBarPopup menu1;
??????? Office.CommandBarButton menu2;
??????? Office.CommandBarButton menu1_1;
??????? Office.CommandBarButton menu1_2;
??????? Office.CommandBarButton menu1_3;
??????? Office.CommandBar bar1;
??????? Office.CommandBarPopup cmenu;
??????? Office.CommandBarButton cmenu_1;
??????? Office.CommandBarButton cmenu_2;
??????? private void Sheet4_Startup(object sender, System.EventArgs e)
??????? {
??????????? /普通的菜單///
??????????? //添加一級菜單
??????????? menuBar = this.Application.CommandBars.ActiveMenuBar;
??????????? menuCustom =
??????????????? (Office.CommandBarPopup)menuBar.Controls.Add(
??????????????? Office.MsoControlType.msoControlPopup,
??????????????? missing, missing, missing, true);
??????????? menuCustom.Caption = “自定義菜單”;
??????????? //添加二級菜單
??????????? menu1 =
??????????????? (Office.CommandBarPopup)menuCustom.Controls.Add(
??????????????? Office.MsoControlType.msoControlPopup,
??????????????? missing, missing, missing, true);
??????????? menu1.Caption = “菜單風(fēng)格(&S)”;
??????????? //添加同級的菜單
??????????? menu2 =
??????????????? (Office.CommandBarButton)menuCustom.Controls.Add(
??????????????? Office.MsoControlType.msoControlButton,
??????????????? missing, missing, missing, true);
??????????? menu2.Caption = “自定義按鈕類型的菜單(&S)”;
??????????? //添加事件要設(shè)置Tag屬性
??????????? menu2.Tag = “custom”;
??????????? //添加的事件處理函數(shù)
??????????? menu2.Click += new Microsoft.Office.Core._CommandBarButtonEvents_ClickEventHandler(menu2_Click);
??????????? //添加三級菜單
??????????? menu1_1 =
??????????????? (Office.CommandBarButton)menu1.Controls.Add(
??????????????? Office.MsoControlType.msoControlButton,
??????????????? missing, missing, missing, true);
??????????? menu1_1.Caption = “Up”;
??????????? menu1_1.Tag = “MyUp”;
??????????? menu1_1.State = Office.MsoButtonState.msoButtonUp;
??????????? //添加事件
??????????? menu1_1.Click += new Microsoft.Office.Core._CommandBarButtonEvents_ClickEventHandler(menu1_1_Click);
??????????? menu1_2 =
??????????????? (Office.CommandBarButton)menu1.Controls.Add(
??????????????? Office.MsoControlType.msoControlButton,
??????????????? missing, missing, missing, true);
??????????? menu1_2.Caption = “Down”;
??????????? menu1_2.Tag = “MyDown”;
??????????? menu1_2.State = Office.MsoButtonState.msoButtonDown;
??????????? //
??????????? menu1_2.Click += new Microsoft.Office.Core._CommandBarButtonEvents_ClickEventHandler(menu1_1_Click);
??????????? menu1_3 =
??????????????? (Office.CommandBarButton)menu1.Controls.Add(
??????????????? Office.MsoControlType.msoControlButton,
??????????????? missing, missing, missing, true);
??????????? menu1_3.Caption = “Mixed”;
??????????? menu1_3.State = Office.MsoButtonState.msoButtonMixed;
??????????? //上下文菜單/
??????????? bar1 = Application.CommandBars["Cell"];
??????????? if (bar1 == null)
??????????????? return;
??????????? //添加一級菜單
?????????? ??????????? cmenu =
??????????????? (Office.CommandBarPopup)bar1.Controls.Add(
??????????????? Office.MsoControlType.msoControlPopup,
??????????????? missing, missing, missing, true);
??????????? cmenu.Caption = “自定義菜單”;
??????????? //添加二級菜單
??????????? cmenu_1 =
??????????????? (Office.CommandBarButton)cmenu.Controls.Add(
??????????????? Office.MsoControlType.msoControlButton,
??????????????? missing, missing, missing, true);
??????????? cmenu_1.Caption = “Up”;
??????????? cmenu_1.State = Office.MsoButtonState.msoButtonUp;
??????????? //添加二級菜單
??????????? cmenu_2 =
??????????????? (Office.CommandBarButton)cmenu.Controls.Add(
??????????????? Office.MsoControlType.msoControlButton,
??????????????? missing, missing, missing, true);
??????????? cmenu_2.Caption = “Down”;
??????????? cmenu_2.State = Office.MsoButtonState.msoButtonDown;
??????? }
??????? void menu1_1_Click(Microsoft.Office.Core.CommandBarButton Ctrl, ref bool CancelDefault)
??????? {
??????????? //throw new NotImplementedException();
??????????? if (Ctrl.Caption == “Up”)
??????????? { ???????????????
Ctrl.State = Office.MsoButtonState.msoButtonDown;
??????????????? Ctrl.Caption = “Down”;
??????????? }
??????????? else
??????????? {
??????????????? Ctrl.State = Office.MsoButtonState.msoButtonUp;
??????????????? Ctrl.Caption = “Up”;
??????????? }
??????? }
??????? void menu2_Click(Microsoft.Office.Core.CommandBarButton Ctrl, ref bool CancelDefault)
??????? {
??????????? //throw new NotImplementedException();
??????????? MessageBox.Show(”這是一個(gè)自定義菜單項(xiàng)”);
??????? }
??????? private void Sheet4_Shutdown(object sender, System.EventArgs e) ??????? { ??????? }
??????? #region VSTO Designer generated code
??????? /// <summary>
??????? /// Required method for Designer support - do not modify
??????? /// the contents of this method with the code editor.
??????? /// </summary>
??????? private void InternalStartup()
??????? {
??????????? this.Startup += new System.EventHandler(Sheet4_Startup);
??????????? this.Shutdown += new System.EventHandler(Sheet4_Shutdown);
??????? }
??????? #endregion
轉(zhuǎn)載于:https://www.cnblogs.com/lisengl/archive/2012/11/28/2792153.html
總結(jié)
以上是生活随笔為你收集整理的VSTO为Excel快捷菜单添加项的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 微软的公开的DLL库
- 下一篇: 封装事件绑定函数解决this在ie下的绑