欢迎加我的QQ群:193522571,一起来讨论、交流!

AutoCAD.Net/C#.Net QQ群:193522571 AutoCAD .Net 创建Ribbon界面并且进行执行

版权声明:本文为CSDN博主「HisinWang」的原创文章,遵循CC 4.0 by-sa版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/hisinwang/article/details/78797068

从 AutoCAD 2009 版开始,引入了 Ribbon 界面。
那怎样用代码创建 Ribbon 界面呢?
本篇文章讲解如何创建如下图所示的 Ribbon Tab 页:测试Ribbon页。
 
如上图所示:
AutoCAD 的 Ribbon 界面由一系列 Tab 组成:默认、插入、注释……
测试Ribbon页 是我们创建的 Tab 页。
它由两个 Panel 组成:Panel1、Panel2。
Panel 下面就是三个按钮:直线、圆、Test。
除了需要添加 AutoCAD 引用: acmgd、acdbmgd、accoremgd(AutoCAD 2012版本及后续版本)。
还需要添加如下 AutoCAD 引用:
* AcWindows
* AdWindows
using System;
using System.Collections.Generic;
using System.Text;

using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.EditorInput;

using Autodesk.AutoCAD.Ribbon;
using Autodesk.Windows;

//-----------------------------------------------
private static RibbonTab myRibbonTab = null;

[CommandMethod("MyRibbonTab")]
public static void MyRibbonTab()
{
    // 创建Ribbon Tab页
    if (myRibbonTab == null)
    {
        myRibbonTab = new RibbonTab();
        myRibbonTab.Title = "测试Ribbon页";
        myRibbonTab.Id = "MyRibbonTab";

        //
        RibbonPanel panel1 = new RibbonPanel();
        RibbonPanelSource panel1Src = new RibbonPanelSource();
        panel1Src.Title = "Panel 1";
        panel1.Source = panel1Src;
        myRibbonTab.Panels.Add(panel1);

        RibbonButton rbnBtnLine = NewRibbonBtn("直线");
        panel1Src.Items.Add(rbnBtnLine);
        RibbonButton rbnBtnCircle = NewRibbonBtn("");
        panel1Src.Items.Add(rbnBtnCircle);

        //
        RibbonPanel panel2 = new RibbonPanel();
        RibbonPanelSource panel2Src = new RibbonPanelSource();
        panel2Src.Title = "Panel 2";
        panel2.Source = panel2Src;
        myRibbonTab.Panels.Add(panel2);

        RibbonButton rbnBtnTest = NewRibbonBtn("Test");
        panel2Src.Items.Add(rbnBtnTest);
    }

    // 在AutoCAD的Ribbon窗口中显示
    RibbonControl ribCntrl = RibbonServices.RibbonPaletteSet.RibbonControl;
    ribCntrl.Tabs.Add(myRibbonTab);
}

private static RibbonButton NewRibbonBtn(string text)
{
    RibbonButton button = new RibbonButton();
    button.Text = text;
    button.ShowText = true;
    return button;
}
--------------------- 
版权声明:本文为CSDN博主「HisinWang」的原创文章,遵循CC 4.0 by-sa版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/hisinwang/article/details/78797068

以上部分,只是实现了 Ribbon 界面,但 Ribbon 按钮还没实现触发执行命令。
首先,我们实现一自定义类 AdskCommandHandler,通过该类关联 Ribbon 按钮和对应的命令。

public class AdskCommonHandler : ICommand
    {
        public event EventHandler CanExecuteChanged;

        public bool CanExecute(object parameter)
        {
            return true;
        }

        public void Execute(object parameter)
        {
            RibbonButton button = parameter as RibbonButton;
            if (button != null)
            {
                Application.DocumentManager.MdiActiveDocument.SendStringToExecute(
                    (string)button.CommandParameter, true, false, true);
            }
        }
    }
--------------------- 
版权声明:本文为CSDN博主「HisinWang」的原创文章,遵循CC 4.0 by-sa版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/hisinwang/article/details/78797068

然后,更改 MyRibbonTab 代码如下:

using System;
using System.Collections.Generic;
using System.Text;

using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.EditorInput;

using Autodesk.AutoCAD.Ribbon;
using Autodesk.Windows;

//-----------------------------------------------
private static RibbonTab myRibbonTab = null;

