AutoCAD always starts up with a new or existing document open. It is possible, however, to close all documents during the current session.
AutoCAD 通常在启动时会打开一个新的或现有文档。但也可以关闭当前任务中的所有文档。
If you close all the documents in the AutoCAD user interface, you will notice a few changes to the application window. The Quick Access toolbar and application menu offer limited options. These limited options are related to creating and opening drawings, displaying the Sheet Set Manager, and recovering drawings. If the menu bar is displayed, simplified File, View, Window, and Help menus are also displayed. You will also notice that there is no command line.
如果关闭 AutoCAD 用户界面中的所有文档,应用程序窗口将发生一些改变。快速访问工具栏和应用程序菜单将只出现可用的选项。并且这些可用选项只与创建和打开图形、显示图纸集管理和恢复图形有关。如果显示了菜单栏,也只简单的显示“文件”、“视图”、“窗口”和“帮助”菜单。此外还会注意到没有显示命令行。
When working in zero document state, you can do the following:
在零文档状态中工作,只允许执行以下操作:
To react to AutoCAD when it enters zero document state, you should use the DocumentDestroyed event. The DocumentDestroyed event is triggered when an open document is closed. The document count when the last document is closed will be 1. Use the Count property of the DocumentManager to determine the number of open documents at the time the DocumentDestroyed event is triggered.
当进入零文档状态时若要与 AutoCAD 起反应,应该使用 DocumentDestroyed 事件。DocumentDestroyed 事件在一个打开的文档关闭时触发。文档数在最后一个文档被关闭时为1。当 DocumentDestroyed 事件触发时使用 DocumentManager 的 Count 属性可以确定打开文档的数量。
For more information on the using events in AutoCAD, see Use Events.
更多关于使用 AutoCAD 事件的详细信息,请参见 使用事件 部分。
This example code uses the DocumentDestroyed event to monitor when the last drawing is closed and when zero document state is entered. Once zero document state is entered, the Opening event is registered with the application menu. When the application menu is clicked, the Opening event is triggered. During the Opening event, a new menu item is added to the application menu. The new menu item displays a message box.
本例代码使用 DocumentDestroyed 事件监视什么时候最后文档关闭和什么时候进入零文档状态。一旦进入零文档状态,应用程序菜单的 Opening 事件将被注册。当应用程序菜单被点击时,Opening 事件将被触发。在 Opening 事件中,一个新的菜单项将添加到应用程序菜单中。新菜单项将显示一个消息框。
NoteYou must reference AdWindows.dll to your project in order to use the following example code. AdWindows.dll contains the namespace used to customize the application menu and can be found in the install folder of AutoCAD or part of the ObjectARX SDK. You will also need to reference WindowsBase which can be found on the .NET tab of the Add Reference dialog box.
注意为了使用下面的示例代码用户必须引用 AdWindows.dll 到工程。 AdWindows.dll 包含用于定义应用程序菜单的命名空间,它可以在 AutoCAD 的安装文件夹或 ObjectARX SDK 部件中找到。同样也需要引用 WindowsBase,它可以在添加引用对话框的 .NET 标签中找到。
Imports System.Windows.Input
Imports Autodesk.Windows
Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
'' 创建用于自定义应用程序菜单项的命令处理程序 Create the command handler for the custom application menu item
Public Class MyCommandHandler
Implements ICommand
Event CanExecuteChanged(ByVal sender As Object, ByVal e As EventArgs) _
Implements ICommand.CanExecuteChanged
Function CanExecute(ByVal parameter As Object) As Boolean _
Implements ICommand.CanExecute
Return True
End Function
Sub Execute(ByVal parameter As Object) Implements ICommand.Execute
Application.ShowAlertDialog("MyMenuItem has been clicked")
End Sub
End Class
Public Class Chapter4
''用于 ZeroDocState 的全局变量 Global var for ZeroDocState
Dim acApMenuItem As ApplicationMenuItem = Nothing
<CommandMethod("AddZeroDocEvent")> _
Public Sub AddZeroDocEvent()
'' 获得 DocumentCollection 并注册 DocumentDestroyed 事件 Get the DocumentCollection and register the DocumentDestroyed event
Dim acDocMgr As DocumentCollection = Application.DocumentManager
AddHandler acDocMgr.DocumentDestroyed, AddressOf docDestroyed
End Sub
Public Sub docDestroyed(ByVal obj As Object, _
ByVal acDocDesEvtArgs As DocumentDestroyedEventArgs)
'' 判断菜单项是否已经存在和打开文档的数量 Determine if the menu item already exists and the number of documents open
If Application.DocumentManager.Count = 1 And IsNothing(acApMenuItem) Then
'' Add the event handler to watch for when the application menu is opened
'' AdWindows.dll 必须引用到工程中 AdWindows.dll must be referenced to the project
AddHandler ComponentManager.ApplicationMenu.Opening, _
AddressOf ApplicationMenu_Opening
End If
End Sub
Public Sub ApplicationMenu_Opening(ByVal sender As Object, _
ByVal e As EventArgs)
'' 检查以确定自定义菜单项前面是否已经添加 Check to see if the custom menu item was added previously
If IsNothing(acApMenuItem) Then
'' 获得应用程序菜单组件 Get the application menu component
Dim acApMenu As ApplicationMenu = ComponentManager.ApplicationMenu
'' 创建新的应用程序菜单项 Create a new application menu item
acApMenuItem = New ApplicationMenuItem()
acApMenuItem.Text = "MyMenuItem"
acApMenuItem.CommandHandler = New MyCommandHandler()
'' 追加新的菜单项 Append the new menu item
acApMenu.MenuContent.Items.Add(acApMenuItem)
'' 移除应用程序菜单的 Opening 事件处理程序 Remove the application menu Opening event handler
RemoveHandler ComponentManager.ApplicationMenu.Opening, _
AddressOf ApplicationMenu_Opening
End If
End Sub
End Class
using Autodesk.Windows;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
// 创建用于自定义应用程序菜单项的命令处理程序 Create the command handler for the custom application menu item
public class MyCommandHandler : System.Windows.Input.ICommand
{
public bool CanExecute(object parameter)
{
return true;
}
public event EventHandler CanExecuteChanged;
public void Execute(object parameter)
{
Application.ShowAlertDialog("MyMenuItem has been clicked");
}
}
class Chapter4
{
//用于 ZeroDocState 的全局变量 Global var for ZeroDocState
ApplicationMenuItem acApMenuItem = null;
[CommandMethod("AddZeroDocEvent")]
public void AddZeroDocEvent()
{
// 获得 DocumentCollection 并注册 DocumentDestroyed 事件 Get the DocumentCollection and register the DocumentDestroyed event
DocumentCollection acDocMgr = Application.DocumentManager;
acDocMgr.DocumentDestroyed +=
new DocumentDestroyedEventHandler(docDestroyed);
}
public void docDestroyed(object obj,
DocumentDestroyedEventArgs acDocDesEvtArgs)
{
// 判断菜单项是否已经存在和打开文档的数量 Determine if the menu item already exists and the number of documents open
if (Application.DocumentManager.Count == 1 && acApMenuItem == null)
{
// Add the event handler to watch for when the application menu is opened
// AdWindows.dll 必须引用到工程中 AdWindows.dll must be referenced to the project
ComponentManager.ApplicationMenu.Opening +=
new EventHandler<EventArgs>(ApplicationMenu_Opening);
}
}
void ApplicationMenu_Opening(object sender, EventArgs e)
{
// 检查以确定自定义菜单项前面是否已经添加 Check to see if the custom menu item was added previously
if (acApMenuItem == null)
{
// 获得应用程序菜单组件 Get the application menu component
ApplicationMenu acApMenu = ComponentManager.ApplicationMenu;
// 创建新的应用程序菜单项 Create a new application menu item
acApMenuItem = new ApplicationMenuItem();
acApMenuItem.Text = "MyMenuItem";
acApMenuItem.CommandHandler = new MyCommandHandler();
// 追加新的菜单项 Append the new menu item
acApMenu.MenuContent.Items.Add(acApMenuItem);
// 移除应用程序菜单的 Opening 事件处理程序 Remove the application menu Opening event handler
ComponentManager.ApplicationMenu.Opening -=
new EventHandler<EventArgs>(ApplicationMenu_Opening);
}
}
}