Inveigled By Design

The art of persuasive design

Maximizing a WPF Window With WindowState=None

When you get set a WPF window’s WindowStyle to None, the window will cover up the task bar when you set its WindowState to Maximized. I’ve seen various approaches to handling this through:

Another approach that seems to work quite well is to use the functionality in the System.Windows.Forms and System.Windows.Interop libraries to get the screen working area size from the monitor it is being displayed on. Add System.Windows.Forms and System.Drawing in the project references. Then add a handler for the Window.Loaded event in the xaml:

Loaded="Window_Loaded"

Implement the Window_Loaded function as follows:

private void Window_Loaded(object sender, RoutedEventArgs e)
{
    IntPtr windowHandle;
    Rectangle workingArea;
    Screen screen;

    windowHandle = new WindowInteropHelper(this).Handle;
    screen = Screen.FromHandle(windowHandle);
    workingArea = screen.WorkingArea;

    Left = workingArea.Left;
    Top = workingArea.Top;
    Width = workingArea.Width;
    Height = workingArea.Height;
}

The WindowInteropHelper helps bridge the WPF/Forms gap by giving us a handle for the window. While there are some drawbacks to having to include the Windows.Forms and Drawing libraries, this should work for any screen the window is being displayed on.

2 Responses to “Maximizing a WPF Window With WindowState=None”

  1. Great work you ‘ve done!I ‘ve read so many blogs and forums and only your code worked like a charm!But…i can’t figure out a way to handle my custom button Maximize_Restore.I call your handler with the click on my Maximize button, so it gets to a maximized (desired) state.When i click it again it doesn’t restore (of course), anyway i wonder if you can help me out to set it to my custom window..
    Cheers,
    Ilias

  2. Hi Ilias,

    Before the window gets maximized you need to save its state (Top, Left, Width, Height properties) and then when you restore, reset those properties on the window.

Leave a Reply

Creative Commons License
Content on this site is licensed under a Creative Commons Attribution-Share Alike 3.0 License
Copyright © 2009 Inveigled Software