Document オブジェクト イベントは、ドキュメント ウィンドウへの応答に使用されます。登録されたドキュメント イベントは、関連付けられているドキュメント オブジェクトとのみ関連付けられます。したがって、各ドキュメントにイベントを登録する必要がある場合は、DocumentCollection オブジェクトの DocumentCreated イベントを使用して、新しい図面または開かれた図面ごとにイベントを登録します。
Document オブジェクトでは次のイベントを使用できます。
図面を閉じる要求を受け取ったときに発生します。
図面を開く直前に発生します。
図面を閉じる操作が中止されると発生します。
BeginDocumentClose イベントの後で、図面を閉じる操作が始まる前に、発生します。
コマンドが完了する前にキャンセルされると発生します。
コマンドが完了した直後に発生します。
コマンドの完了に失敗し、キャンセルされないと発生します。
コマンドを発行した直後で、そのコマンドの処理が完了する前に発生します。
図面が開かれたときに発生します。
現在の PickFirst (編集コマンド選択前にクリック)の選択セットが変更された時点で発生します。
レイアウトを現在のレイアウトに設定した後に発生します。
レイアウトを現在のレイアウトに設定しているときに発生します。
LISP 式の評価がキャンセルされたときに発生します。
LISP 式の評価完了時に発生します。
AutoCAD が LISP 式の評価要求を受けた直後に発生します。
コマンド プロンプトで不明のコマンドが入力された直後に発生します。
図面のビューが変更されると発生します。
次の例では、BeginDocumentClose イベントを使用して、現在の図面を閉じる操作を続行するかどうかをユーザに問い合わせます。[Yes]および[No]ボタンを含むメッセージ ボックスが表示されます。[No]がクリックされると、イベント ハンドラによって返される引数の Veto メソッドを使用して、図面を閉じる操作を中止します。
Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
<CommandMethod("AddDocEvent")> _
Public Sub AddDocEvent()
'' Get the current document
Dim acDoc As Document = Application.DocumentManager.MdiActiveDocument
AddHandler acDoc.BeginDocumentClose, AddressOf docBeginDocClose
End Sub
<CommandMethod("RemoveDocEvent")> _
Public Sub RemoveDocEvent()
'' Get the current document
Dim acDoc As Document = Application.DocumentManager.MdiActiveDocument
RemoveHandler acDoc.BeginDocumentClose, AddressOf docBeginDocClose
End Sub
Public Sub docBeginDocClose(ByVal senderObj As Object, _
ByVal docBegClsEvtArgs As DocumentBeginCloseEventArgs)
'' Display a message box prompting to continue closing the document
If System.Windows.Forms.MessageBox.Show( _
"The document is about to be closed." & _
vbLf & "Do you want to continue?", _
"Close Document", _
System.Windows.Forms.MessageBoxButtons.YesNo) = _
System.Windows.Forms.DialogResult.No Then
docBegClsEvtArgs.Veto()
End If
End If
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
[CommandMethod("AddDocEvent")]
public void AddDocEvent()
{
// Get the current document
Document acDoc = Application.DocumentManager.MdiActiveDocument;
acDoc.BeginDocumentClose +=
new DocumentBeginCloseEventHandler(docBeginDocClose);
}
[CommandMethod("RemoveDocEvent")]
public void RemoveDocEvent()
{
// Get the current document
Document acDoc = Application.DocumentManager.MdiActiveDocument;
acDoc.BeginDocumentClose -=
new DocumentBeginCloseEventHandler(docBeginDocClose);
}
public void docBeginDocClose(object senderObj,
DocumentBeginCloseEventArgs docBegClsEvtArgs)
{
// Display a message box prompting to continue closing the document
if (System.Windows.Forms.MessageBox.Show(
"The document is about to be closed." +
"\nDo you want to continue?",
"Close Document",
System.Windows.Forms.MessageBoxButtons.YesNo) ==
System.Windows.Forms.DialogResult.No)
{
docBegClsEvtArgs.Veto();
}
}
Private Sub AcadDocument_BeginDocClose(Cancel As Boolean)
' This procedure intercepts the Document BeginDocClose event.
If MsgBox("The document is about to be closed." & _
vbLf & "Do you want to continue?", vbYesNo, _
"Close Document") = vbNo Then
' Veto the document close
Cancel = True
End If
End Sub