首先引用一下 SDK下的ZdWindows.dll和ZcWindows.dll
参考下面代码
[CommandMethod(“Test_LoadRibbonMenu”)]
public void LoadRibbonMenu()
{
RibbonControl ribbonCtrl = ComponentManager.Ribbon;
RibbonTab tab1 = new RibbonTab();
tab1.Title = “测试Ribbon菜单”;
ribbonCtrl.Tabs.Add(tab1);
tab1.IsActive = true;
RibbonPanelSource panelSource = new RibbonPanelSource();
panelSource.Title = “测试Ribbon”;
RibbonPanel panel = new RibbonPanel();
panel.Source = panelSource;
tab1.Panels.Add(panel);
//RibbonSplitButton splitbtn = new RibbonSplitButton();
//splitbtn.Name = “splitbtn”;
//splitbtn.Text = “split测试按钮”;
//splitbtn.Description = “split测试按钮”;
//splitbtn.ShowText = true;
//splitbtn.Size = RibbonItemSize.Large;
//splitbtn.ShowImage = false;
//splitbtn.Orientation = Orientation.Vertical;
//panelSource.Items.Add(splitbtn);
RibbonButton btn1 = new RibbonButton();
btn1.Name = “btn1”;
btn1.Text = “测试按钮1”;
btn1.Description = “测试按钮1”;
btn1.ShowText = true;
btn1.Size = RibbonItemSize.Large;
btn1.LargeImage = LoadImage(Properties.Resources.a_large);
btn1.Image = LoadImage(Properties.Resources.a_small);
btn1.Orientation = Orientation.Vertical;
btn1.CommandHandler = new MyCommandHandler();
btn1.CommandParameter = “cmd1 “;
panelSource.Items.Add(btn1);
RibbonButton btn2 = new RibbonButton();
btn2.Name = “btn2”;
btn2.Text = “测试按钮2”;
btn2.Description = “测试按钮2”;
btn2.ShowText = true;
btn2.Size = RibbonItemSize.Large;
btn2.ShowImage = false;
btn2.Orientation = Orientation.Vertical;
btn2.CommandHandler = new MyCommandHandler();
btn2.CommandParameter = “cmd2 “;
panelSource.Items.Add(btn2);
RibbonButton btn3 = new RibbonButton();
btn3.Name = “btn3”;
btn3.Text = “测试按钮3”;
btn3.Description = “测试按钮3”;
btn3.ShowText = true;
btn3.Size = RibbonItemSize.Large;
btn3.ShowImage = false;
btn3.Orientation = Orientation.Vertical;
btn3.CommandHandler = new MyCommandHandler();
btn3.CommandParameter = “cmd1 “;
panelSource.Items.Add(btn3);
}
[CommandMethod(“cmd1”)]
public void cmd1()
{
Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
ed.WriteMessage(“执行命令:cmd1”);
}
[CommandMethod(“cmd2”)]
public void cmd2()
{
Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
ed.WriteMessage(“执行命令:cmd2”);
}
[CommandMethod(“cmd3”)]
public void cmd3()
{
Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
ed.WriteMessage(“执行命令:cmd3”);
}
public class MyCommandHandler : System.Windows.Input.ICommand
{
/// <summary>
/// 是否可执行
/// </summary>
/// <param name=”parameter”>命令名</param>
/// <returns></returns>
public bool CanExecute(object parameter)
{
return true;//
}
/// <summary>
/// 改变可执行状态
/// </summary>
public event EventHandler CanExecuteChanged;
/// <summary>
/// 执行命令
/// </summary>
/// <param name=”parameter”></param>
public void Execute(object parameter)
{
try
{
//is from Ribbon Button
RibbonButton ribBtn = parameter as RibbonButton;
if (ribBtn != null)
{
string cmd = (string)ribBtn.CommandParameter;
Application.DocumentManager.MdiActiveDocument.SendStringToExecute(“\u001b\u001b” + cmd, true, false, true);
}
}
catch (System.Exception ex)
{
//声明命令对象
Editor e = Application.DocumentManager.MdiActiveDocument.Editor;
//输出文字
e.WriteMessage(“执行命令失败:” + ex.Message);
}
}
}
中望2018支持吗?我找不到ZdWindows.dll和ZcWindows.dll这两个文件
从2022版本才开始支持,2022以下的版本只能用cui命令来编辑添加ribbon菜单