Author Topic: ContextMenu  (Read 4732 times)

0 Members and 1 Guest are viewing this topic.

kdub_nz

  • Mesozoic keyThumper
  • SuperMod
  • Water Moccasin
  • Posts: 2145
  • class keyThumper<T>:ILazy<T>
ContextMenu
« on: August 19, 2023, 08:45:37 PM »
Progressing from
this : https://www.theswamp.org/index.php?topic=14492.0
and this: https://www.theswamp.org/index.php?topic=53143.0

I've been updating the way text and commands are passed to my ContextMenu ( used for calling test code )

I thought I'd try to add Icons to the Context Menu
I was able to attach Icons to the MenuItems , but I can't find a way to add an Icon to the
ContextMenuExtension Title object . . . refer to the attached image

I'd be happy to hear from anyone who has a solution for this ?

For anyone interested, this is the code I'm using
I just need to add a few lines to my default Initialization.cs file . . . saves remembering command names for each test routine, and keeps things in order if needed.

added: Have to have a giggle about some of the attempts the GhostDox XML Documenter add-In makes at it's comments :)
but still tonnes easier that doing it all myself


ContextMenu.cs
Code - C#: [Select]
  1. using Autodesk.AutoCAD.Windows;
  2. using System;
  3. using System.Collections.Generic;
  4. using CadApp = Autodesk.AutoCAD.ApplicationServices.Application;
  5.  
  6. // TODO: include in Common namespace Project later
  7. namespace Kdub.Common
  8. {
  9.    /// <summary>
  10.    ///
  11.    /// </summary>
  12.    public class AppContextMenu
  13.    {
  14.       static ContextMenuExtension _appMenu = null;
  15.  
  16.       /// <summary>
  17.       /// Adds the context menu.
  18.       /// </summary>
  19.       /// <param name="title">The title.</param>
  20.       /// <param name="menuContents">The menu items.</param>
  21.       public static void AddContextMenu(string title, List<(string, string)> menuContents)
  22.       {
  23.          _appMenu = new ContextMenuExtension
  24.          {
  25.             Title = title
  26.          };
  27.  
  28.          foreach ((string desc, string cmd) in menuContents)
  29.          {
  30.             if (string.IsNullOrEmpty(desc))
  31.             {
  32.                _appMenu.MenuItems.Add(new MenuItem(""));    // separator
  33.             }
  34.             else
  35.             {
  36.                _appMenu.MenuItems.Add(new AppContextMenuItem(desc, cmd));
  37.             }
  38.          }
  39.  
  40.          var icon = PrivateTesting.Properties.Resources.bug_edit_icon;
  41.          _appMenu.MenuItems[0].Icon = icon;
  42.  
  43.          foreach (MenuItem mnuItem in _appMenu.MenuItems)
  44.          {
  45.             var appContextMnuItem = mnuItem as AppContextMenuItem;
  46.             if (appContextMnuItem != null)
  47.             {
  48.                appContextMnuItem.Click += new EventHandler(ExecuteCommand);
  49.             }
  50.          }
  51.  
  52.          CadApp.AddDefaultContextMenuExtension(_appMenu);
  53.       }
  54.  
  55.       /// <summary>
  56.       /// Executes the command.
  57.       /// </summary>
  58.       /// <param name="o">The o.</param>
  59.       /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
  60.       private static void ExecuteCommand(Object o, EventArgs e)
  61.       {
  62.          var mnuItem = (AppContextMenuItem)o;
  63.  
  64.          string fullCmdLine = string.Format($"_{mnuItem.CommandName}\n");
  65.          CadApp.DocumentManager.MdiActiveDocument.SendStringToExecute(fullCmdLine,
  66.             false, false, true);
  67.       }
  68.  
  69.       /// <summary>
  70.       /// Removes the context menu.
  71.       /// </summary>
  72.       public static void RemoveContextMenu() => CadApp.RemoveDefaultContextMenuExtension(_appMenu);
  73.    }
  74.  
  75.  
  76.    /// <summary>
  77.    ///
  78.    /// </summary>
  79.    /// <seealso cref="Autodesk.AutoCAD.Windows.MenuItem" />
  80.    public class AppContextMenuItem : Autodesk.AutoCAD.Windows.MenuItem
  81.    {
  82.       public string CommandName { get; private set; }
  83.  
  84.       /// <summary>
  85.       /// Initializes a new instance of the <see cref="AppContextMenuItem"/> class.
  86.       /// </summary>
  87.       /// <param name="title">The title.</param>
  88.       /// <param name="cmdName">Name of the command.</param>
  89.       public AppContextMenuItem(string title, string cmdName) : base(title)
  90.       {
  91.          CommandName = cmdName;
  92.       }
  93.    }
  94. }
  95.  
  96.  