[CommandMethod("MyRibbonTab")]
public static void MyRibbonTab()
{
    // 创建Ribbon Tab页
    if (myRibbonTab == null)
    {
        myRibbonTab = new RibbonTab();
        myRibbonTab.Title = "测试Ribbon页";
        myRibbonTab.Id = "MyRibbonTab";

        //
        RibbonPanel panel1 = new RibbonPanel();
        RibbonPanelSource panel1Src = new RibbonPanelSource();
        panel1Src.Title = "Panel 1";
        panel1.Source = panel1Src;
        myRibbonTab.Panels.Add(panel1);

        RibbonButton rbnBtnLine = NewRibbonBtn("直线", "line ");
        panel1Src.Items.Add(rbnBtnLine);
        RibbonButton rbnBtnCircle = NewRibbonBtn("", "circle ");
        panel1Src.Items.Add(rbnBtnCircle);

        //
        RibbonPanel panel2 = new RibbonPanel();
        RibbonPanelSource panel2Src = new RibbonPanelSource();
        panel2Src.Title = "Panel 2";
        panel2.Source = panel2Src;
        myRibbonTab.Panels.Add(panel2);

        RibbonButton rbnBtnTest = NewRibbonBtn("Test", "MyCmdTest ");
        panel2Src.Items.Add(rbnBtnTest);
    }

    // 在AutoCAD的Ribbon窗口中显示
    RibbonControl ribCntrl = RibbonServices.RibbonPaletteSet.RibbonControl;
    bool isShow = false;
    foreach (RibbonTab rbnTab in ribCntrl.Tabs)
    {
        if (rbnTab == myRibbonTab)
        {
            isShow = true;
            break;
        }
    }
    if (!isShow)
    {
        ribCntrl.Tabs.Add(myRibbonTab);
    }
}

[CommandMethod("MyCmdTest")]
public static void MyCmdTest()
{
    Document doc = Application.DocumentManager.MdiActiveDocument;
    doc.Editor.WriteMessage("www.caxdev.com\n");
}

private static RibbonButton NewRibbonBtn(string text, string cmd)
{
    RibbonButton button = new RibbonButton();
    button.Text = text;
    button.ShowText = true;
    button.CommandHandler = new AdskCommonHandler();
    button.CommandParameter = cmd;
    return button;
}
--------------------- 
版权声明:本文为CSDN博主「HisinWang」的原创文章,遵循CC 4.0 by-sa版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/hisinwang/article/details/78797068
上一篇文章实现了简单的 Ribbon 页面,仅仅包含了文字按钮。
接下来,我们来实现如下图所示的包含图片的 Ribbon 控件。
 
首先,在项目中添加资源文件。在资源文件中添加两张图片:
* smiley_16x16.png
* smiley_32x32.png
大小分别是 16x16 和 32x32。
将上一篇中创建 Ribbon 按钮的代码替换为如下的代码。
// 按钮 Test1
RibbonButton btn1 = new RibbonButton();
btn1.Text = "Test1";
btn1.ShowText = true;
btn1.Image = ImageSourceForBitmap(Resource1.smiley_16x16);
btn1.LargeImage = ImageSourceForBitmap(Resource1.smiley_32x32);
btn1.ShowImage = true;
btn1.Size = RibbonItemSize.Large;
btn1.Orientation = System.Windows.Controls.Orientation.Horizontal;
panelSrc.Items.Add(btn1);

// 按钮 Test2
RibbonButton btn2 = new RibbonButton();
btn2.Text = "Test2";
btn2.ShowText = true;
btn2.Image = ImageSourceForBitmap(Resource1.smiley_16x16);
btn2.LargeImage = ImageSourceForBitmap(Resource1.smiley_32x32);
btn2.ShowImage = true;
btn2.Size = RibbonItemSize.Large;
btn2.Orientation = System.Windows.Controls.Orientation.Vertical;
panelSrc.Items.Add(btn2);

// 按钮 Test3
RibbonButton btn3 = new RibbonButton();
btn3.Text = "Test2";
btn3.ShowText = true;
btn3.Image = ImageSourceForBitmap(Resource1.smiley_16x16);
btn3.LargeImage = ImageSourceForBitmap(Resource1.smiley_32x32);
btn3.ShowImage = true;
btn3.Size = RibbonItemSize.Standard;
btn3.Orientation = System.Windows.Controls.Orientation.Vertical;
panelSrc.Items.Add(btn3);
--------------------- 
版权声明:本文为CSDN博主「HisinWang」的原创文章,遵循CC 4.0 by-sa版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/hisinwang/article/details/78797087

添加引用 System.Drawing。
还要添加如下的处理 Bitmap 的代码。

using System;
using System.Text;
using System.Runtime.InteropServices;
using System.Drawing;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Interop;12345678



[DllImport("gdi32.dll", EntryPoint = "DeleteObject")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool DeleteObject([In] IntPtr hObject);

public static ImageSource ImageSourceForBitmap(Bitmap bmp)
{
    var handle = bmp.GetHbitmap();
    try
    {
        return Imaging.CreateBitmapSourceFromHBitmap(handle, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
    }
    finally { DeleteObject(handle); }
}
--------------------- 
版权声明:本文为CSDN博主「HisinWang」的原创文章,遵循CC 4.0 by-sa版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/hisinwang/article/details/78797087

RibbonButton 有两个属性 Image 和 LargeImage,分别存放小图片和大图片。
小图片大小应为 16x16。
大图片大小应为 32x32。
最终,显示小图片还是大图片,通过指定 Size 为 RibbonItemSize.Standard 还是 RibbonItemSize.Large 确定。
ShowText,是否显示文字。
ShowImage,是否显示图片。
Orientation,指定文字、图片的排布方式。

posted @ 2019-08-16 09:19  swtool  阅读(729)  评论(0编辑  收藏  举报
欢迎加我的QQ群:193522571,一起来讨论、交流!