Control the AutoCAD Environment
控制AutoCAD环境
This chapter describes the fundamentals for developing an application that runs in-process with AutoCAD. It explains many of the concepts to control and work effectively with the AutoCAD environment.
本章是开发AutoCAD进程内应用程序的基础,将学习到控制和使用AutoCAD环境的许多概念。
Topics in this section
本章主要内容
• Control the Application Window 控制应用程序窗口
• Control the Drawing Windows 控制图形窗口 Create,Open,Save,and Close Drawings 创建、打开、保存和关闭图形 Lock and Unlock a Document 锁定和解锁文档 Set AutoCAD Preferences 设置AutoCAD选项 Set and Return System Variables 设置和返回系统变量 Draw with Precision 精确制图 Prompt for User Input 提示用户输入 Access the AutoCAD Command Line访问AutoCAD命令行
Control the Application Window
控制应用程序窗口The ability to control the Application window allows developers the flexibility to create effective and intelligent applications. There will be times when it is appropriate for your application to minimize the AutoCAD window,perhaps while your code is performing work in another application such as Microsoft® Excel®. Additionally,you will often want to verify the state of the AutoCAD window before performing such tasks as prompting for input from the user.
控制AutoCAD应用程序窗口的能力让开发人员可以灵活地创建高效智能的应用程序。比如有时我们需要在程序中适时地最小化AutoCAD窗口,也许此时我们的代码正在使用其他应用程序如Excel处理任务。又比如,我们在执行像提示用户输入这样的任务前,经常需要确认AutoCAD窗口的状态。Using methods and properties found on the Application object,you can change the position,size,and visibility of the Application window. You can also use the WindowState property to minimize,maximize,and check the current state of the Application window.
使用Application对象的方法和属性,我们可以改变Application窗口的位置、大小及可见性,还可以用WindowState属性来最小化、最大化Application窗口,以及检查Application窗口的当前状态等。
This example uses the Location and Size properties to position the AutoCAD Application window in the upper-left corner of the screen and size it to 400 pixels wide by 400 pixels high.
本例使用Location属性和Size属性将AutoCAD应用窗口定位于屏幕左上角,并将窗口大小设置为400×400像素。
Note The following examples require that the PresentationCore (PresentationCore.dll) library to be referenced to the project. Use the Add Reference dialog Box and select PresentationCore from the .NET tab.
注意:下列示例需要在项目中引用PresentationCore库(PresentationCore.dll)。从添加引用对话框的.NET选项页中选PresentationCore即可。
Imports System.Drawing
Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
<CommandMethod("PositionApplicationWindow")> _
Public Sub PositionApplicationWindow()
'' Set the position of the Application window
Dim ptApp As Point = New Point(0,0)
Application.MainWindow.Location = ptApp
'' Set the size of the Application window
Dim szApp As Size = New Size(400,400)
Application.MainWindow.Size = szApp
End Sub
using System.Drawing;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
[CommandMethod("PositionApplicationWindow")]
public static void PositionApplicationWindow()
{
// Set the position of the Application window
// 设置应用程序窗口位置
Point ptApp = new Point(0,0);
Application.MainWindow.Location = ptApp;
// Set the size of the Application window
// 设置应用程序窗口大小
Size szApp = new Size(400,400);
Application.MainWindow.Size = szApp;
}
VBA/ActiveX Code Reference
Sub PositionApplicationWindow()
'' Set the position of the Application window
ThisDrawing.Application.WindowTop = 0
ThisDrawing.Application.WindowLeft = 0
'' Set the size of the Application window
ThisDrawing.Application.width = 400
ThisDrawing.Application.height = 400
End Sub
Imports System.Drawing
Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.Windows
<CommandMethod("MinMaxApplicationWindow")> _
Sub MinMaxApplicationWindow()
'' Minimize the Application window
Application.MainWindow.WindowState = Window.State.Minimized
MsgBox("Minimized",MsgBoxStyle.SystemModal,"MinMax")
'' Maximize the Application window
Application.MainWindow.WindowState = Window.State.Maximized
MsgBox("Maximized","MinMax")
End Sub
using System.Drawing;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.Windows;
[CommandMethod("MinMaxApplicationWindow")]
public static void MinMaxApplicationWindow()
{
// Minimize the Application window最小化应用程序窗口
Application.MainWindow.WindowState = Window.State.Minimized;
System.Windows.Forms.MessageBox.Show("Minimized","MinMax",
System.Windows.Forms.MessageBoxButtons.OK,
System.Windows.Forms.MessageBoxIcon.None,
System.Windows.Forms.MessageBoxDefaultButton.Button1,
System.Windows.Forms.MessageBoxOptions.ServiceNotification);
// Maximize the Application window最大化应用程序窗口
Application.MainWindow.WindowState = Window.State.Maximized;
System.Windows.Forms.MessageBox.Show("Maximized","MinMax");
}
Sub MinMaxApplicationWindow()
'' Minimize the Application window
ThisDrawing.Application.WindowState = acMin
MsgBox "Minimized"
'' Maximize the Application window
ThisDrawing.Application.WindowState = acMax
MsgBox "Maximized"
End Sub
Find the current state of the Application window 获取应用程序窗口当前状态This example queries the state of the Application window and displays the state in a message Box to the user.
Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.Windows
<CommandMethod("CurrentWindowState")> _
Public Sub CurrentWindowState()
System.Windows.Forms.MessageBox.Show("The application window is " + _
Application.MainWindow.WindowState.ToString(),_
"Window State")
End Sub
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.Windows;
[CommandMethod("CurrentWindowState")]
public static void CurrentWindowState()
{
System.Windows.Forms.MessageBox.Show("The application window is " +
Application.MainWindow.WindowState.ToString(),
"Window State");
}
Sub CurrentWindowState()
Dim CurrWindowState As Integer
Dim msg As String
CurrWindowState = ThisDrawing.Application.WindowState
msg = Choose(CurrWindowState,"Normal",_
"Minimized","Maximized")
MsgBox "The application window is " + msg
End Sub
Make the Application window invisible and visible 使应用程序窗口不可见和可见The following code uses the Visible property to make the AutoCAD application invisible and then visible again.
下列代码用Visible属性让AutoCAD应用程序窗口不可见,然后再可见。
Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.Windows
<CommandMethod("HideWindowState")> _
Public Sub HideWindowState()
'' Hide the Application window
Application.MainWindow.Visible = False
MsgBox("Invisible","Show/Hide")
'' Show the Application window
Application.MainWindow.Visible = True
MsgBox("Visible","Show/Hide")
End Sub
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.Windows;
[CommandMethod("HideWindowState")]
public static void HideWindowState()
{
// Hide the Application window隐藏应用程序窗口
Application.MainWindow.Visible = false;
System.Windows.Forms.MessageBox.Show("Invisible","Show/Hide");
// Show the Application window显示应用程序窗口
Application.MainWindow.Visible = true;
System.Windows.Forms.MessageBox.Show("Visible","Show/Hide");
}
Sub HideWindowState()
'' Hide the Application window
ThisDrawing.Application.Visible = False
MsgBox "Invisible"
'' Show the Application window
ThisDrawing.Application.Visible = True
MsgBox "Visible"
End Sub