Initialization.cs
Code - C#: [Select]
  1. using Autodesk.AutoCAD.Runtime;
  2. using Kdub.Common;
  3. using System;
  4. using System.Collections.Generic;
  5. using CadApp = Autodesk.AutoCAD.ApplicationServices.Application;
  6.  
  7. [assembly: ExtensionApplication(typeof(PrivateTesting.Initialization))]
  8.  
  9. namespace PrivateTesting
  10. {
  11.    public class Initialization : IExtensionApplication
  12.    {
  13.       /// <summary>
  14.       /// Initializes this instance.
  15.       /// </summary>
  16.       public void Initialize()
  17.       {
  18.          CadApp.Idle += OnIdle;
  19.       }
  20.  
  21.       /// <summary>
  22.       /// Called when [idle].
  23.       /// </summary>
  24.       /// <param name="sender">The sender.</param>
  25.       /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
  26.       private void OnIdle(object sender, EventArgs e)
  27.       {
  28.          var doc = CadApp.DocumentManager.MdiActiveDocument;
  29.          if (doc != null)
  30.          {
  31.             CadApp.Idle -= OnIdle;
  32.             doc.Editor.WriteMessage("\nPrivateTesting loaded.\n");
  33.  
  34.             var menuContents = new List<(string desc, string cmd)>
  35.             { // desc , commandString
  36.                 ("Test 1 . . . ", "TEST_01" ),
  37.                 ("Test 2 . . . ", "TEST_02" ),
  38.                 ("", "separator" ),
  39.                 ("Test 3 . . . ", "TEST_03" )
  40.             };
  41.  
  42.             AppContextMenu.AddContextMenu("Current Testing Commands", menuContents);
  43.  
  44.             doc.Editor.WriteMessage("\nRight-Click in drawing for UserMenu.\n");
  45.          }
  46.       }
  47.  
  48.       /// <summary>
  49.       /// Terminates this instance.
  50.       /// </summary>
  51.       public void Terminate()
  52.       { }
  53.    }
  54. }
  55.  
  56.  

Commands.cs
Code - C#: [Select]
  1. using Autodesk.AutoCAD.Runtime;
  2.  
  3. using CadApp = Autodesk.AutoCAD.ApplicationServices.Application;
  4.  
  5. [assembly: CommandClass(typeof(PrivateTesting.Commands))]
  6.  
  7. namespace PrivateTesting
  8. {
  9.    public class Commands
  10.    {
  11.       /// <summary>
  12.       /// Tests 01.
  13.       /// </summary>
  14.       [CommandMethod("TEST_01")]
  15.       public static void Test_01()
  16.       {
  17.          CadApp.DocumentManager.MdiActiveDocument.Editor.WriteMessage(
  18.             "Running test_01\n");
  19.       }
  20.  
  21.       /// <summary>
  22.       /// Tests 02.
  23.       /// </summary>
  24.       [CommandMethod("TEST_02")]
  25.       public static void Test_02()
  26.       {
  27.          CadApp.DocumentManager.MdiActiveDocument.Editor.WriteMessage(
  28.             "Running test_02\n");
  29.       }
  30.  
  31.       /// <summary>
  32.       /// Tests 03.
  33.       /// </summary>
  34.       [CommandMethod("TEST_03")]
  35.       public static void Test_03()
  36.       {
  37.          CadApp.DocumentManager.MdiActiveDocument.Editor.WriteMessage(
  38.             "Running test_03\n");
  39.       }
  40.  
  41.       /// <summary>
  42.       /// Tests this instance.
  43.       /// </summary>
  44.       [CommandMethod("TEST")]
  45.       public static void Test()
  46.       {
  47.          var doc = CadApp.DocumentManager.MdiActiveDocument;
  48.          var db = doc.Database;
  49.          var ed = doc.Editor;
  50.  
  51.          ed.WriteMessage("Initial default Test");
  52.       }
  53.    }
  54. }
  55.  
  56.  


