Click here to Skip to main content
15,610,563 members
Articles / Desktop Programming / WPF
Posted 13 Jan 2017


18 bookmarked

WPF Windows on Two Screens

Rate me:
Please Sign up or sign in to vote.
4.71/5 (7 votes)
2 Jul 2017CPOL4 min read
Position WPF window on secondary monitor
In this article, you will learn how to position WPF window on secondary monitor or show two windows on two monitors.


The post shows how to position WPF window on secondary monitor or show two windows on two monitors. The post contains the complete code and we discuss how to address several scenarios.

There are the following disclaimers:

  1. I have provided code and conclusions based on empirical results, so I could be wrong or describe things incorrectly.
  2. The code was tested on a computer with two monitors, so three and more monitors could behave differently.
  3. There are many topics concerning how to show or manipulate by the secondary monitor in WPF application. The post doesn't include these links, as it may be easily Googled.

The original post contains screenshots from a computer with two displays.

Image 1 Image 2

Image 3


The application demonstrates the following features:

  1. Output maximized windows on all displays
  2. Window contains list box with sizes of screens from system parameters and Screen class
  3. Using of dependency container


The solution uses C#6, .NET 4.6.1, WPF with MVVM pattern, System.Windows.Forms, System.Drawing, NuGet packages Unity and Ikc5.TypeLibrary.


The solution contains one WPF application project. From the WPF application, all screens are considered as one "virtual" screen. In order to position window, it is necessary set window coordinates in "current" screen size. SystemParameters provides physical resolution of the primary screen, but according to WPF architecture, the position of elements should not rely on physical dimensions of the display. The main issue is that the exact value of "current" screen size depends on various factors. For example, they include text size from Windows' settings, how user connects to computer - by remote access or locally, and so on.

Screen class from System.Windows.Forms library provides useful property, Screen[] AllScreens. It allows to enumerate all screens and reads their working area. But coordinates of non-primary screen are set according to "current" text size of the primary window.


As windows are created manually, StartupUri should be removed from App.xaml. Method OnStartup executes the following code:

  • init Unity container
  • Calculate current text size of the primary screen as ratio between Screen.PrimaryScreen.WorkingArea and SystemParameters.PrimaryScreen
  • For each screen, create window, position at current screen, show and maximize
public partial class App : Application
    private void InitContainer(IUnityContainer container)
        container.RegisterType<ILogger, EmptyLogger>();
        container.RegisterType<IMainWindowModel, MainWindowModel>();

    protected override void OnStartup(StartupEventArgs e)

        IUnityContainer container = new UnityContainer();

        var logger = container.Resolve<ILogger>();
        logger.Log($"There are {Screen.AllScreens.Length} screens");

        // calculates text size in that main window (i.e. 100%, 125%,...)
        var ratio = 
           Math.Max(Screen.PrimaryScreen.WorkingArea.Width / 
                    Screen.PrimaryScreen.WorkingArea.Height / 

        var pos = 0;
        foreach (var screen in Screen.AllScreens)
                $"#{pos + 1} screen, size = ({screen.WorkingArea.Left}, 
                {screen.WorkingArea.Top}, {screen.WorkingArea.Width}, 
                                          {screen.WorkingArea.Height}), " +
                (screen.Primary ? "primary screen" : "secondary screen"));

            // Show automata at all screen
            var mainViewModel = container.Resolve<IMainWindowModel>(
                new ParameterOverride("backgroundColor", _screenColors[Math.Min
                                      (pos++, _screenColors.Length - 1)]),
                new ParameterOverride("primary", screen.Primary),
                new ParameterOverride("displayName", screen.DeviceName));

            var window = new MainWindow(mainViewModel);
            if (screen.Primary)
                Current.MainWindow = window;

            window.Left = screen.WorkingArea.Left / ratio;
            window.Top = screen.WorkingArea.Top / ratio;
            window.Width = screen.WorkingArea.Width / ratio;
            window.Height = screen.WorkingArea.Height / ratio;
            window.WindowState = WindowState.Maximized;
        Current.ShutdownMode = ShutdownMode.OnMainWindowClose;

    private readonly Color[] _screenColors =
        Colors.LightGray, Colors.DarkGray, 
        Colors.Gray, Colors.SlateGray, Colors.DarkSlateGray


Main window contains ListView that shows the list of ScreenRectangle models from MainWindowModel view model. In order to show window on expected screen, it should have the following properties:


In addition, we remove caption bar and make window non-resizable:


If comment line #45 in App.xaml.cs:

window.WindowState = WindowState.Maximized;

then window will occupy the whole screen except taskbar.


MainWindowModel class implements IMainWindowModel interface:

public interface IMainWindowModel
    /// <summary>
    /// Background color.
    /// </summary>
    Color BackgroundColor { get; }
    /// <summary>
    /// Width of the view.
    /// </summary>
    double ViewWidth { get; set; }
    /// <summary>
    /// Height of the view.
    /// </summary>
    double ViewHeight { get; set; }
    /// <summary>
    /// Set of rectangles.
    /// </summary>
    ObservableCollection<ScreenRectangle> Rectangles { get; }

