AutoCAD は常に、新規ドキュメントまたは既存のドキュメントを開いて起動します。ただし、現在のセッションですべてのドキュメントを閉じてしまうことができます。
AutoCAD ユーザ インタフェースのドキュメントをすべて閉じると、アプリケーション ウィンドウの外観が少し変わります。クイック アクセス ツールバーおよびアプリケーション メニューのオプションが制限されます。制限されたオプションは、図面の作成とオープン、シート セット マネージャの表示、図面の修復に関連するものです。メニュー バーが表示される場合は、簡素化された[ファイル]、[ビュー]、[ウィンドウ]、[ヘルプ]メニューも表示されます。さらにコマンド ラインが表示されなくなります。
ドキュメントがない状態で操作するときは、次のことが実行できます。
ドキュメントがない状態になったときに AutoCAD に反応するには、DocumentDestroyed イベントを使用する必要があります。開いているドキュメントが閉じられたときに、documentdestroyed イベントが呼び出されます。最後のドキュメントが閉じられたときのドキュメント カウントは 1 になります。DocumentDestroyed イベントが呼び出された時点で開いているドキュメントの数を確認するには、DocumentManager の Count プロパティを使用します。
この例では、最後に図面を閉じたとき、およびドキュメントがない状態になったときを監視するために、DocumentDestroyed イベントが使用されます。ドキュメントがない状態になると、Opening イベントがアプリケーション メニューに登録されます。アプリケーション メニューをクリックすると、Opening イベントが呼び出されます。Opening イベント中に、新しいメニュー項目がアプリケーション メニューに追加されます。新しいメニュー項目にはメッセージ ボックスが表示されます。
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
''Global var for ZeroDocState
Dim acApMenuItem As ApplicationMenuItem = Nothing
<CommandMethod("AddZeroDocEvent")> _
Public Sub AddZeroDocEvent()
'' 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 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)
'' 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
{
//Global var for ZeroDocState
ApplicationMenuItem acApMenuItem = null;
[CommandMethod("AddZeroDocEvent")]
public void AddZeroDocEvent()
{
// 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 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);
// Remove the application menu Opening event handler
ComponentManager.ApplicationMenu.Opening -=
new EventHandler<EventArgs>(ApplicationMenu_Opening);
}
}
}