locked
How can I make a WPF window maximized on the screen with the mouse cursor? RRS feed

  • Question

  • 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.

    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 have a solution?
    • Edited by andrew.geer Wednesday, June 30, 2010 5:41 PM removed external link
    Tuesday, June 29, 2010 10:49 PM

Answers

  • Hello Andrew,

    During the window initialization, the internal positioning logic ignores (at some point) the window position when the window state is set to Maximized or Minimized. This caused what you observed.

    A quick workaround:

    Window wnd = new Window();
    wnd.WindowStartupLocation = WindowStartupLocation.CenterScreen;
    wnd.SourceInitialized += (snd, arg) => wnd.WindowState = WindowState.Maximized;
    wnd.Show();

    The basic idea here is to set the Maximized state at the time when initialization is done, but the window is not yet shown - so I used the SourceInitialized event - thus making the initialization logic hornor the positioning and we can also have the window maximized.

    Hope this helps.

    Regards,
    Jie
    MSDN Subscriber Support in Forum
    If you have any feedback on our support, please contact msdnmg@microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    If you have any feedback, please tell us.

    The All-In-One Code Framework Project
    My Blog (in Simplified Chinese)
    • Marked as answer by andrew.geer Wednesday, June 30, 2010 4:58 PM
    Wednesday, June 30, 2010 9:03 AM

All replies

  • Hello Andrew,

    During the window initialization, the internal positioning logic ignores (at some point) the window position when the window state is set to Maximized or Minimized. This caused what you observed.

    A quick workaround:

    Window wnd = new Window();
    wnd.WindowStartupLocation = WindowStartupLocation.CenterScreen;
    wnd.SourceInitialized += (snd, arg) => wnd.WindowState = WindowState.Maximized;
    wnd.Show();

    The basic idea here is to set the Maximized state at the time when initialization is done, but the window is not yet shown - so I used the SourceInitialized event - thus making the initialization logic hornor the positioning and we can also have the window maximized.

    Hope this helps.

    Regards,
    Jie
    MSDN Subscriber Support in Forum
    If you have any feedback on our support, please contact msdnmg@microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    If you have any feedback, please tell us.

    The All-In-One Code Framework Project
    My Blog (in Simplified Chinese)
    • Marked as answer by andrew.geer Wednesday, June 30, 2010 4:58 PM
    Wednesday, June 30, 2010 9:03 AM
  • Jie, thank you very much. That workaround is exactly what I needed! And thanks for the explanation as well.
    Wednesday, June 30, 2010 4:59 PM