How to restore MainWindow to maximized on startup

I tried to restore the MainWindow on application startup on the second screen. I saved some window position, Size and state on previous window shutdown. On startup i tried to restore the old state.

window.Left = rectangleLocation.Location.X;
window.Top = rectangleLocation.Location.Y;
window.Width = rectangleLocation.Width;
window.Height = rectangleLocation.Height;
if (state == System.Windows.WindowState.Maximized)
window.WindowState = state;

Unfortunately the window comes up maximized on the first screen.
Is the code above wrong or is there another way to do this.


Answer this question

How to restore MainWindow to maximized on startup

  • Antoine.Pourrat

    Unfortunately the Window APIs can not target monitor. One workaround I can think of is to set the left, top, width, height, show the window and then set the state to maximized. That will make sure the window shows in the original location, which means the original monitor. However there might be a little bit flash as the window changes its state after show.

    The code looks like this:

    window.Left = rectangleLocation.Location.X;

    window.Top = rectangleLocation.Location.Y;

    window.Width = rectangleLocation.Width;

    window.Height = rectangleLocation.Height;

    window.Show();

    if (state == System.Windows.WindowState.Maximized)

    window.WindowState = state;


  • SlitCanvas

    Yes, i do mean the second monitor. It seems that the WindowState property is applied befor the position properties.

  • Chris Wundram

    Hua Wang - MSFT wrote:

    Unfortunately the Window APIs can not target monitor.

    I have to admit that I'm a bit sad that multimonitor support isnt being improved for WPF. What exists so far with win32 is a good start, and programmers can include (with pain) which monitor their window will start on (and save that information)

    I've been using 2 monitors ever since i've been able to do it, and honestly, having more support for multimonitor would be very great.

    ... a WPF admirer.


  • 10131011

    I'm guessing you are familiar with GetWindowPlacement/SetWindowPlacement Although not managed, here's a WPF Window that uses them to remember size/location/window state from one instance to the next (using .NET's application settings support).

    If the window is minimized when closed, this code restores it to its normal size the next time the window opens. Otherwise, the window opens the same as it was prior to last closing ie restored or maximized.

    [1]

    using System;

    using System.Collections.Generic;

    using System.Text;

    using System.Windows;

    using System.Windows.Controls;

    using System.Windows.Interop;

    using System.Runtime.InteropServices;

    namespace PersistWindowSettings_AppSettings

    {

    [Serializable]

    [StructLayout(LayoutKind.Sequential)]

    public struct WINDOWPLACEMENT

    {

    public int length;

    public int flags;

    public int showCmd;

    public POINT minPosition;

    public POINT maxPosition;

    public RECT normalPosition;

    }

    [Serializable]

    [StructLayout(LayoutKind.Sequential)]

    public struct RECT

    {

    public int Left;

    public int Top;

    public int Width;

    public int Height;

    public RECT(int left, int top, int width, int height)

    {

    this.Left = left;

    this.Top = top;

    this.Width = width;

    this.Height = height;

    }

    }

    [Serializable]

    [StructLayout(LayoutKind.Sequential)]

    public struct POINT

    {

    public int X;

    public int Y;

    public POINT(int x, int y)

    {

    this.X = x;

    this.Y = y;

    }

    }

    public partial class MainWindow : Window

    {

    [DllImport("user32.dll")]

    static extern bool SetWindowPlacement(IntPtr hWnd, [In] ref WINDOWPLACEMENT lpwndpl);

    [DllImport("user32.dll")]

    static extern bool GetWindowPlacement(IntPtr hWnd, out WINDOWPLACEMENT lpwndpl);

    const int SW_SHOWNORMAL = 1;

    const int SW_SHOWMINIMIZED = 2;

    public MainWindow()

    {

    InitializeComponent();

    }

    protected override void OnSourceInitialized(EventArgs e)

    {

    base.OnSourceInitialized(e);

    try

    {

    WINDOWPLACEMENT wp = (WINDOWPLACEMENT)Properties.Settings.Default.WindowPlacement;

    wp.length = Marshal.SizeOf(typeof(WINDOWPLACEMENT));

    wp.flags = 0;

    wp.showCmd = ( wp.showCmd == SW_SHOWMINIMIZED SW_SHOWNORMAL : wp.showCmd);

    IntPtr hwnd = new WindowInteropHelper(this).Handle;

    SetWindowPlacement(hwnd, ref wp);

    }

    catch { }

    }

    // WARNING - Not fired when Application.SessionEnding is fired

    protected override void OnClosing(System.ComponentModel.CancelEventArgs e)

    {

    base.OnClosing(e);

    WINDOWPLACEMENT wp = new WINDOWPLACEMENT();

    IntPtr hwnd = new WindowInteropHelper(this).Handle;

    GetWindowPlacement(hwnd, out wp);

    Properties.Settings.Default.WindowPlacement = wp;

    Properties.Settings.Default.Save();

    }

    }

    }


  • Lisa Feigenbaum

    I have already tried your workaround. It works with flash. But you should really fix this bug until release. It worked in Win32 why not in WPF
  • Janthegroat

    I might be incorrect on this but looks like the code above is incorrect.

    if (state == System.Windows.WindowState.Maximized)
    window.WindowState = state;

    This piece of code will always make the WindowState Maximized.

    By second screen do you mean the second monitor

    - Sunil.


  • Peter Manse

    No ideas

  • How to restore MainWindow to maximized on startup