如何使用C#添加带文本和图标的Excel行命令栏标题

我需要在运行时使用C#在Excel命令栏中添加一个组头,这样我就可以将Office.CommandBarButton选项与默认的Excel选项分开。例如,在Excel 2013中,如果您使用鼠标右键选择一行和鼠标右键,则会显示默认命令栏,其中包含许多选项。你会注意到有一个标题叫做"Paste Options:“,在左边有一个标准的粘贴图标。我想使用C#创建一个类似于“粘贴选项:”的标题(组)。

顺便说一下,我使用下面的代码示例在Excel中成功地添加了几个Office.CommandBarButton选项;

    private void AddMyRowMenu() 
    {
       Office.CommandBars commandBars = null;
       Office.CommandBar commandBarRowMenu = null;
       Office.CommandBarButton commandBarButtonMyOptions1;
       try
       {
           commandBarRowMenu = commandBars["Row"];
           commandBarButtonMyOptions1 = (Office.CommandBarButton)commandBarRowMenu.Controls["My Option 1"];
       }
       catch (ArgumentException)
       {
           commandBarButtonMyOptions1 = (Office.CommandBarButton)commandBarRowMenu.Controls.Add(Office.MsoControlType.msoControlButton, oMissing, oMissing, oMissing, oMissing);
           commandBarButtonMyOptions1.BeginGroup = true;
           commandBarButtonMyOptions1.Caption = "My Option 1";
       }
       commandBarButtonMyOptions1.Click += new Microsoft.Office.Core._CommandBarButtonEvents_ClickEventHandler(commandBarButtonMyOptions1_Click);
    }

我使用上面的代码添加了3个Office.CommandBarButton选项,为了清晰起见,需要将它们与默认的Excel人民币选项分开。

转载请注明出处:http://www.guonuovip.com/article/20230401/2035351.html