处理文档事件
 
 

Document object events are used to respond to the document window. When a document event is registered, it is only associated with the document object in which it is associated. So if an event needs to be registered with each document, you will want to use the DocumentCreated event of the DocumentCollection object to register events with each new or opened drawing.

文档对象事件用于响应文档窗口。当文档事件被注册后,它仅关联与之关联的文档对象。所以如果一个事件需要和每个文档注册,就应该使用 DocumentCollection 对象的 DocumentCreated 事件注册事件给每个新的或打开的图形。

The following events are available for Document objects:

下列事件可用于文档对象:

BeginDocumentClose

Triggered just after a request is received to close a drawing.

仅在收到关闭图形的请求后触发。

CloseAborted

Triggered when an attempt to close a drawing is aborted.

当尝试终止关闭图形时触发。

CloseWillStart

Triggered after the BeginDocumentClose event and before closing the drawing begins.

在 BeginDocumentClose 事件后,开始关闭图形前触发。

CommandCancelled

Triggered when a command is cancelled before it completes.

当命令在完成前被取消时触发。

CommandEnded

Triggered immediately after a command completes.

命令完成后马上触发。

CommandFailed

Triggered when a command fails to complete and is not cancelled.

当命令未能完成并没有取消时触发。

CommandWillStart

Triggered immediately after a command is issued, but before it completes.

命令发出后,在它完成前马上触发。

ImpliedSelectionChanged

Triggered when the current pickfirst selection set changes.

当当前 Pickfirst 选择集设置修改时触发。

LispCancelled

Triggered when the evaluation of a LISP expression is canceled.

当 LISP 表达式计算取消时触发。

LispEnded

Triggered upon completion of evaluating a LISP expression.

在 LISP 表达式计算完成时触发。

LispWillStart

Triggered immediately after AutoCAD receives a request to evaluate a LISP expression.

当 AutoCAD 接收到 LISP 表达式的计算请求后马上触发。

UnknownCommand

Triggered immediately when an unknown command is entered at the Command prompt.

当在命令提示符中输入未知命令时马上触发。

启动文档对象事件

The following example uses the BeginDocumentClose event to prompt the user if they want to continue closing the current drawing. A message box is displayed with the Yes and No buttons. Clicking No aborts the closing of the drawing by using the Veto method of the arguments that are returned by the event handler.

本例使用 BeginDocumentClose 事件提示用户是否想继续关闭当前图形。一个有 Yes 和 No 按扭的消息框会被显示。点击 No 后使用事件处理程序返回的参数的 Veto 方法终止文档的关闭。

VB.NET

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

C#

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();
  }
}
VBA/ActiveX 代码参考