Kean Walmsley

July 2009

Sun Mon Tue Wed Thu Fri Sat
      1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31  

Twitter Updates

    follow me on Twitter



    « Creating a custom AutoCAD table style using .NET | Main | Pitching .NET to a hardcore C++ developer »

    November 05, 2008

    Displaying a context menu during a custom AutoCAD command using .NET

    Some of you may have stumbled across these previous posts, which show how to add custom context menu items for specific object types and to the default AutoCAD context menu. There is a third way to create and display context menus inside AutoCAD, and this approach may prove useful to those of you who wish to display context menus during particular custom commands.

    One word of caution: I've been told that this technique does not currently work for transparent commands, so if your command needs to be called transparently then this may not be the approach for you (you should investigate asking for keywords, instead, as this should work without problem in this context).

    Here's the C# code:

    using Autodesk.AutoCAD.ApplicationServices;

    using Autodesk.AutoCAD.EditorInput;

    using Autodesk.AutoCAD.Runtime;

    using Autodesk.AutoCAD.Windows;

    using System;


    namespace CommandContextMenu

    {

      public class Commands

      {

        public class MyContextMenu : ContextMenuExtension

        {

          public MyContextMenu()

          {

            this.Title = "Command context menu";


            MenuItem mi = new MenuItem("Item One");

            mi.Click +=

              new EventHandler(Commands.OnClick);

            this.MenuItems.Add(mi);


            mi = new MenuItem("Item Two");

            mi.Click +=

              new EventHandler(Commands.OnClick);

            this.MenuItems.Add(mi);


            MenuItem smi = new MenuItem("Sub Item One");

            smi.Click +=

              new EventHandler(Commands.OnClick);

            this.MenuItems.Add(smi);

          }

        };


        [CommandMethod(

          "mygroup",

          "mycmd",

          null,

          CommandFlags.Modal,

          typeof(MyContextMenu)

        )]

        public static void MyCommand()

        {

          Editor ed =

            Application.DocumentManager.MdiActiveDocument.Editor;

          ed.GetPoint("\nRight-click before selecting a point:");

        }


        static void OnClick(object sender, EventArgs e)

        {

          Editor ed =

            Application.DocumentManager.MdiActiveDocument.Editor;

          ed.WriteMessage("\nA context menu item was selected.");

        }

      }

    }

    A couple of points about getting this working: on my system I used AutoCAD's OPTIONS command to make sure the right-click menu gets displayed after a certain delay (I enabled the option from the "User Preferences" -> "Right-Click Customization" and then changed the longer click duration to 100 milliseconds, to save a little time. :-)

    AutoCAD options for right-click menu display

    The other option for enabling command menus is the SHORTCUTMENU system variable (the documentation inside AutoCAD will tell you about the various values).

    Then, once inside the MYCMD command, I was able to right-click and see my custom menu:

    Custom command right-click menu

    From the command-line, we can see that our callback was called successfully:

    Command:  MYCMD

    Right-click before selecting a point:

    A context menu item was selected.

    If you wish to have separate callbacks for your various items, it's simply a matter of defining separate functions and passing them in as the Click event handler instead of Command.OnClick.

    TrackBack

    TrackBack URL for this entry:
    http://www.typepad.com/services/trackback/6a00d83452464869e2010535d1e345970b

    Listed below are links to weblogs that reference Displaying a context menu during a custom AutoCAD command using .NET:

    Comments

    Hi Kean,

    Not to pick on you, but I was just responding to a post on the discussion groups about this post, and I noticed this:

    MenuItem smi = new MenuItem("Sub Item One");
    smi.Click +=
    new EventHandler(Commands.OnClick);
    this.MenuItems.Add(smi);

    should be MI.MenuItems.Add(smi);

    If you actually want it to be a 'Sub Item'

    Also, when I did a subitem, I didn't add a handler to the parent item, and it turns out (I tested it) that the menuitem which is the parent of subitems did not fire an event even though one was added.

    If you make a parent item, it probably only behaves as a parent and no longer fires its own events.

    Thanks for the note, David.

    Kean

    Verify your Comment

    Previewing your Comment

    This is only a preview. Your comment has not yet been posted.

    Working...
    Your comment could not be posted. Error type:
    Your comment has been posted. Post another comment

    The letters and numbers you entered did not match the image. Please try again.

    As a final step before posting your comment, enter the letters and numbers you see in the image below. This prevents automated programs from posting comments.

    Having trouble reading this image? View an alternate.

    Working...

    Post a comment

    Feed & Share

    Search