【发布时间】:2011-06-12 19:57:16
【问题描述】:
如果用户有多个屏幕,
如何在启动时在主屏幕或所选屏幕中启动应用程序
【问题讨论】:
如果用户有多个屏幕,
如何在启动时在主屏幕或所选屏幕中启动应用程序
【问题讨论】:
这是基本代码。它使用 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 在启动时定位您的屏幕。
【讨论】:
更好的是,将当前窗口位置保存到独立存储中,然后在启动时将窗口恢复到相同位置(如果您可以找到存储在隔离存储)。按照 Roy T 的建议,使用 Window.WindowStartupLocation。这也应该适用于多台显示器。
【讨论】:
这是我纯粹的 WPF 解决方案,将窗口居中在主监视器上,周围有空白区域(因为我不希望它最大化)。我的设置是左侧的方形显示器和右侧的宽屏。这是在将每个监视器设置为 Windows 中的主监视器的情况下进行测试的。
在我找到那个解决方案之前,System.Windows.SystemParameters 上有三个有用的属性,它们给出了不同的高度。给出的数字适用于我的 1920x1080 宽屏。
PrimaryScreenHeight - 1080。Windows 中设置的实际分辨率高度。WorkArea.Height - 1040。实际分辨率高度减去起始栏FullPrimaryScreenHeight - 1018。实际分辨率减去开始栏和窗口标题。这是我的解决方案,我使用WorkArea.Height:
protected T SetWindowLocation<T>(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 设置为手动。
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;
}
【讨论】: