我怎样才能让一个WPF窗口最大化的鼠标光标在屏幕上?鼠标、光标、窗口、屏幕上

2023-09-04 01:01:04 作者:解释就是找死

根据MSDN文档的WindowStartupLocation物业:

设置中心屏幕导致要定位在包含鼠标光标在屏幕的中心的窗口。

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

尽管MSDN文档的CenterScreen现场本身稍差明确定义它为:

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

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

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.

然而,如果你尝试设置窗口,最大限度地提高你表现出来之前,新的窗口将只最大限度地从你启动的应用程序在屏幕上。在button_Click事件处理程序更改为以下,看看我的意思是:

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?

推荐答案

我问在MSDN WPF论坛同样的问题,得到了与this真棒解决方法:

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();
}

我还提交了bug报告这个问题,以微软,他们目前正在审查。

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