如何将WPF应用程序置于屏幕中心?

Mar*_*l B 104 c# wpf screen-positioning

我想在主屏幕上启动我的WPF应用程序.我知道我必须设置myWindow.Left和myWindow.Top,但是我在哪里获取值?

我发现System.Windows.Forms.Screen.PrimaryScreen,这显然不是WPF.有没有WPF替代品给我屏幕分辨率或类似的东西?

Pra*_*are 146

XAML

WindowStartupLocation="CenterScreen"
Run Code Online (Sandbox Code Playgroud)

  • 我更喜欢使用WPF xaml.谢谢!;-) (10认同)

Ind*_*000 124

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

WindowStartupLocation = System.Windows.WindowStartupLocation.CenterScreen;
Run Code Online (Sandbox Code Playgroud)

.NET Framework支持:4,3.5,3.0

.NET Framework Client Profile支持:4,3.5 SP1

  • 为我工作(.NET 4),我也喜欢一些子窗口的'WindowStartupLocation.CenterOwner` (2认同)

Mic*_*tta 49

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

Rectangle workingArea = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea;
Run Code Online (Sandbox Code Playgroud)

......工作得很好.

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


小智 8

PresentationFramework中的SystemParameters类怎么样?它有一个WorkArea属性,似乎是你正在寻找的.

但是,为什么不设置Window.WindowStartupLocation呢?CenterScreen是枚举值之一.你需要调整定心吗?


Mim*_*imi 7

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