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.
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.
Position and size the Application window
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.
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;
}
Minimize and maximize the Application window
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
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");
}
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");
}
Make the Application window invisible and visible
The following code uses the Visible property to make the AutoCAD application invisible and then visible again.
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
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");
}