COM ベースのイベントを登録する(.NET)

AutoCAD COM オートメーション ライブラリには、.NET API にはない固有のイベントがいくつかあります。COM ライブラリに含まれるイベントを登録することは、VB または VBA を使用してイベントを初期化することとは異なります。イベント ハンドラをイベントに登録するには、VB.NET の AddHandler ステートメントまたは C# の += 演算子を使用します。イベント ハンドラには、イベントが発生したときに呼び出されるプロシージャのアドレスが必要です。

COM ベースのイベントを登録する

次の例では、COM 相互運用機能を使用して BeginFileDrop イベントを登録する方法を示します。BeginFileDrop イベントは、AutoCAD COM オートメーション ライブラリの Application オブジェクトと関連付けられます。コマンドが AutoCAD にロードされた後、コマンド プロンプトで AddCOMEvent を入力し、DWG ファイルを図面ウィンドウにドラッグしてドロップします。メッセージ ボックスに続行を問い合わせるメッセージが表示されます。イベント ハンドラを削除するには、RemoveCOMEvent コマンドを使用します。

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;
  }
}

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

VBA/ActiveX コード リファレンス

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