Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
695 views
in Technique[技术] by (71.8m points)

.net - How can I make a WPF window maximized on the screen with the mouse cursor?

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...