« Last Edit: August 19, 2023, 08:58:13 PM by kdub_nz »
Called Kerry in my other life
Retired; but they dragged me back in !

I live at UTC + 13.00

---
some people complain about loading the dishwasher.
Sometimes the question is more important than the answer.

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8753
  • AKA Daniel
Re: ContextMenu
« Reply #1 on: August 19, 2023, 10:09:39 PM »
analog to AcEdUIContext?

docs
Quote
There is no limit on the number of menu items, but items should contain only text.
I tried when wrapping this https://www.theswamp.org/index.php?topic=58367.0

kdub_nz

  • Mesozoic keyThumper
  • SuperMod
  • Water Moccasin
  • Posts: 2145
  • class keyThumper<T>:ILazy<T>
Re: ContextMenu
« Reply #2 on: August 19, 2023, 10:23:26 PM »
analog to AcEdUIContext?

docs
Quote
There is no limit on the number of menu items, but items should contain only text.
I tried when wrapping this https://www.theswamp.org/index.php?topic=58367.0

Well that's incorrect.
I've added Icons to the MenuItems.
The issue is when there are child menu's (MenuItems) the 'Title' takes the place of the Parent  item.

But it's not really important, just annoying.
Called Kerry in my other life
Retired; but they dragged me back in !

I live at UTC + 13.00

---
some people complain about loading the dishwasher.
Sometimes the question is more important than the answer.

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8753
  • AKA Daniel
Re: ContextMenu
« Reply #3 on: August 19, 2023, 10:31:34 PM »
I couldn’t get it to work for any menu item... but I see you used a resource, I didn’t try that

kdub_nz

  • Mesozoic keyThumper
  • SuperMod
  • Water Moccasin
  • Posts: 2145
  • class keyThumper<T>:ILazy<T>
Re: ContextMenu
« Reply #4 on: August 19, 2023, 10:40:35 PM »
This is/was  auto-generated in Resources.Designer.cs after I added the 16*16 icon

Code - C#: [Select]
  1.       /// <summary>
  2.       ///   Looks up a localized resource of type System.Drawing.Icon similar to (Icon).
  3.       /// </summary>
  4.       internal static System.Drawing.Icon bug_edit_icon
  5.       {
  6.          get
  7.          {
  8.             object obj = ResourceManager.GetObject("bug_edit_icon", resourceCulture);
  9.             return ((System.Drawing.Icon)(obj));
  10.          }
  11.       }
  12.  
  13.  


which allowed this (where the icon is an System.Drawing.Icon Type):
Code - C#: [Select]
  1.    var icon = PrivateTesting.Properties.Resources.bug_edit_icon;
  2.  
Called Kerry in my other life
Retired; but they dragged me back in !

I live at UTC + 13.00

---
some people complain about loading the dishwasher.
Sometimes the question is more important than the answer.

kdub_nz

  • Mesozoic keyThumper
  • SuperMod
  • Water Moccasin
  • Posts: 2145
  • class keyThumper<T>:ILazy<T>
Re: ContextMenu
« Reply #5 on: August 19, 2023, 10:47:08 PM »
The main reason I wanted an Icon on the Title Text was to make it stand out.

I'll probably just use a prefix something like  "**??**" for the title.

added image:

« Last Edit: August 19, 2023, 10:50:33 PM by kdub_nz »
Called Kerry in my other life
Retired; but they dragged me back in !

