题
我想中心我的WPF应用程序启动时的主屏幕上。我知道我必须设置myWindow.Left和myWindow.Top,但我在哪里得到的值?
我发现System.Windows.Forms.Screen.PrimaryScreen
,这显然是不WPF。是否有一个WPF的替代方案,给我的屏幕分辨率或类似的东西?
解决方案
将这个
WindowStartupLocation = System.Windows.WindowStartupLocation.CenterScreen;
.NET FrameworkSupported中:4,3.5, 3.0
.NET框架客户端ProfileSupported 在:4,3.5 SP1
其他提示
<强> XAML 强>
WindowStartupLocation="CenterScreen"
您还可以使用Screen类从一个WPF应用程序。你只需要从应用程序组件参照System.Windows.Forms的。一旦这样做,(和参考System.Drawing中为下面的示例):
Rectangle workingArea = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea;
...工作得很好。
你有没有考虑你的主窗口属性WindowStartupLocation设置为中心屏幕?
有关PresentationFramework的SystemParameters类是什么?它有一个 WORKAREA 的财产,这似乎是你所期待的。
但是,为什么不设置Window.WindowStartupLocation工作?中心屏幕是枚举值之一。你必须调整的中心?
您不需要从你的应用程序中引用的System.Windows.Forms
组装。相反,你可以使用System.Windows.SystemParameters.WorkArea
。这是相当于System.Windows.Forms.Screen.PrimaryScreen.WorkingArea
!
没有WPF等效。 System.Windows.Forms.Screen
仍然是.NET框架的一部分,并且可以从WPF虽然可以使用。
请参阅这个问题了解细节,但你可以通过使用的 WindowInteropHelper
类来包装您的WPF控制。
var window = new MyWindow();
对于屏幕使用中心:
window.WindowStartupLocation = System.Windows.WindowStartupLocation.CenterScreen;
对于父窗口使用中心:
window.WindowStartupLocation = System.Windows.WindowStartupLocation.CenterOwner;