Wpf从主屏幕开始

如果用户有多个屏幕,

如何在启动时在主屏幕或所选屏幕中启动应用程序

这是基本代码。 它使用WinForms,但我不知道纯WPF解决方案。

using System; using System.Windows; using System.Windows.Forms; namespace Foo { public class WindowUtility { public static void MoveToMonitor(Window window, int monitorId, bool maximize) { Screen[] screens = Screen.AllScreens; int screenId = monitorId - 1; if (screens.Length > 1 && screenId < screens.Length) { var screen = screens[screenId]; var area = screen.WorkingArea; if (maximize) { window.Left = area.Left; window.Top = area.Top; window.Width = area.Width; window.Height = area.Height; } else { window.Left = area.Left; window.Top = area.Top; } } } } } 

请参阅此MSDN问题: http : //social.msdn.microsoft.com/Forums/en-US/wpf/thread/316c001b-0511-4c18-8e26-d46021381ae6

您可以在SystemParameters.PrimaryScreen中找到有关主屏幕的信息*然后您可以使用Window.WindowStartupLocation,或者对于特定点,您可以使用W32 API并使用SetWindowPos在启动时定位屏幕。

更好的是, 将当前窗口位置保存到隔离存储 ,然后在启动时将窗口恢复到相同位置 (如果可以找到存储在隔离存储中的窗口位置)。 使用Window.WindowStartupLocation,如Roy T建议的那样。 这应该适用于多个监视器。

这是我纯粹的WPF解决方案,它将一个Window集中在主显示器上,周围有一个空白边框(因为我不希望它最大化)。 我的设置是左侧的方形显示器和右侧的宽屏幕。 测试时将每个监视器设置为Windows中的主监视器。

在我开始使用该解决方案之前, System.Windows.SystemParameters上有三个有用的属性可以提供不同的高度。 给出的数字是我的1920×1080宽屏。

  • PrimaryScreenHeight – 1080.在Windows中设置的实际分辨率高度。
  • WorkArea.Height – 1040.实际分辨率高度减去开始栏
  • FullPrimaryScreenHeightFullPrimaryScreenHeight实际分辨率减去开始栏并减去Window标题。

这是我的解决方案,我使用WorkArea.Height

  protected T SetWindowLocation(T window) where T : Window { //This function will set a window to appear in the center of the user's primary monitor. //Size will be set dynamically based on resoulution but will not shrink below a certain size nor grow above a certain size //Desired size constraints. Makes sure window isn't too small if the users monitor doesn't meet the minimum, but also not too big on large monitors //Min: 1024w x 768h //Max: 1400w x 900h const int absoluteMinWidth = 1024; const int absoluteMinHeight = 768; const int absoluteMaxWidth = 1400; const int absoluteMaxHeight = 900; var maxHeightForMonitor = System.Windows.SystemParameters.WorkArea.Height - 100; var maxWidthForMonitor = System.Windows.SystemParameters.WorkArea.Width - 100; var height = Math.Min(Math.Max(maxHeightForMonitor, absoluteMinHeight), absoluteMaxHeight); var width = Math.Min(Math.Max(maxWidthForMonitor, absoluteMinWidth), absoluteMaxWidth); window.Height = height; window.Width = width; window.Left = (System.Windows.SystemParameters.FullPrimaryScreenWidth - width) / 2; window.Top = (System.Windows.SystemParameters.FullPrimaryScreenHeight - height) / 2; window.WindowStartupLocation = WindowStartupLocation.Manual; return window; } 

这是我的无边框窗口实现。 如果您需要任何差异,您应该能够解释这一点。 我将窗口设置为主监视器的1/2,然后将其居中。 确保将WindowStartupLocation设置为Manual。

 private void Window_Loaded(object sender, RoutedEventArgs e) { Width = SystemParameters.PrimaryScreenWidth / 2; Height = SystemParameters.PrimaryScreenHeight / 2; Left = (SystemParameters.PrimaryScreenWidth - Width) / 2; Top = (SystemParameters.PrimaryScreenHeight - Height) / 2; }