Control the Application Window (.NET)

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.

Note: The following example requires 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.

C# Example

using System.Drawing;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
 
[CommandMethod("PositionApplicationWindow")]
public static void PositionApplicationWindow()
{
    // Set the position of the Application window
    System.Windows.Point ptApp = new System.Windows.Point(0, 0);
    Autodesk.AutoCAD.ApplicationServices.Application.MainWindow.DeviceIndependentLocation = ptApp;

    // Set the size of the Application window
    System.Windows.Size szApp = new System.Windows.Size(400, 400);
    Autodesk.AutoCAD.ApplicationServices.Application.MainWindow.DeviceIndependentSize = szApp;
}

Minimize and maximize the Application window

Note: The following example requires 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.

C# Example

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.

C# Example

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 window invisible and then visible again.

C# Example

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