How can I make a WPF window maximized on the screen with the mouse cursor?
Asked Answered
L

4

56

According to the MSDN documentation for the WindowStartupLocation Property:

Setting CenterScreen causes a window to be positioned in the center of the screen that contains the mouse cursor.

Although the MSDN doc for the CenterScreen Field itself defines it somewhat less clearly as:

The startup location of a window is the center of the screen on which it is opened.

A simple test shows this working as it should:

MainWindow.xaml

<Window x:Class="CenterScreenTest.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <Button Click="button_Click">Open Window</Button>
</Window>

MainWindow.xaml.cs

using System.Windows;

namespace CenterScreenTest
{
    public partial class MainWindow
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        void button_Click(object sender, RoutedEventArgs e)
        {
            Window window = new Window();
            window.WindowStartupLocation = WindowStartupLocation.CenterScreen;
            window.Show();
        }
    }
}

If you test this out on a dual monitor system, you can see that the new window will be centered on the screen where the mouse cursor is when you click the button. That's exactly how it should work.

However, if you try to set the window to maximize before you show it, the new window will only maximize on the display from which you launched the application. Change the button_Click event handler to the following to see what I mean:

void button_Click(object sender, RoutedEventArgs e)
{
    Window window = new Window();
    window.WindowStartupLocation = WindowStartupLocation.CenterScreen;
    window.WindowState = WindowState.Maximized;
    window.Show();
}

Now the window will only maximize on the screen from which the application is launched, no matter where the mouse cursor is when you click the button. If you set the window state to maximized after you show it, CenterScreen works properly. This is equivalent to the user maximizing the window. For instance:

void button_Click(object sender, RoutedEventArgs e)
{
    Window window = new Window();
    window.WindowStartupLocation = WindowStartupLocation.CenterScreen;
    window.Show();
    window.WindowState = WindowState.Maximized;
}

The problem here, of course, is that maximizing the window after showing it takes much longer and in an app such as mine, the window needs to pop into place immediately.

Anyone know of a solution?

Lawson answered 25/6, 2010 at 22:41 Comment(1)
tl;dr: Set WindowStartupLocation to CenterScreen to open it on the screen with the mouse cursor; then in the Loaded event add this.WindowState = WindowState.Maximized;. (On Windows 10, it just opens maximized if you do this, and on the screen with the mouse cursor; since loaded fires before the window is first displayed.)Coper
L
46

I asked the same question on the MSDN WPF Forum and got an answer with this awesome workaround:

void button_Click(object sender, RoutedEventArgs e)
{
    Window window = new Window();
    window.WindowStartupLocation = WindowStartupLocation.CenterScreen;
    window.SourceInitialized += (s, a) => window.WindowState = WindowState.Maximized;
    window.Show();
}

I've also submitted a bug report about this issue to Microsoft that they are currently reviewing.

Lawson answered 30/6, 2010 at 17:51 Comment(1)
How this is related with the mouse cursor?Cattleya
P
101

You can set it in XAML

<Window Height="300" Width="300" WindowState="Maximized">
</Window>

You need to set SizeToContent to Manual. See other answers for details.

Parlin answered 19/1, 2014 at 19:55 Comment(0)
L
46

I asked the same question on the MSDN WPF Forum and got an answer with this awesome workaround:

void button_Click(object sender, RoutedEventArgs e)
{
    Window window = new Window();
    window.WindowStartupLocation = WindowStartupLocation.CenterScreen;
    window.SourceInitialized += (s, a) => window.WindowState = WindowState.Maximized;
    window.Show();
}

I've also submitted a bug report about this issue to Microsoft that they are currently reviewing.

Lawson answered 30/6, 2010 at 17:51 Comment(1)
How this is related with the mouse cursor?Cattleya
A
11

Starting with the window maximize can be achieved by the following addition to the XAML markup.

<Window Height="300" Width="300" 
  WindowState="Maximized"
  SizeToContent="Manual">
</Window>

The property WindowState is subordinate to SizeToContent, which means that you need to set the latter Manual (if you wish the actual maximization). You can also set SizeToContent to Height or Width (if you wish to maximize the window in one dimension, whereas obeying the size computed based on the controls' sizes in the other).

<Window Height="300" Width="300" 
  WindowState="Maximized"
  SizeToContent="Width">
</Window>

The above will make the window span from top to bottom but not necessarily from left to right edge. It's equivalent to pressing the key combination Win+Shift+Up.

Anaglyph answered 27/8, 2016 at 9:34 Comment(0)
F
5

Make sure that SizeToContent is set to SizeToContent.Manual, otherwise it won't work.

Fact answered 24/7, 2012 at 8:5 Comment(1)
You actually need to do both those thing. Please view my answer to make sure. +1 anyway.Anaglyph

© 2022 - 2024 — McMap. All rights reserved.