C#版ribbon菜单
本帖最后由 epwt 于 2013-7-25 17:16 编辑老大说整个Ribbon菜单用用。
虽然我对这Ribbon菜单没什么好感,觉得中看不中用,得点那么多下。但是用户需求啊。
给需要的童鞋共享下。参考《深入浅出AutoCAD二次开发》李冠亿
public class RibbonMenu
{
public void AddRibbon()
{
RibbonControl rc = RibbonControl();
RibbonTab rt = RibbonTab("我的自定义菜单","1");
RibbonPanel rp = RibbonPanel("菜单面板");
RibbonButton rb = RibbonButton("菜单按钮","菜单命令");
rp.Source.Items.Add(rb);
rt.Panels.Add(rp);
rc.Tabs.Add(rt);
}
/// <summary>
/// 获得菜单控制
/// </summary>
/// <returns>菜单控制</returns>
public static RibbonControl RibbonControl()
{
if (Autodesk.Windows.ComponentManager.Ribbon == null)
{
Autodesk.Windows.ComponentManager.ItemInitialized += new EventHandler<RibbonItemEventArgs>(ComponentManager_ItemInitialized);
}
return Autodesk.Windows.ComponentManager.Ribbon;
}
/// <summary>
/// 用于激活菜单
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
static void ComponentManager_ItemInitialized(object sender, RibbonItemEventArgs e)
{
if (Autodesk.Windows.ComponentManager.Ribbon != null)
{
Autodesk.Windows.ComponentManager.ItemInitialized -= new EventHandler<RibbonItemEventArgs>(ComponentManager_ItemInitialized);
}
}
/// <summary>
/// 创建菜单标签
/// </summary>
/// <param name="name">标签名</param>
/// <param name="id">标签id</param>
/// <returns>菜单标签</returns>
public static RibbonTab RibbonTab(string name, string id)
{
RibbonTab ribTab = new RibbonTab();
ribTab.Title = name;
ribTab.Id = id;
ribTab.IsActive = true;
return ribTab;
}
/// <summary>
/// 创建菜单面板
/// </summary>
/// <param name="title">面板名</param>
/// <returns>菜单面板</returns>
public static RibbonPanel RibbonPanel(string title)
{
RibbonPanelSource ribSourcePanel = new RibbonPanelSource();
ribSourcePanel.Title = title;
RibbonPanel ribPanel = new RibbonPanel();
ribPanel.Source = ribSourcePanel;
return ribPanel;
}
/// <summary>
/// 创建菜单按钮
/// </summary>
/// <param name="btName">按钮名</param>
/// <param name="cmdName">命令名</param>
/// <returns>菜单按钮</returns>
public static RibbonButton RibbonButton(string btName, string cmdName)
{
RibbonButton ribButton = new RibbonButton();
ribButton.Text = btName;
ribButton.CommandParameter = cmdName;
ribButton.ShowText = true;
ribButton.CommandHandler = new CommandHandler();
return ribButton;
}
}
publicclass CommandHandler: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)
{
RibbonButton ribBtn = parameter as RibbonButton;
if (ribBtn != null)
{
Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.SendStringToExecute((String)ribBtn.CommandParameter,true,false,true);
RibbonTextBox ribTxt = parameter as RibbonTextBox;
if (ribTxt != null)
{
MessageBox.Show(ribTxt.TextValue);
}
}
}
}
搞了老半天,最后用SendStringToExecute这个东西做终结者? 怎么这个例子看起来非常眼熟? 怎么和深入浅出那个例子一样 有效果图么?
页:
[1]