Disclaimer: This article is the original article CSDN bloggers "HisinWang", following the CC 4.0 by-sa copyright agreement, reproduced, please attach the original source link and this statement.
Original link: https: //blog.csdn.net/hisinwang/article/details/78797068
How does that create the Ribbon interface in code?
This article explains how to create a figure below the Ribbon Tab Page: Ribbon test page.
Shown above:
AutoCAD Ribbon interface consists of a series Tab: the default, insert, comment ......
test Ribbon Tab page is a page that we created.
It consists of two Panel composed of: Panel1, Panel2.
Panel Here are three buttons: lines, circles, Test.
AutoCAD also need to add the following references:
* 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 = to true ; return the Button; } --------------------- Disclaimer: This article is CSDN blogger "HisinWang 'original article, follow the CC 4.0 by- SA copyright agreement, reproduced, please attach the original source link and this statement. Original link: HTTPS: // blog.csdn.net/hisinwang/article/details/78797068
Section above, but realized the Ribbon interface, but not yet implemented Ribbon button to trigger the execution of the command.
First, we implement a custom class AdskCommandHandler, by the class association Ribbon button and the corresponding command.
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, to true , false , to true ); } } } --------------------- Disclaimer: This article is CSDN original blogger "HisinWang" of article, follow the CC 4.0 by- SA copyright agreement, reproduced, please attach the original source link and this statement. Original link: HTTPS: // blog.csdn.net/hisinwang/article/details/78797068
Then, change MyRibbonTab code is as follows:
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 new RibbonButton (); button.Text = text; button.ShowText = to true ; button.CommandHandler = new new AdskCommonHandler (); button.CommandParameter = cmd; return the Button; } --------------------- Disclaimer: This article is CSDN blogger "HisinWang 'original article, follow the CC 4.0 by- SA copyright agreement, reprint Please include links to the original source and this statement. Original link: https://blog.csdn.net/hisinwang/article/details/78797068
Next, we come to realize Ribbon control contains a picture as shown below.
First, add the resource file to the project. Add two pictures in a resource file:
* smiley_16x16.png
* smiley_32x32.png
The previous code to create Ribbon buttons replaced with the following code.
// 按钮 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 = to true ; btn3.Image = ImageSourceForBitmap (Resource1.smiley_16x16); btn3.LargeImage = ImageSourceForBitmap (Resource1.smiley_32x32); btn3.ShowImage = to true ; btn3.Size = RibbonItemSize.Standard; btn3.Orientation = System.Windows.Controls.Orientation.Vertical; panelSrc.Items.Add (btn3); --------------------- Disclaimer: This article is CSDN bloggers " original articles HisinWang ", following the CC 4.0 by- SA copyright agreement, reproduced, please attach the original source link and this statement. Original link: HTTPS: //blog.csdn.net/hisinwang/article/details/78797087
Add a reference to System.Drawing.
Also add the following code to handle 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 staticImageSourceForBitmap the ImageSource (Bitmap BMP) { var handle = bmp.GetHbitmap (); the try { return Imaging.CreateBitmapSourceFromHBitmap (handle, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions ()); } the finally {the DeleteObject (handle);} } --------------------- Disclaimer: This article is the original article CSDN bloggers "HisinWang", following the CC 4.0 by- SA copyright agreement, reproduced, please attach the original source link and this statement. Original link: HTTPS: // blog.csdn.net/hisinwang/article/details/78797087
RibbonButton has two properties Image and LargeImage, were stored small picture and the big picture.
Small picture size should be 16x16.
Large picture size should be 32x32.
In the end, it shows a small picture or big picture, identified as RibbonItemSize.Standard or RibbonItemSize.Large by specifying Size.
ShowText, whether to display text.
ShowImage, whether to display the picture.
Orientation, specify the text, picture arrangement manner.