CAD开发者社区

 找回密码
 立即注册

QQ登录

只需一步,快速开始

ObjectARX 开发指南

不使用打开的文档 (.NET)

2023-1-1 15:55| 发布者: admin| 查看: 532| 评论: 0|来自: AutoCAD

AutoCAD 始终在打开新文档或现有文档时启动。但是,可以在本届会议期间关闭所有文件。

如果关闭 AutoCAD 用户界面中的所有文档,您会注意到应用程序窗口发生了一些更改。快速访问工具栏和应用程序菜单提供有限的选项。这些受限选项与创建和打开图形、显示图纸集管理器以及恢复图形有关。如果显示菜单栏,则还会显示简化的“文件”、“视图”、“窗口”和“帮助”菜单。您还会注意到没有命令行。

在零文档状态下工作时,可以执行以下操作:

  • 您可以创建新文档或打开现有文档
  • 您可以自定义应用程序菜单和菜单栏的零文档状态
  • 您可以关闭AutoCAD

要在 AutoCAD 进入零文档状态时对其进行响应,应使用事件。关闭打开的文档时触发该事件。关闭最后一个文档时的文档计数将为 1。使用 的属性来确定触发事件时打开的文档数。DocumentDestroyedDocumentDestroyedCountDocumentManagerDocumentDestroyed

自定义应用程序菜单

此示例代码使用 theevent 来监视何时关闭最后一个图形以及何时输入零文档状态。进入零文档状态后,事件将注册到应用程序菜单。单击应用程序菜单时,将触发事件。在事件期间,一个新的菜单项将添加到应用程序菜单中。新菜单项将显示一个消息框。DocumentDestroyedOpeningOpeningOpening

注意:必须将AdWindows.dll引用到项目中才能使用以下示例代码。AdWindows.dll包含用于自定义应用程序菜单的命名空间,可以在AutoCAD的安装文件夹或ObjectARX SDK的一部分中找到。您还需要引用,可以在“添加引用”对话框的“AutoCAD .NET”选项卡上找到。WindowsBase

VB.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
    ''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

C#

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

路过

雷人

握手

鲜花

鸡蛋

最新评论

QQ|Archiver|CAD开发者社区 ( 苏ICP备2022047690号-1 )

GMT+8, 2024-5-19 15:03

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

返回顶部