AutoCAD: 添加鼠标快捷键/鼠标右键
Autodesk.AutoCAD.ApplicationServices.Application 支持两种 ContextMenu 扩展:DefaultContextMenu 和 ObjectContextMenu。
DefaultContextMenu:当前上下文环境没有选中任何 Entity 情况下的快捷菜单。
ObjectContextMenu:当前上下文环境选中指定类型 Entity 情况下的快捷菜单。如下图:
#region AddContextMenu
/// <summary>
/// 添加右键菜单
/// </summary>
private void AddContextMenu()
{
try
{
// DefaultContextMenu
ContextMenuExtension defaultContextMenu = new ContextMenuExtension();
defaultContextMenu.Title = "MyDefaultContextMenu";
Autodesk.AutoCAD.Windows.MenuItem defaultContextMenu_Item1 = new Autodesk.AutoCAD.Windows.MenuItem("MyDefaultContextMenu_Item1", ARX.UI.Resources.Resource1.taobao);
defaultContextMenu_Item1.Click += new EventHandler(defaultContextMenu_OnClick);
defaultContextMenu.MenuItems.Add(defaultContextMenu_Item1);
ArxApp.AddDefaultContextMenuExtension(defaultContextMenu);
// ObjectContextMenu
ContextMenuExtension objContextMenu = new ContextMenuExtension();
objContextMenu.Title = "MyObjectContextMenu";
objContextMenu.Popup += new EventHandler(objContextMenu_Popup);
Autodesk.AutoCAD.Windows.MenuItem objContextMenu_Item1 = new Autodesk.AutoCAD.Windows.MenuItem("Go to Baidu", ARX.UI.Resources.Resource1.baidu);
objContextMenu_Item1.Click += new EventHandler(objContextMenu_Item1_Click);
objContextMenu.MenuItems.Add(objContextMenu_Item1);
Autodesk.AutoCAD.Windows.MenuItem objContextMenu_Item2 = new Autodesk.AutoCAD.Windows.MenuItem("Go to Google", ARX.UI.Resources.Resource1.google);
objContextMenu_Item2.Click += new EventHandler(objContextMenu_Item2_Click);
objContextMenu.MenuItems.Add(objContextMenu_Item2);
ArxApp.AddObjectContextMenuExtension(RXObject.GetClass(typeof(Polyline)), objContextMenu);
}
catch (System.Exception exc)
{
WriteLine(string.Format("\n ContextMenu error: {0}", exc.Message));
}
}
void objContextMenu_Popup(object sender, EventArgs e)
{
using (DocumentLock docLock = ArxApp.DocumentManager.MdiActiveDocument.LockDocument())
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
bool visible = true;
//If that is multiple selection, disabled the menu item.
PromptSelectionResult selectionRes = ed.SelectImplied();
if (selectionRes.Status == PromptStatus.OK)
{
ObjectId[] objIds = selectionRes.Value.GetObjectIds();
if (objIds != null && objIds.Length > 1)
{
visible = false;
}
}
ContextMenuExtension objContextMenu = sender as ContextMenuExtension;
if (objContextMenu != null)
{
foreach (MenuItem item in objContextMenu.MenuItems)
{
item.Enabled = visible;
}
}
}
}
private void defaultContextMenu_OnClick(object sender, EventArgs e)
{
using (DocumentLock docLock = ArxApp.DocumentManager.MdiActiveDocument.LockDocument())
{
ArxApp.ShowAlertDialog("defaultContextMenu_OnClick");
}
}
void objContextMenu_Item1_Click(object sender, EventArgs e)
{
using (DocumentLock docLock = ArxApp.DocumentManager.MdiActiveDocument.LockDocument())
{
System.Diagnostics.Process.Start("IEXPLORE.EXE", "http://www.baidu.com");
}
}
void objContextMenu_Item2_Click(object sender, EventArgs e)
{
using (DocumentLock docLock = ArxApp.DocumentManager.MdiActiveDocument.LockDocument())
{
System.Diagnostics.Process.Start("IEXPLORE.EXE", "http://www.google.com");
}
}
#endregion
/// <summary>
/// 添加右键菜单
/// </summary>
private void AddContextMenu()
{
try
{
// DefaultContextMenu
ContextMenuExtension defaultContextMenu = new ContextMenuExtension();
defaultContextMenu.Title = "MyDefaultContextMenu";
Autodesk.AutoCAD.Windows.MenuItem defaultContextMenu_Item1 = new Autodesk.AutoCAD.Windows.MenuItem("MyDefaultContextMenu_Item1", ARX.UI.Resources.Resource1.taobao);
defaultContextMenu_Item1.Click += new EventHandler(defaultContextMenu_OnClick);
defaultContextMenu.MenuItems.Add(defaultContextMenu_Item1);
ArxApp.AddDefaultContextMenuExtension(defaultContextMenu);
// ObjectContextMenu
ContextMenuExtension objContextMenu = new ContextMenuExtension();
objContextMenu.Title = "MyObjectContextMenu";
objContextMenu.Popup += new EventHandler(objContextMenu_Popup);
Autodesk.AutoCAD.Windows.MenuItem objContextMenu_Item1 = new Autodesk.AutoCAD.Windows.MenuItem("Go to Baidu", ARX.UI.Resources.Resource1.baidu);
objContextMenu_Item1.Click += new EventHandler(objContextMenu_Item1_Click);
objContextMenu.MenuItems.Add(objContextMenu_Item1);
Autodesk.AutoCAD.Windows.MenuItem objContextMenu_Item2 = new Autodesk.AutoCAD.Windows.MenuItem("Go to Google", ARX.UI.Resources.Resource1.google);
objContextMenu_Item2.Click += new EventHandler(objContextMenu_Item2_Click);
objContextMenu.MenuItems.Add(objContextMenu_Item2);
ArxApp.AddObjectContextMenuExtension(RXObject.GetClass(typeof(Polyline)), objContextMenu);
}
catch (System.Exception exc)
{
WriteLine(string.Format("\n ContextMenu error: {0}", exc.Message));
}
}
void objContextMenu_Popup(object sender, EventArgs e)
{
using (DocumentLock docLock = ArxApp.DocumentManager.MdiActiveDocument.LockDocument())
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
bool visible = true;
//If that is multiple selection, disabled the menu item.
PromptSelectionResult selectionRes = ed.SelectImplied();
if (selectionRes.Status == PromptStatus.OK)
{
ObjectId[] objIds = selectionRes.Value.GetObjectIds();
if (objIds != null && objIds.Length > 1)
{
visible = false;
}
}
ContextMenuExtension objContextMenu = sender as ContextMenuExtension;
if (objContextMenu != null)
{
foreach (MenuItem item in objContextMenu.MenuItems)
{
item.Enabled = visible;
}
}
}
}
private void defaultContextMenu_OnClick(object sender, EventArgs e)
{
using (DocumentLock docLock = ArxApp.DocumentManager.MdiActiveDocument.LockDocument())
{
ArxApp.ShowAlertDialog("defaultContextMenu_OnClick");
}
}
void objContextMenu_Item1_Click(object sender, EventArgs e)
{
using (DocumentLock docLock = ArxApp.DocumentManager.MdiActiveDocument.LockDocument())
{
System.Diagnostics.Process.Start("IEXPLORE.EXE", "http://www.baidu.com");
}
}
void objContextMenu_Item2_Click(object sender, EventArgs e)
{
using (DocumentLock docLock = ArxApp.DocumentManager.MdiActiveDocument.LockDocument())
{
System.Diagnostics.Process.Start("IEXPLORE.EXE", "http://www.google.com");
}
}
#endregion
可以在 objContextMenu_Popup 事件中做一些逻辑处理。
下面是用到的namespace:

using Autodesk.AutoCAD.ApplicationServices;
using ArxApp = Autodesk.AutoCAD.ApplicationServices.Application;
using ArxDoc = Autodesk.AutoCAD.ApplicationServices.Document;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Windows;
using ArxApp = Autodesk.AutoCAD.ApplicationServices.Application;
using ArxDoc = Autodesk.AutoCAD.ApplicationServices.Document;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Windows;
分类:
AutoCAD.net
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架