ADN Open CIS
Сообщество программистов Autodesk в СНГ

06/08/2015

Как показать иерархическое меню в Ленте

Если вам нужно показать простой (одноранговый) список – подойдёт RibbonSplitButton. О нём есть соответствующая статья. Но если вам нужно организовать иерархическое меню - подойдёт RibbonMenuButton. Ниже пример кода, создающий меню как на следующей картинке:

 

Код - C#: [Выделить]
  1. using System;
  2. using System.Windows.Media.Imaging;
  3. using System.IO;
  4. using System.Drawing;
  5. using System.Drawing.Imaging;
  6. using Autodesk.AutoCAD.Runtime;
  7. using Autodesk.AutoCAD.ApplicationServices;
  8. // Требуется ссылка на AdWindows.dll
  9. using Autodesk.Windows;
  10.  
  11. namespace ACADTestTemplate
  12. {
  13.     public class Test
  14.     {
  15.         [CommandMethod("RibbonMenuButton")]
  16.         public void RibbonMenuButton()
  17.         {
  18.             RibbonControl ribbonControl = ComponentManager.Ribbon;
  19.  
  20.             RibbonTab Tab = new RibbonTab();
  21.             Tab.Title = "Тест Ленты";
  22.             Tab.Id = "TESTRIBBON_TAB_ID";
  23.  
  24.             ribbonControl.Tabs.Add(Tab);
  25.  
  26.             RibbonPanelSource srcPanel = new RibbonPanelSource();
  27.             srcPanel.Title = "Панель1";
  28.  
  29.             RibbonPanel Panel = new RibbonPanel();
  30.             Panel.Source = srcPanel;
  31.             Tab.Panels.Add(Panel);
  32.  
  33.             RibbonMenuItem button1 = new RibbonMenuItem();
  34.             button1.Text = "Кнопка1";
  35.             button1.ShowText = true;
  36.             button1.LargeImage
  37.                 = getBitmap(Properties.Resources.Image1, 32, 32);
  38.             button1.Image
  39.                 = getBitmap(Properties.Resources.Image1, 16, 16);
  40.             button1.CommandHandler = new MenuButtonCmdHandler();
  41.  
  42.             RibbonMenuItem subButton1 = new RibbonMenuItem();
  43.             subButton1.Text = "ПодКнопка1";
  44.             subButton1.ShowText = true;
  45.             subButton1.LargeImage
  46.                 = getBitmap(Properties.Resources.Image1, 32, 32);
  47.             subButton1.Image
  48.                 = getBitmap(Properties.Resources.Image1, 16, 16);
  49.             subButton1.CommandHandler
  50.                 = new MenuButtonCmdHandler();
  51.  
  52.             RibbonMenuItem subButton2 = new RibbonMenuItem();
  53.             subButton2.Text = "ПодКнопка2";
  54.             subButton2.ShowText = true;
  55.             subButton2.LargeImage
  56.                 = getBitmap(Properties.Resources.Image1, 32, 32);
  57.             subButton2.Image
  58.                 = getBitmap(Properties.Resources.Image1, 16, 16);
  59.             subButton2.CommandHandler  = new MenuButtonCmdHandler();
  60.  
  61.             button1.Items.Add(subButton1);
  62.             button1.Items.Add(subButton2);
  63.  
  64.             RibbonMenuItem button2 = new RibbonMenuItem();
  65.             button2.Text = "Кнопка2";
  66.             button2.ShowText = true;
  67.             button2.LargeImage
  68.                 = getBitmap(Properties.Resources.Image1, 32, 32);
  69.             button2.Image
  70.                 = getBitmap(Properties.Resources.Image1, 16, 16);
  71.             button2.CommandHandler = new MenuButtonCmdHandler();
  72.  
  73.             RibbonMenuButton ribMenuButton = new RibbonMenuButton();
  74.  
  75.             ribMenuButton.Id = "ADN.RibbonMenuButton.1";
  76.  
  77.             ribMenuButton.Text = "RibbonMenuButton";
  78.             ribMenuButton.ShowText = true;
  79.             ribMenuButton.Size = RibbonItemSize.Large;
  80.             ribMenuButton.LargeImage
  81.                 = getBitmap(Properties.Resources.Image1, 32, 32);
  82.             ribMenuButton.Image
  83.                 = getBitmap(Properties.Resources.Image1, 16, 16);
  84.             ribMenuButton.ShowImage = true;
  85.             ribMenuButton.MaxHeight = double.PositiveInfinity;
  86.             ribMenuButton.MinHeight = 0;
  87.             ribMenuButton.IsSplit = true;
  88.             ribMenuButton.IsSynchronizedWithCurrentItem = true;
  89.  
  90.             ribMenuButton.Items.Add(button1);
  91.             ribMenuButton.Items.Add(button2);
  92.  
  93.             srcPanel.Items.Add(ribMenuButton);
  94.  
  95.             Tab.IsActive = true;
  96.         }
  97.  
  98.         BitmapImage getBitmap(Bitmap bitmap, int height, int width)
  99.         {
  100.             MemoryStream stream = new MemoryStream();
  101.             bitmap.Save(stream, ImageFormat.Png);
  102.  
  103.             BitmapImage bmp = new BitmapImage();
  104.             bmp.BeginInit();
  105.             bmp.StreamSource = new MemoryStream(stream.ToArray());
  106.             bmp.DecodePixelHeight = height;
  107.             bmp.DecodePixelWidth = width;
  108.             bmp.EndInit();
  109.  
  110.             return bmp;
  111.         }
  112.  
  113.         public class MenuButtonCmdHandler
  114.             : System.Windows.Input.ICommand
  115.         {
  116.             public bool CanExecute(object parameter)
  117.             {
  118.                 return true;
  119.             }
  120.  
  121.             public event EventHandler CanExecuteChanged;
  122.  
  123.             public void Execute(object parameter)
  124.             {
  125.                 Document doc
  126.                     = Application.DocumentManager.MdiActiveDocument;
  127.  
  128.                 if (parameter is RibbonMenuItem)
  129.                 {
  130.                     RibbonMenuItem menuItem
  131.                         = parameter as RibbonMenuItem;
  132.                     if (menuItem != null)
  133.                     {
  134.                         doc.Editor.WriteMessage(
  135.                             "\nВыполняется пункт меню: "
  136.                             + menuItem.Text);
  137.                     }
  138.                 }
  139.             }
  140.         }
  141.     }
  142. }

Источник: 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