Wpf在主屏幕上启动

5
如果用户有多个屏幕,如何在启动时在主屏幕或选择的屏幕上启动应用程序?
5个回答

8
这是基本代码,它使用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;
                }
            }
        }
    }
}

是的,对不起。代码最初使用了一个配置值(字符串)。 - bic

4

好的,没有什么快速解决方案,但是如果你研究这些类,应该能够编写一些代码来完成这个任务。不过我没有现成的代码示例可以给你,但是那样做有什么乐趣呢 ;)。 - Roy T.
如果我想选择屏幕,我该怎么做? - Navid Rahmani

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

1
好观点,我讨厌程序不能从我离开它们的地方开始 :)。 - Roy T.

1
这是我纯粹使用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;
    }

0
这是我的无边框窗口实现。如果您需要任何差异,您应该能够解释它。我将窗口大小设置为主监视器的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;
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接