53

I have a windowless wpf application, whenever I set the window state as maximized it maximizes it on the primary display.

What I would like to do is have it maximize on which ever display the application is running.

So any idea how I would do this?

My code at the moment is just

private void titleBarThumb_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
    if (this.WindowState == System.Windows.WindowState.Normal)
    {
        this.WindowState = System.Windows.WindowState.Maximized;
    }
    else
    {
        this.WindowState = System.Windows.WindowState.Normal;
    }
}
Diego Montania
  • 322
  • 5
  • 12
electricsheep
  • 5,114
  • 9
  • 37
  • 41
  • WHen you said "windowless" did you mean borderless? – Tim May 20 '11 at 13:00
  • Yes its borderless, basically I meant that Window.WindowStyle is none – electricsheep May 20 '11 at 15:08
  • 3
    When I maximize a window it always maximizes on the screen it's on. Does it work when you give the window a border? – RandomEngy Jun 07 '11 at 23:11
  • `WindowState.Maximized` works normally, but when I use `WindowState.Normal`, the window cannot be restored. – januw a Oct 06 '20 at 06:59
  • I asked a similar question that you might find helpful. [How Can I Make a WPF Window Maximized on the Screen with the Mouse Cursor?](https://stackoverflow.com/questions/3121900/how-can-i-make-a-wpf-window-maximized-on-the-screen-with-the-mouse-cursor) – Eben Geer Jun 07 '11 at 22:16
  • Look at this question and answer: https://stackoverflow.com/questions/935599/how-to-center-a-wpf-app-on-screen You can use the described functions in Windows.Forms.Screen to get the current screen. Then maybe setting the windows' StartupLocation to this screen (before maximizing as you already did) may achieve what you want, but I didn't take the time to try it myself, to be honest. – Simon D. May 20 '11 at 12:58

9 Answers9

51

I've included this line on my MainWindow (first control) constructor:

Application.Current.MainWindow.WindowState = WindowState.Maximized;
Francisco Campos
  • 1,191
  • 8
  • 16
14

Because of the taskbar you should use user working area's size:

this.Width=SystemParameters.WorkArea.Width;
this.Height=SystemParameters.WorkArea.Height;

You can use this in view's constructor

Navid Rahmani
  • 7,848
  • 9
  • 39
  • 57
  • 1
    If you want to set it on the left corner add this (this.Top = SystemParameters.WorkArea.Top; this.Left = SystemParameters.WorkArea.Left;) – user1520494 Feb 24 '14 at 10:33
  • This only works for the primary screen. Additional screens are not accessible through SystemParameters and may have different dimensions and rotations. – AlwaysLearning Aug 12 '19 at 05:07
5

I am not sure if this is answered yet- I created a sample app with the

WindowStyle = WindowStyle.None;

I created a button and on the click handler did this-

WindowState = WindowState.Maximized

I hooked up the MouseLeftButtonDown handler for the window to drag move-

this.MouseLeftButtonDown += new(MainWindow_MouseLeftButtonDown);

private void MainWindow_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
   DragMove();
}

When I dragged my window to my second monitor and clicked the maximize button, it maximized in the current window, not the startup window. I was using VS2010 and .NET 4. Let me know if this helps.

thebringking
  • 1,339
  • 1
  • 15
  • 31
4

A question with 7 upvotes deserves the correct answer. :D

Use this window instead of normal window and then Maxmize/Minimize/normalize will take care of itself.

using System;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Interop;

public partial class MyWindow : Window
{
    public MyWindow ()
    {
        this.InitializeComponent();

        this.SourceInitialized += this.OnSourceInitialized;
    }

    #endregion

    #region Methods

    private static IntPtr WindowProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
    {
        switch (msg)
        {
            case 0x0024:
                WmGetMinMaxInfo(hwnd, lParam);
                handled = true;
                break;
        }
        return (IntPtr)0;
    }

    private static void WmGetMinMaxInfo(IntPtr hwnd, IntPtr lParam)
    {
        var mmi = (MINMAXINFO)Marshal.PtrToStructure(lParam, typeof(MINMAXINFO));

        // Adjust the maximized size and position to fit the work area of the correct monitor
        IntPtr monitor = MonitorFromWindow(hwnd, (int)MonitorFromWindowFlags.MONITOR_DEFAULTTONEAREST);

        if (monitor != IntPtr.Zero)
        {
            var monitorInfo = new MONITORINFO();
            GetMonitorInfo(monitor, monitorInfo);
            RECT rcWorkArea = monitorInfo.rcWork;
            RECT rcMonitorArea = monitorInfo.rcMonitor;
            mmi.ptMaxPosition.x = Math.Abs(rcWorkArea.Left - rcMonitorArea.Left);
            mmi.ptMaxPosition.y = Math.Abs(rcWorkArea.Top - rcMonitorArea.Top);
            mmi.ptMaxSize.x = Math.Abs(rcWorkArea.Right - rcWorkArea.Left);
            mmi.ptMaxSize.y = Math.Abs(rcWorkArea.Bottom - rcWorkArea.Top);
        }

        Marshal.StructureToPtr(mmi, lParam, true);
    }

    private void OnSourceInitialized(object sender, EventArgs e)
    {
        var window = sender as Window;

        if (window != null)
        {
            IntPtr handle = (new WindowInteropHelper(window)).Handle;
            HwndSource.FromHwnd(handle).AddHook(WindowProc);
        }
    }
}

DLL imports and declarations

