C# 如何在屏幕上居中放置 WPF 应用程序?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/935599/
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-08-06 03:40:59  来源:igfitidea点击:

How to center a WPF app on screen?

c#wpfscreen-positioning

提问by Marcel B

I want to center my WPF app on startup on the primary screen. I know I have to set myWindow.Left and myWindow.Top, but where do I get the values?

我想在主屏幕上启动时将我的 WPF 应用程序居中。我知道我必须设置 myWindow.Left 和 myWindow.Top,但是我从哪里获得这些值?

I found System.Windows.Forms.Screen.PrimaryScreen, which is apparently not WPF. Is there a WPF alternative that gives me the screen resolution or something like that?

我发现System.Windows.Forms.Screen.PrimaryScreen,这显然不是 WPF。是否有 WPF 替代方案可以为我提供屏幕分辨率或类似的分辨率?

采纳答案by Indy9000

Put this in your window constructor

把它放在你的窗口构造函数中

WindowStartupLocation = System.Windows.WindowStartupLocation.CenterScreen;

.NET FrameworkSupported in: 4, 3.5, 3.0

.NET Framework Client ProfileSupported in: 4, 3.5 SP1

.NET Framework 支持:4、3.5、3.0

.NET Framework 客户端配置文件在以下版本中受支持:4、3.5 SP1

回答by Michael Petrotta

You can still use the Screen class from a WPF app. You just need to reference the System.Windows.Forms assembly from your application. Once you've done that, (and referenced System.Drawing for the example below):

您仍然可以使用 WPF 应用程序中的 Screen 类。您只需要从您的应用程序中引用 System.Windows.Forms 程序集。完成后,(并在下面的示例中引用 System.Drawing):

Rectangle workingArea = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea;

...works just fine.

...工作得很好。

Have you considered setting your main window property WindowStartupLocation to CenterScreen?

您是否考虑过将主窗口属性 WindowStartupLocation 设置为 CenterScreen?

回答by Jeff Yates

There is no WPF equivalent. System.Windows.Forms.Screenis still part of the .NET framework and can be used from WPF though.

没有 WPF 等效项。System.Windows.Forms.Screen仍然是 .NET 框架的一部分,但可以从 WPF 中使用。

See this questionfor more details, but you can use the calls relating to screens by using the WindowInteropHelperclass to wrap your WPF control.

有关更多详细信息,请参阅此问题,但您可以通过使用WindowInteropHelper类包装 WPF 控件来使用与屏幕相关的调用。

回答by Eddie Butt

What about the SystemParameters class in PresentationFramework? It has a WorkAreaproperty that seems to be what you are looking for.

PresentationFramework 中的 SystemParameters 类呢?它有一个WorkArea属性,这似乎是您要查找的内容。

But, why won't setting the Window.WindowStartupLocation work? CenterScreen is one of the enum values. Do you have to tweak the centering?

但是,为什么设置 Window.WindowStartupLocation 不起作用?CenterScreen 是枚举值之一。你需要调整中心吗?

回答by Pratik Deoghare

xaml

xml

WindowStartupLocation="CenterScreen"

回答by Mimi

You don't need to reference the System.Windows.Formsassembly from your application. Instead, you can use System.Windows.SystemParameters.WorkArea. This is equivalent to the System.Windows.Forms.Screen.PrimaryScreen.WorkingArea!

您不需要System.Windows.Forms从应用程序中引用程序集。相反,您可以使用System.Windows.SystemParameters.WorkArea. 这相当于System.Windows.Forms.Screen.PrimaryScreen.WorkingArea

回答by Artem

var window = new MyWindow();

for center of the screen use:

对于屏幕中央使用:

window.WindowStartupLocation = System.Windows.WindowStartupLocation.CenterScreen;

for center of the parent window use:

对于父窗口的中心使用:

window.WindowStartupLocation = System.Windows.WindowStartupLocation.CenterOwner;

回答by Jason Robinson

I prefer to put it in the WPF code.

我更喜欢把它放在 WPF 代码中。

In [WindowName].xamlfile:

[WindowName].xaml文件中:

<Window x:Class=...
...
WindowStartupLocation ="CenterScreen">