日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

VSTO为Excel快捷菜单添加项

發布時間:2025/5/22 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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 = “菜單風格(&S)”;

??????????? //添加同級的菜單

??????????? menu2 =

??????????????? (Office.CommandBarButton)menuCustom.Controls.Add(

??????????????? Office.MsoControlType.msoControlButton,

??????????????? missing, missing, missing, true);

??????????? menu2.Caption = “自定義按鈕類型的菜單(&S)”;

??????????? //添加事件要設置Tag屬性

??????????? menu2.Tag = “custom”;

??????????? //添加的事件處理函數

??????????? 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(”這是一個自定義菜單項”);

??????? }

??????? 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

轉載于:https://www.cnblogs.com/lisengl/archive/2012/11/28/2792153.html

總結

以上是生活随笔為你收集整理的VSTO为Excel快捷菜单添加项的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。