[StructLayout(LayoutKind.Sequential)]
public struct MINMAXINFO
{
    public POINT ptReserved;

    public POINT ptMaxSize;

    public POINT ptMaxPosition;

    public POINT ptMinTrackSize;

    public POINT ptMaxTrackSize;
} ;

public enum MonitorFromWindowFlags
{
    MONITOR_DEFAULTTONEAREST = 0x00000002
}

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public class MONITORINFO
{
    public int cbSize = Marshal.SizeOf(typeof(MONITORINFO));

    public RECT rcMonitor;

    public RECT rcWork;

    public int dwFlags;
}

[StructLayout(LayoutKind.Sequential, Pack = 0)]
public struct RECT
{
    public int Left;

    public int Top;

    public int Right;

    public int Bottom;

    public static readonly RECT Empty;

    public int Width
    {
        get
        {
            return Math.Abs(this.Right - this.Left);
        } // Abs needed for BIDI OS
    }

    public int Height
    {
        get
        {
            return this.Bottom - this.Top;
        }
    }

    public RECT(int left, int top, int right, int bottom)
    {
        this.Left = left;
        this.Top = top;
        this.Right = right;
        this.Bottom = bottom;
    }

    public RECT(RECT rcSrc)
    {
        this.Left = rcSrc.Left;
        this.Top = rcSrc.Top;
        this.Right = rcSrc.Right;
        this.Bottom = rcSrc.Bottom;
    }

    public bool IsEmpty
    {
        get
        {
            // BUGBUG : On Bidi OS (hebrew arabic) left > right
            return this.Left >= this.Right || this.Top >= this.Bottom;
        }
    }

    public override string ToString()
    {
        if (this == Empty)
        {
            return "RECT {Empty}";
        }
        return "RECT { left : " + this.Left + " / top : " + this.Top + " / right : " + this.Right + " / bottom : " +
               this.Bottom + " }";
    }

    public override bool Equals(object obj)
    {
        if (!(obj is RECT))
        {
            return false;
        }
        return (this == (RECT)obj);
    }

    public override int GetHashCode()
    {
        return this.Left.GetHashCode() + this.Top.GetHashCode() + this.Right.GetHashCode() +
               this.Bottom.GetHashCode();
    }

    public static bool operator ==(RECT rect1, RECT rect2)
    {
        return (rect1.Left == rect2.Left && rect1.Top == rect2.Top && rect1.Right == rect2.Right &&
                rect1.Bottom == rect2.Bottom);
    }

    public static bool operator !=(RECT rect1, RECT rect2)
    {
        return !(rect1 == rect2);
    }
}
[DllImport("user32.dll", SetLastError = true)]
public static extern bool GetMonitorInfo(IntPtr hMonitor, MONITORINFO lpmi);

[DllImport("user32.dll", SetLastError = true)]
public static extern IntPtr MonitorFromWindow(IntPtr handle, int flags);
NVM
  • 5,442
  • 4
  • 41
  • 61
  • I followed your code but am getting some weird functionality. Would you mind looking at my [question](http://stackoverflow.com/questions/25041416/vertically-maximizing-in-windows-wpf)? – Dehli Jul 30 '14 at 20:26
  • 3
    This fails when maximizing on a secondary monitor, if it is larger than the primary. – Murray Aug 19 '14 at 07:35
  • Specifically, from Murray's link. I think this is the part that does it (correct me if I'm wrong) https://github.com/MahApps/MahApps.Metro/blob/fd8a7938097c0fbe65d000cd1bbaecafdee1992e/MahApps.Metro/Behaviours/BorderlessWindowBehavior.cs#L254 – Branden Barber Sep 15 '15 at 15:03
1

I had my application getting maximized in the secondary screen by doing this

Add this at the top of the main window :

using Screen = System.Windows.Forms.Screen;

Add this in the maximize handler :

private void AdjustWindowSize()
{
    if (this.WindowState == WindowState.Maximized)
    {
        this.WindowState = WindowState.Normal;
    }
    else
    {
        System.Drawing.Rectangle r = Screen.GetWorkingArea(new System.Drawing.Point((int)this.Left, (int)this.Top));
        this.MaxWidth = r.Width;
        this.MaxHeight = r.Height;
        this.WindowState = WindowState.Maximized;
    }
}

Here we go !

Diego Montania
  • 322
  • 5
  • 12
Christophe
  • 11
  • 1
1

We cannot maximize the window until it's loaded. So by hooking the Loaded event of fullScreenWindow and handling the event along the lines of:

private void Window_Loaded(object sender, RoutedEventArgs e) 
{
    WindowState = WindowState.Maximized;
}
Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Sibin
  • 53
  • 1
  • 8
  • That's not true! You can without problems implement ctor { this.InitializeComponent(); this.WindowState = WindowState.Maximized; } This works pretty fine! – peter70 Jan 11 '20 at 08:16
0

I just ran into the same problem. In my case it turned out to be the fact that I was Hiding my pop up window when I was done with it. So if I called it next time and asked it to Maximize, it would do it on the original screen. Once I started Closing it instead, it started to maximize on the proper screen.

Eternal21
  • 4,190
  • 2
  • 48
  • 63
0

Try:

Window.WindowState = WindowState.Normal;
0

c# application first starts on the primary display, unless it is moved your code will work. However, if your wpf app will be moved to another display new location can be recorded and stored in a local config file. But, your app will have no borders or any other native controls so you will also have to implement the moving bit. and when your window is moved you will be able to capture the display index using SystemParameters.

Good Luck

zukas
  • 21
  • 1