If user has multiple screens,
how can I start application in primary screen or chosen screen at start up
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;
}
}
}
}
}
This is my borderless window implementation. You should be able to interpret this if you need any differences. I make the window 1/2 the size of my primary monitor then center it. Make sure 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;
}
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.
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.
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 BarFullPrimaryScreenHeight
- 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;
}