Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wpf start in primary screen

Tags:

c#

.net

wpf

If user has multiple screens,

how can I start application in primary screen or chosen screen at start up

like image 241
Navid Rahmani Avatar asked Jun 12 '11 19:06

Navid Rahmani


4 Answers

Heres the basic code. It uses WinForms but I dont know of a pure 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;
                }
            }
        }
    }
}
like image 176
bic Avatar answered Oct 30 '22 01:10

bic


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

You can find information about the primary screen in SystemParameters.PrimaryScreen* Then you can use Window.WindowStartupLocation or for a specific point you can use the W32 APIs and use SetWindowPos to position your screen on start up.

like image 43
Roy T. Avatar answered Oct 30 '22 00:10

Roy T.


Better yet, save the current window location to Isolated Storage and then at startup time restore the window to the same location (if you can find a window location stored in isolated storage). Use the Window.WindowStartupLocation as Roy T suggested. This should work across multiple monitors as well.

like image 1
Philipp Schmid Avatar answered Oct 30 '22 02:10

Philipp Schmid


This is my purely WPF solution to center a Window on the primary monitor with a border of empty space around it (because I don't want it maximized). My setup is a square-ish monitor on my left and a widescreen on my right. This was tested with each monitor being set as the primary monitor in Windows.

Before I get to that solution, there are three useful properties on System.Windows.SystemParameters that give various heights. The numbers given are for my 1920x1080 widescreen.

  • PrimaryScreenHeight - 1080. The actual resolution height set in Windows.
  • WorkArea.Height - 1040. The actual resolution height minus the Start Bar
  • FullPrimaryScreenHeight - 1018. The actual resolution minus the Start Bar and minus the Window header.

This is my solution and I use 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;
    }
like image 1
Kevin Kalitowski Avatar answered Oct 30 '22 02:10

Kevin Kalitowski