Start Wpf on the main screen

If the user has multiple screens,

how can I launch the application on the main screen or the selected screen at startup

+5
source share
5 answers

Here is the main code. It uses WinForms, but I do not know a clean WPF solution.

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;
                }
            }
        }
    }
}
+8
source

See this question on MSDN: http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/316c001b-0511-4c18-8e26-d46021381ae6

You can find information about the main screen in SystemParameters.PrimaryScreen * Then you can use Window.WindowStartupLocation or for a specific point you can use the W32 API and use SetWindowPos to position your screen at startup.

+4

, , ( , ). Window.WindowStartupLocation, 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 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
source

This is my borderless implementation. You should be able to interpret this if you need any differences. I make the window 1/2 the size of my main monitor, and then centers it. Verify that WindowStartupLocation is set to 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;
}
0
source

All Articles