控制应用程序窗口
 
 

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 窗口最小化,例如此时代码正在其他像 Microsoft® 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 对象的方法和属性可以更改“应用程序”窗口的位置、大小和可见性。另外,还可以使用 WindowState 属性来最小化、最大化“应用程序”窗口以及检查窗口的当前状态。

更改“应用程序”窗口的位置和大小

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 像素。

NoteThe 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) 库到工程中。使用添加引用(Add Reference) 对话框并从 .NET 选项卡中选择 PresentationCore。

VB.NET

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

C#

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 代码参考

最小化和最大化“应用程序”窗口

NoteThe 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) 库到工程中。使用添加引用(Add Reference) 对话框并从 .NET 选项卡中选择 PresentationCore。

VB.NET

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", MsgBoxStyle.SystemModal, "MinMax")
End Sub

C#

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

检索“应用程序”窗口的当前状态

This example queries the state of the Application window and displays the state in a message box to the user.

本例查询“应用程序”窗口的状态,并将该状态以消息框的形式显示给用户。

VB.NET

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

C#

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

使“应用程序”窗口隐藏和显示

The following code uses the Visible property to make the AutoCAD application invisible and then visible again.

下面的代码使用 Visible 属性使 AutoCAD 应用程序对最终用户不可见,然后再显示出来。

VB.NET

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", MsgBoxStyle.SystemModal, "Show/Hide")
 
  ''显示应用程序窗口  Show the Application window
  Application.MainWindow.Visible = True
  MsgBox("Visible", MsgBoxStyle.SystemModal, "Show/Hide")
End Sub

C#

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