06/08/2015
Как показать иерархическое меню в Ленте
Если вам нужно показать простой (одноранговый) список – подойдёт RibbonSplitButton. О нём есть соответствующая статья. Но если вам нужно организовать иерархическое меню - подойдёт RibbonMenuButton. Ниже пример кода, создающий меню как на следующей картинке:
Код - C#: [Выделить]
- using System;
- using System.Windows.Media.Imaging;
- using System.IO;
- using System.Drawing;
- using System.Drawing.Imaging;
- using Autodesk.AutoCAD.Runtime;
- using Autodesk.AutoCAD.ApplicationServices;
- // Требуется ссылка на AdWindows.dll
- using Autodesk.Windows;
- namespace ACADTestTemplate
- {
- public class Test
- {
- [CommandMethod("RibbonMenuButton")]
- public void RibbonMenuButton()
- {
- RibbonControl ribbonControl = ComponentManager.Ribbon;
- RibbonTab Tab = new RibbonTab();
- Tab.Title = "Тест Ленты";
- Tab.Id = "TESTRIBBON_TAB_ID";
- ribbonControl.Tabs.Add(Tab);
- RibbonPanelSource srcPanel = new RibbonPanelSource();
- srcPanel.Title = "Панель1";
- RibbonPanel Panel = new RibbonPanel();
- Panel.Source = srcPanel;
- Tab.Panels.Add(Panel);
- RibbonMenuItem button1 = new RibbonMenuItem();
- button1.Text = "Кнопка1";
- button1.ShowText = true;
- button1.LargeImage
- = getBitmap(Properties.Resources.Image1, 32, 32);
- button1.Image
- = getBitmap(Properties.Resources.Image1, 16, 16);
- button1.CommandHandler = new MenuButtonCmdHandler();
- RibbonMenuItem subButton1 = new RibbonMenuItem();
- subButton1.Text = "ПодКнопка1";
- subButton1.ShowText = true;
- subButton1.LargeImage
- = getBitmap(Properties.Resources.Image1, 32, 32);
- subButton1.Image
- = getBitmap(Properties.Resources.Image1, 16, 16);
- subButton1.CommandHandler
- = new MenuButtonCmdHandler();
- RibbonMenuItem subButton2 = new RibbonMenuItem();
- subButton2.Text = "ПодКнопка2";
- subButton2.ShowText = true;
- subButton2.LargeImage
- = getBitmap(Properties.Resources.Image1, 32, 32);
- subButton2.Image
- = getBitmap(Properties.Resources.Image1, 16, 16);
- subButton2.CommandHandler = new MenuButtonCmdHandler();
- button1.Items.Add(subButton1);
- button1.Items.Add(subButton2);
- RibbonMenuItem button2 = new RibbonMenuItem();
- button2.Text = "Кнопка2";
- button2.ShowText = true;
- button2.LargeImage
- = getBitmap(Properties.Resources.Image1, 32, 32);
- button2.Image
- = getBitmap(Properties.Resources.Image1, 16, 16);
- button2.CommandHandler = new MenuButtonCmdHandler();
- RibbonMenuButton ribMenuButton = new RibbonMenuButton();
- ribMenuButton.Id = "ADN.RibbonMenuButton.1";
- ribMenuButton.Text = "RibbonMenuButton";
- ribMenuButton.ShowText = true;
- ribMenuButton.Size = RibbonItemSize.Large;
- ribMenuButton.LargeImage
- = getBitmap(Properties.Resources.Image1, 32, 32);
- ribMenuButton.Image
- = getBitmap(Properties.Resources.Image1, 16, 16);
- ribMenuButton.ShowImage = true;
- ribMenuButton.MaxHeight = double.PositiveInfinity;
- ribMenuButton.MinHeight = 0;
- ribMenuButton.IsSplit = true;
- ribMenuButton.IsSynchronizedWithCurrentItem = true;
- ribMenuButton.Items.Add(button1);
- ribMenuButton.Items.Add(button2);
- srcPanel.Items.Add(ribMenuButton);
- Tab.IsActive = true;
- }
- BitmapImage getBitmap(Bitmap bitmap, int height, int width)
- {
- MemoryStream stream = new MemoryStream();
- bitmap.Save(stream, ImageFormat.Png);
- BitmapImage bmp = new BitmapImage();
- bmp.BeginInit();
- bmp.StreamSource = new MemoryStream(stream.ToArray());
- bmp.DecodePixelHeight = height;
- bmp.DecodePixelWidth = width;
- bmp.EndInit();
- return bmp;
- }
- public class MenuButtonCmdHandler
- : System.Windows.Input.ICommand
- {
- public bool CanExecute(object parameter)
- {
- return true;
- }
- public event EventHandler CanExecuteChanged;
- public void Execute(object parameter)
- {
- Document doc
- = Application.DocumentManager.MdiActiveDocument;
- if (parameter is RibbonMenuItem)
- {
- RibbonMenuItem menuItem
- = parameter as RibbonMenuItem;
- if (menuItem != null)
- {
- doc.Editor.WriteMessage(
- "\nВыполняется пункт меню: "
- + menuItem.Text);
- }
- }
- }
- }
- }
- }
Источник: http://adndevblog.typepad.com/autocad/2015/07/displaying-hierarchical-list-of-options-in-ribbon.html
Автор перевода: Александр Ривилис
Обсуждение: http://adn-cis.org/forum/index.php?topic=2902
Опубликовано 06.08.2015