|
AutoCAD默认的菜单项中添加子菜单。当用户右击鼠标,且CAD中的对象处于未选择状态时,就会出现默认菜单。这也是个添加命令菜单的好地方。
这个方法与跟以前的很相似,尽管为了获取模块初始化时的反馈,我添加了几个额外的命令来添加和移除各个菜单。
几点需要注意的:
我们并不是简单的添加一个菜单项,而是We're not just adding a single menu item, but are using a cascading menu - three menu items underneath a main application menu item.
我们再次使用了 Autodesk.AutoCAD.Internal namespace 命令空间(需要饮用 AcMgdInternal.dll )中的 PostCommandPrompt() 函数。当然,如果通过发送字符串到Execute(),就不需要它了。
代码如下:
- using Autodesk.AutoCAD.ApplicationServices;
- using Autodesk.AutoCAD.DatabaseServices;
- using Autodesk.AutoCAD.EditorInput;
- using Autodesk.AutoCAD.Runtime;
- using Autodesk.AutoCAD.Windows;
- using Autodesk.AutoCAD.Internal;
- using System;
- namespace ContextMenuApplication
- {
- public class Commands : IExtensionApplication
- {
- public void Initialize()
- {
- CountMenu.Attach();
- ApplicationMenu.Attach();
- }
- public void Terminate()
- {
- CountMenu.Detach();
- ApplicationMenu.Detach();
- }
- [CommandMethod("ADDCONTEXT")]
- static public void AttachContextMenus()
- {
- CountMenu.Attach();
- ApplicationMenu.Attach();
- }
- [CommandMethod("NOCONTEXT")]
- static public void DetachContextMenus()
- {
- CountMenu.Detach();
- ApplicationMenu.Detach();
- }
- [CommandMethod("COUNT", CommandFlags.UsePickSet)]
- static public void CountSelection()
- {
- Editor ed =
- Application.DocumentManager.MdiActiveDocument.Editor;
- PromptSelectionResult psr = ed.GetSelection();
- if (psr.Status == PromptStatus.OK)
- {
- ed.WriteMessage(
- "\nSelected {0} entities.",
- psr.Value.Count
- );
- }
- }
- }
- public class CountMenu
- {
- private static ContextMenuExtension cme;
- public static void Attach()
- {
- if (cme == null)
- {
- cme = new ContextMenuExtension();
- MenuItem mi = new MenuItem("Count");
- mi.Click += new EventHandler(OnCount);
- cme.MenuItems.Add(mi);
- }
- RXClass rxc = Entity.GetClass(typeof(Entity));
- Application.AddObjectContextMenuExtension(rxc, cme);
- }
- public static void Detach()
- {
- RXClass rxc = Entity.GetClass(typeof(Entity));
- Application.RemoveObjectContextMenuExtension(rxc, cme);
- }
- private static void OnCount(Object o, EventArgs e)
- {
- Document doc =
- Application.DocumentManager.MdiActiveDocument;
- doc.SendStringToExecute("_.COUNT ", true, false, false);
- }
- }
- public class ApplicationMenu
- {
- private static ContextMenuExtension cme;
- public static void Attach()
- {
- if (cme == null)
- {
- cme = new ContextMenuExtension();
- cme.Title = "Kean's commands";
- MenuItem mi1 = new MenuItem("1st");
- mi1.Click += new EventHandler(On1st);
- cme.MenuItems.Add(mi1);
- MenuItem mi2 = new MenuItem("2nd");
- mi2.Click += new EventHandler(On2nd);
- cme.MenuItems.Add(mi2);
- MenuItem mi3 = new MenuItem("3rd");
- mi3.Click += new EventHandler(On3rd);
- cme.MenuItems.Add(mi3);
- }
- Application.AddDefaultContextMenuExtension(cme);
- }
- public static void Detach()
- {
- Application.RemoveDefaultContextMenuExtension(cme);
- }
- private static void On1st(Object o, EventArgs e)
- {
- Editor ed =
- Application.DocumentManager.MdiActiveDocument.Editor;
- ed.WriteMessage("\nFirst item selected.");
- Utils.PostCommandPrompt();
- }
- private static void On2nd(Object o, EventArgs e)
- {
- Editor ed =
- Application.DocumentManager.MdiActiveDocument.Editor;
- ed.WriteMessage("\nSecond item selected.");
- Utils.PostCommandPrompt();
- }
- private static void On3rd(Object o, EventArgs e)
- {
- Editor ed =
- Application.DocumentManager.MdiActiveDocument.Editor;
- ed.WriteMessage("\nThird item selected.");
- Utils.PostCommandPrompt();
- }
- }
- }
复制代码 |
|