I live at UTC + 13.00

---
some people complain about loading the dishwasher.
Sometimes the question is more important than the answer.

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8753
  • AKA Daniel
Re: ContextMenu
« Reply #6 on: August 19, 2023, 11:09:40 PM »
ascii art  :laugh:

kdub_nz

  • Mesozoic keyThumper
  • SuperMod
  • Water Moccasin
  • Posts: 2145
  • class keyThumper<T>:ILazy<T>
Re: ContextMenu
« Reply #7 on: August 19, 2023, 11:11:40 PM »
I recall seeing ascii art of a man shrugging on someones signature . . I may steal that   :wink:

« Last Edit: August 19, 2023, 11:17:03 PM by kdub_nz »
Called Kerry in my other life
Retired; but they dragged me back in !

I live at UTC + 13.00

---
some people complain about loading the dishwasher.
Sometimes the question is more important than the answer.

kdub_nz

  • Mesozoic keyThumper
  • SuperMod
  • Water Moccasin
  • Posts: 2145
  • class keyThumper<T>:ILazy<T>
Re: ContextMenu
« Reply #8 on: August 20, 2023, 06:27:14 PM »
Rethink time last night !

The Goal was to make a simple black-box ContextMenu engine,

So, I've decided to remove the Icon implementation.
This will leave me with a simple library class I can use by making a single call with a bit of data from the initializer for the testing class.

No weird dependencies, no modification,  no baggage, no resource Library,  easy to use, simple to explain.
and usable with .net versions from Framework4.8 to  .NET8,
Job done.
Called Kerry in my other life
Retired; but they dragged me back in !

I live at UTC + 13.00

---
some people complain about loading the dishwasher.
Sometimes the question is more important than the answer.

Jeff H

  • Needs a day job
  • Posts: 6150
Re: ContextMenu
« Reply #9 on: August 21, 2023, 08:45:41 AM »
Thanks Kerry,
Its been great having you more active again!

BlackBox

  • King Gator
  • Posts: 3770
Re: ContextMenu
« Reply #10 on: August 21, 2023, 03:32:06 PM »
The Goal was to make a simple black-box ContextMenu engine,

As long as you don't also consider me simplistic, I'll take it - Ous!



I have a 3yo; don't judge me. Haha :beer:
"How we think determines what we do, and what we do determines what we get."

kdub_nz

  • Mesozoic keyThumper
  • SuperMod
  • Water Moccasin
  • Posts: 2145
  • class keyThumper<T>:ILazy<T>
Re: ContextMenu
« Reply #11 on: August 21, 2023, 04:35:24 PM »
thanks guys,

this was a result of not being able to remember 5 or 6 test command names the day after I've written them.

necessity is the mother !

and BB, no, far from simple :)

 
Called Kerry in my other life
Retired; but they dragged me back in !

I live at UTC + 13.00

---
some people complain about loading the dishwasher.
Sometimes the question is more important than the answer.

Atook

  • Swamp Rat
  • Posts: 1030
  • AKA Tim
Re: ContextMenu
« Reply #12 on: August 21, 2023, 10:27:38 PM »
Kerry, have you tried an emoji?

I'm using them for palette titles, they might work in the context menu.

win+. should bring up an emoji picker..

kdub_nz

  • Mesozoic keyThumper
  • SuperMod
  • Water Moccasin
  • Posts: 2145
  • class keyThumper<T>:ILazy<T>
Re: ContextMenu
« Reply #13 on: August 21, 2023, 11:49:51 PM »
WOW !! that thought came from another box altogether. Well done Tim

Thanks :)
« Last Edit: August 22, 2023, 12:02:43 AM by kdub_nz »
Called Kerry in my other life
Retired; but they dragged me back in !

I live at UTC + 13.00

---
some people complain about loading the dishwasher.
Sometimes the question is more important than the answer.

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8753
  • AKA Daniel
Re: ContextMenu
« Reply #14 on: August 22, 2023, 01:11:23 AM »
Ah! that's cool!