.net 如何使用鼠标光标在屏幕上最大化 WPF 窗口?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3121900/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 14:26:51  来源:igfitidea点击:

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

.netwpfmultiple-monitorsmaximizewindowstate

提问by Eben Geer

According to the MSDN documentation for the WindowStartupLocation Property:

根据WindowStartupLocation 属性的 MSDN 文档:

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

设置 CenterScreen 会导致一个窗口位于包含鼠标光标的屏幕中心。

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

尽管CenterScreen 字段本身的 MSDN 文档将其定义为不太清楚:

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

主窗口.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

主窗口.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 shouldwork.

如果您在双显示器系统上对此进行测试,您可以看到新窗口将在您单击按钮时鼠标光标所在的屏幕上居中。这正是它应该如何工作的。

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:

但是,如果您在显示之前尝试将窗口设置为最大化,则新窗口只会在您启动应用程序的显示器上最大化。将 button_Click 事件处理程序更改为以下内容以了解我的意思:

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 afteryou show it, CenterScreen works properly. This is equivalent to the user maximizing the window. For instance:

现在,无论单击按钮时鼠标光标在哪里,窗口只会在启动应用程序的屏幕上最大化。如果显示后将窗口状态设置为最大化,则 CenterScreen 可以正常工作。这相当于用户最大化窗口。例如:

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?

有人知道解决方案吗?

采纳答案by Eben Geer

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

我在 MSDN WPF 论坛上问了同样的问题,并通过这个很棒的解决方法得到了答案:

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 reportabout this issue to Microsoft that they are currently reviewing.

我还向Microsoft提交了有关此问题的错误报告,他们目前正在该报告

回答by Artur Michajluk

You can set it in XAML

您可以在 XAML 中设置它

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

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

您需要将SizeToContent设置为Manual。有关详细信息,请参阅其他答案。

回答by Tom Leibu

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

确保将SizeToContent 设置为SizeToContent.Manual,否则将不起作用。

回答by Konrad Viltersten

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

从窗口最大化开始,可以通过对 XAML 标记进行以下添加来实现。

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

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

属性WindowState从属于SizeToContent,这意味着您需要设置后者Manual(如果您希望实际最大化)。您还可以将SizeToContent设置为HeightWidth(如果您希望在一个维度上最大化窗口,而遵守基于另一个维度中控件大小计算的大小)。

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

以上将使窗口从上到下跨越,但不一定从左到右边缘。它相当于按下组合键Win+ Shift+ Up