Background color is used to colorize window and distinct them at different screens. ViewHeight and ViewWidth are bound to attached properties in order to obtain view size in view model (the code is taken from Pushing read-only GUI properties back into ViewModel). ScreenRectangle class looks like derived class from Tuple<string, Rectangle> that implements NotifyPropertyChanged interface:

public class ScreenRectangle : BaseNotifyPropertyChanged
    protected ScreenRectangle()
        Name = string.Empty;
        Bounds = new RectangleF();

    public ScreenRectangle(string name, RectangleF bounds)
        Name = name;
        Bounds = bounds;

    public ScreenRectangle(string name, double left, double top, double width, double height)
        : this(name, new RectangleF((float)left, (float)top, (float)width, (float)height))

    public ScreenRectangle(string name, double width, double height)
        : this(name, new RectangleF(0, 0, (float)width, (float)height))

    #region Public properties

    private string _name;
    private RectangleF _bounds;

    public string Name
        get { return _name; }
        set { SetProperty(ref _name, value); }

    public RectangleF Bounds
        get { return _bounds; }
        set { SetProperty(ref _bounds, value); }

    #endregion Public properties

    public void SetSize(double width, double height)
        Bounds = new RectangleF(Bounds.Location, new SizeF((float)width, (float)height));

Update 1

The sample application was updated. Now it shows more system parameters and some sizes and dimensions are commented.

Image 4

The main changes were done in the constructor of MainWindowModel class.

public MainWindowModel(Color backgroundColor, bool primary, string displayName)
	BackgroundColor = backgroundColor;

	_rectangles = new ObservableCollection<screenrectangle>(new[]
		new ScreenRectangle(ScreenNames.View, ViewWidth, ViewHeight,
			"View uses border with BorderThickness='3', 
             and its size is lesser than screen size at 6px at each dimension")
	if( primary)
		_rectangles.Add(new ScreenRectangle(ScreenNames.PrimaryScreen,
			"'Emulated' screen dimensions for Wpf applications"));
		_rectangles.Add(new ScreenRectangle(ScreenNames.FullPrimaryScreen,
			(float) SystemParameters.FullPrimaryScreenWidth, 
            (float) SystemParameters.FullPrimaryScreenHeight,
			"Height difference with working area height depends on locale"));
		_rectangles.Add(new ScreenRectangle(ScreenNames.VirtualScreen,
			(float) SystemParameters.VirtualScreenLeft, 
            (float) SystemParameters.VirtualScreenTop,
			(float) SystemParameters.VirtualScreenWidth, 
            (float) SystemParameters.VirtualScreenHeight));
		_rectangles.Add(new ScreenRectangle(ScreenNames.WorkingArea,
			SystemParameters.WorkArea.Width, SystemParameters.WorkArea.Height,
			"40px is holded for the taskbar height"));
		_rectangles.Add(new ScreenRectangle(ScreenNames.PrimaryWorkingArea,
			Screen.PrimaryScreen.WorkingArea.Left, Screen.PrimaryScreen.WorkingArea.Top,

	foreach (var screeen in Screen.AllScreens)
		if (!primary && !Equals(screeen.DeviceName, displayName))
		_rectangles.Add(new ScreenRectangle($"Screen \"{screeen.DeviceName}\"", 
			"Physical dimensions"));

Update 2

Recently, we faced up with the issue when fixed size dialog in WPF application at Windows 7/10 English works well, but when user uses Windows 7 Chinese, some controls are cut or not shown. There exists the solution for WinForm applications, but it is not suitable for WPF applications, as they use another scaling approach. The issue was fixed by two steps:

  • Fixed size was replaced by the desired size that is calculated by UserControl.Measuze() method.
  • It was noticed that Full primary screen size is less than Work area size, but difference depends on locale. Let localeDelta = SystemParameters.WorkArea.Height - SystemParameters.FullPrimaryScreenHeight. Then:
    • for Windows 7/10 English locale localeDelta == 13.14, and it depends a little on screen dimensions
    • for Windows 7 Chinese locale localeDelta == 22
    Unfortunately, I didn't find any information about the origin of this value, but it was enough to increase the height of the dialog window by localeDelta.


  • 14th January, 2017: Initial post
  • 23rd May, 2017: Added working area size to window
  • 2nd July, 2017: Added comments to show sizes, updated Git repository


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Written By
Software Developer (Senior)
Ukraine Ukraine
• Have more than 25 years of the architecting, implementing, and supporting various applications from small desktop and web utilities up to full-fledged cloud SaaS systems using mainly Microsoft technology stack and implementing the best practices.
• Have significant experience in the architecting applications starting from the scratch and from the existent application (aka “legacy”) where it is required to review, refactor, optimise the codebase and data structure, migrate to new technologies, implement new features, best practices, create tests and write documentation.
• Have experience in project management, collecting business requirements, creating MVP, working with stakeholders and end users, and tasks and backlog management.
• Have hands-on experience in the setting up CI/CD pipelines, the deploying on-premise and cloud systems both in Azure and AWS, support several environments.
• As Mathematician, I interested much in the theory of automata and computer algebra.

Comments and Discussions

-- There are no messages in this forum --