ChiaFong

Maximizing a borderless/no-caption window

In Programming on 5 February, 2009 at 7:00 am

When you choose to have a WPF application without border or caption, you need to provide your own maximize button and your own code to maximize the window.  Normally you can just do:

    this.WindowState = WindowState.Maximized;

and the window will be maximized.  But this call will cause the window to cover up the taskbar.  Thus we will need to set the maximum height and width of the window to a size where the window will not cover the taskbar before you call the above stage change.

    MaxHeight = SystemParameters.MaximizedPrimaryScreenHeight;
    MaxWidth = SystemParameters.MaximizedPrimaryScreenWidth;

This will work at first glance until you test the application on a system with an extended secondary display that has a higher resolution than your primary display.  When you drag your application to the secondary display and maximize it, the height and width will be limited to the primary display’s height and width.  Thus you will see a blank area not cover by your application while it is maximized.  Another thing will happen is that if you try to expand your application while it is on the secondary display, the height and width will also be limited by that of the primary display.

To solve this I’ve immediately thought of finding a way to get the height and width of the secondary display and also to detect whether the application is on a secondary display.  This is all quite complicated.  Then something hit me, this problem only occur on an extended secondary display and on this kind of display there isn’t any taskbar, thus a much easier way is to detect whether the current display is a primary display and then set or reset the MaxHeight and MaxWidth accordingly.  Thus I implement 2 methods RestoreWindow() and MaximizeWindow().

    private void RestoreWindow()
    {
        // reset the max width and height so that you can expand
        // the window properly while on a secondary window
        MaxHeight = double.PositiveInfinity;
        MaxWidth = double.PositiveInfinity;
 
        // set the window back to normal state
        this.WindowState = WindowState.Normal;
    }
 
    private void MaximizeWindow()
    {
        // Get the current display, whether it is on primary or 
        // secondary
        System.Drawing.Point pt = 
                System.Windows.Forms.Cursor.Position;
        System.Windows.Forms.Screen currentScreen;
        currentScreen = 
                System.Windows.Forms.Screen.FromPoint(pt);
 
        // test whether the screen is a primary display
        if (currentScreen.Primary) 
        {
            // if it is a primary display, set the max width and
            // height so that the maximize window will not cover 
            // the taskbar
            MaxHeight = 
                SystemParameters.MaximizedPrimaryScreenHeight;
            MaxWidth = 
                SystemParameters.MaximizedPrimaryScreenWidth;
        }
        else
        {
            // if it is not a primary display, reset the max 
            // width and height
            MaxHeight = double.PositiveInfinity;
            MaxWidth = double.PositiveInfinity;
        }
 
        // set window to become maximized
        this.WindowState = WindowState.Maximized;
    }
About these ads
  1. Lots of folks blog about this matter but you wrote down some true words.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: