CAD开发者社区

 找回密码
 立即注册

QQ登录

只需一步,快速开始

ObjectARX 开发指南

注册基于 COM 的事件 (.NET)

2023-1-1 09:00| 发布者: admin| 查看: 442| 评论: 0|来自: AutoCAD

AutoCAD COM 自动化库提供了一些在 .NET API 中找不到的独特事件。注册 COM 库中的事件不同于使用 VB 或 VBA 初始化事件的方式。您使用 VB。NET语句或 C# 运算符,用于使用事件重新注册事件处理程序。事件处理程序需要引发事件时应在其中调用的过程的地址。AddHandler+=

注册基于 COM 的事件

此示例演示如何使用 COM 互操作注册事件。该事件与 AutoCAD COM 自动化库的应用程序对象相关联。将命令加载到 AutoCAD 后,在命令提示下输入 AddCOMEvent,然后将 DWG 文件拖放到图形窗口中。将显示一个消息框,提示您继续。使用 RemoveCOMEvent 命令删除事件处理程序。BeginFileDropBeginFileDrop

VB.NET

Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
 
Imports Autodesk.AutoCAD.Interop
Imports Autodesk.AutoCAD.Interop.Common
 
'' Global variable for AddCOMEvent and RemoveCOMEvent commands
Dim acAppCom As AcadApplication
 
<CommandMethod("AddCOMEvent")> _
Public Sub AddCOMEvent()
  '' 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()
  '' 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)
  '' 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

C#

using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
 
using Autodesk.AutoCAD.Interop;
using Autodesk.AutoCAD.Interop.Common;
 
// Global variable for AddCOMEvent and RemoveCOMEvent commands
AcadApplication acAppCom;
 
[CommandMethod("AddCOMEvent")]
public void AddCOMEvent()
{
  // 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()
{
  // Unregister the COM event handle
  acAppCom.BeginFileDrop -= 
      new _DAcadApplicationEvents_BeginFileDropEventHandler(appComBeginFileDrop);
  acAppCom = null;
}
 
public void appComBeginFileDrop(string strFileName, ref bool bCancel)
{
  // 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;
  }
}

VBA/ActiveX Code Referemce

Public WithEvents ACADApp As AcadApplication

Sub Example_AcadApplication_Events()
    ' Intialize the public variable (ACADApp)
    ' which will be used to intercept AcadApplication Events
    '
    ' Run this procedure FIRST!
    Set ACADApp = ThisDrawing.Application
End Sub

Private Sub ACADApp_BeginFileDrop _
 (ByVal FileName As String, Cancel As Boolean)
    ' This procedure intercepts an Application BeginFileDrop event.
    '
    ' This event is triggered when a drawing file is dragged
    ' into AutoCAD.
    '
    ' To trigger this example event:
    '     1) Run the Example_AcadApplication_Events procedure to initialize
    '     the public variable (named ACADApp) linked to this event.
    '
    '     2) Drag an AutoCAD drawing file into the AutoCAD
    '        application from either the Windows Desktop
    '        or Windows Explorer
    '
    ' Use the "Cancel" variable to stop the loading of the
    ' dragged file, and the "FileName" variable to  notify
    ' the user which file is about to be dragged in.
 
    If MsgBox("AutoCAD is about to load " & FileName & vbCrLf _
              & "Do you want to continue loading this file?", _
              vbYesNoCancel + vbQuestion) <> vbYes Then
        Cancel = True
    End If
End Sub

路过

雷人

握手

鲜花

鸡蛋

最新评论

QQ|Archiver|CAD开发者社区 ( 苏ICP备2022047690号-1 )

GMT+8, 2024-5-19 12:25

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

返回顶部