The AutoCAD COM Automation library offers some unique events that are not found in the .NET API. Registering events that are in a COM library is different than how you would initialize an event using VB or VBA. You use the VB.NET AddHandler statement or the C# += operator to reigister an event handler with the event. The event handler requires the address of the procedure in which should be called when the event is raised.
AutoCAD COM Automation 库提供一些在 .NET API 中找不到的独特事件。在 COM 库中注册事件与使用 VB 或 VBA 如何初始化一个事件不同。用户使用 VB.NET 的 AddHandler 语句或 C# 的 += 运算符向事件注册事务处理程序。事件处理程序需要事件被触发时应该被调用的过程的地址。
This example demonstrates how to register the BeginFileDrop event using COM interop. The BeginFileDrop event is associated with the Application object of the AutoCAD COM Automation library. Once the commands are loaded into AutoCAD, enter AddCOMEvent at the Command prompt and then drag and drop a DWG file into the drawing window. A message box will be displayed prompting you to continue. Use the RemoveCOMEvent command to remove the event handler.
本例演示如何使用 COM interop 注册 BeginFileDrop 事件。BeginFileDrop 事件是和 AutoCAD COM Automation 库的 Application 对象相关联。一旦命令被加载到 AutoCAD 中后,在命令提示符中输入 AddCOMEvent,然后拖放一个 DWG 文件到图形窗口中。将显示一个消息框并提示你继续。使用 RemoveCOMEvent 命令移除事件处理程序。
Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.Interop
Imports Autodesk.AutoCAD.Interop.Common
'' 用于 COMEvent 和 RemoveCOMEvent 命令的全局变量 Global variable for AddCOMEvent and RemoveCOMEvent commands
Dim acAppCom As AcadApplication
<CommandMethod("AddCOMEvent")> _
Public Sub AddCOMEvent()
'' 设置保存 Application 引用的全局变量并注册 BeginFileDrop 事件 Set the global variable to hold a reference to the application and
'' register the BeginFileDrop COM event
acAppCom = Application.AcadApplication
AddHandler acAppCom.BeginFileDrop, AddressOf appComBeginFileDrop
End Sub
<CommandMethod("RemoveCOMEvent")> _
Public Sub RemoveCOMEvent()
'' 解除注册 COM 事件处理程序 Unregister the COM event handle
RemoveHandler acAppCom.BeginFileDrop, AddressOf appComBeginFileDrop
acAppCom = Nothing
End Sub
Public Sub appComBeginFileDrop(ByVal strFileName As String, _
ByRef bCancel As Boolean)
'' 显示一个消息框并提示继续插入 DWG 文件 Display a message box prompting to continue inserting the DWG file
If System.Windows.Forms.MessageBox.Show("AutoCAD is about to load " & _
strFileName & vbLf & _
"Do you want to continue loading this file?", _
"DWG File Dropped", _
System.Windows.Forms.MessageBoxButtons.YesNo) = _
System.Windows.Forms.DialogResult.No Then
bCancel = True
End If
End Sub
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Interop;
using Autodesk.AutoCAD.Interop.Common;
// 用于 COMEvent 和 RemoveCOMEvent 命令的全局变量 Global variable for AddCOMEvent and RemoveCOMEvent commands
AcadApplication acAppCom;
[CommandMethod("AddCOMEvent")]
public void AddCOMEvent()
{
// 设置保存 Application 引用的全局变量并注册 BeginFileDrop 事件 Set the global variable to hold a reference to the application and
// register the BeginFileDrop COM event
acAppCom = Application.AcadApplication as AcadApplication;
acAppCom.BeginFileDrop +=
new _DAcadApplicationEvents_BeginFileDropEventHandler(appComBeginFileDrop);
}
[CommandMethod("RemoveCOMEvent")]
public void RemoveCOMEvent()
{
// 解除注册 COM 事件处理程序 Unregister the COM event handle
acAppCom.BeginFileDrop -=
new _DAcadApplicationEvents_BeginFileDropEventHandler(appComBeginFileDrop);
acAppCom = null;
}
public void appComBeginFileDrop(string strFileName, ref bool bCancel)
{
// 显示一个消息框并提示继续插入 DWG 文件 Display a message box prompting to continue inserting the DWG file
if (System.Windows.Forms.MessageBox.Show("AutoCAD is about to load " + strFileName +
"\nDo you want to continue loading this file?",
"DWG File Dropped",
System.Windows.Forms.MessageBoxButtons.YesNo) ==
System.Windows.Forms.DialogResult.No)
{
bCancel = true;
}
}