How to set WPF window position in secondary display

29,888

Solution 1

You need to make sure that the WindowStartupLocation is set to Manual for the form you are displaying

Otherwise nothing you do will have any effect on the position of the window.

using System.Windows.Forms;
// reference System.Drawing
//

Screen s = Screen.AllScreens[1];

System.Drawing.Rectangle r  = s.WorkingArea;
Me.Top = r.Top;
Me.Left = r.Left;

This header of the XAML of the Window I used.

<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="823" WindowStartupLocation="Manual">
    <Canvas Width="743">
        //Controls etc
    </Canvas>
</Window>

Solution 2

5 years later! But for anyone else that stumbles across this as I did ...

If you can't or do not want to add the entire System.Windows.Forms dll reference, you can use WpfScreenHelper by micdenny (search in NuGet)

  Screen screen = WpfScreenHelper.AllScreens[0];
  Left = screen.Bounds.Left;
  Top = screen.Bounds.Top;
  Width = screen.Bounds.Width;
  Height = screen.Bounds.Height;

Micdenny has ported the Windows Forms Screen helper for WPF. This is excellent when you have other WPF refs that do not play nice with Forms (Like WPF Live-Charts).

Solution 3

I use the following in VS 2019:

private void MaximizeToSecondaryScreen()
{
    this.Left = SystemParameters.VirtualScreenLeft;
    this.Top = SystemParameters.VirtualScreenTop;
    this.Height = SystemParameters.VirtualScreenHeight;
    this.Width = SystemParameters.VirtualScreenWidth;
}

Solution 4

If you set the window to maximized immediately, it wont work. Use the loaded event and you wont need Windows Forms

var secondaryScreenLeft = SystemParameters.PrimaryScreenWidth - SystemParameters.VirtualScreenWidth;

window.WindowStartupLocation = WindowStartupLocation.Manual;
window.Left = secondaryScreenLeft ;
window.Top = 0;
window.Loaded += Window_Loaded;

private void Window_Loaded(object sender, RoutedEventArgs e)
{
    var senderWindow = sender as Window;
    senderWindow.WindowState = WindowState.Maximized;
}

Solution 5

My solution combines the ideas and ensures, that secondary screen is there:

    using System.Linq;
    // ...
    public partial class MainWindow : Window
    {
        public MainWindow()
        {                
            InitializeComponent();

            var screens = System.Windows.Forms.Screen.AllScreens;
            var firstSecondary = screens.FirstOrDefault(s => s.Primary == false);
            if (firstSecondary != null)
            {
                WindowStartupLocation = WindowStartupLocation.Manual; 
                // Ensure Window is minimzed on creation
                WindowState = WindowState.Minimized; XAML
                // Define Position on Secondary screen, for "Normal" window-mode
                // ( Here Top/Left-Position )
                Left = firstSecondary.Bounds.Left;
                Top = firstSecondary.Bounds.Top;
            }            
            Loaded += MainWindow_Loaded;
        }    

        private void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            // Maximize after position is defined in constructor
            WindowState = WindowState.Maximized;    
        }
     }
Share:
29,888
A N M Bazlur Rahman
Author by

A N M Bazlur Rahman

root@bazlur:~$ cat bazlur.txt I have more than nine years of experience coding in Java, which includes numerous projects – academic, professional, and personal. Amongst these projects are web applications, desktop applications, and mobile applications. From this medley of projects, I have gained a strong familiarity with the tools available to the Java developer – Spring is one of them. In addition to my regular work, I also mentor, write and contribute to the open-source project. I am the founder and current moderator of a Java user group in Bangladesh, where I organize technical sessions and share Java-related knowledge to the community. It helps the developers come under one roof to talk with each other, share their knowledge, which ultimately lifts everyone up. I was named Most Valuable Blogger (MVP) at DZone, one of the most recognized technology publishers in the world. I was also the winner of two article writing contests organized by DZone. Lately, I have joined the AdoptOpenJDK project and started contributing. Besides, I published four books about the Java programming language; one of them was on the bestseller list in Bangladesh.

Updated on July 19, 2022

Comments

  • A N M Bazlur Rahman
    A N M Bazlur Rahman almost 2 years

    I have two displays. I want to make a media player and I want to play video full screen on my secondary display. So I’m trying to make a media player using WPF

    Here is the code so far I wrote

    Screen[] _screens = Screen.AllScreens;
    System.Drawing.Rectangle ractagle = _screens[1].Bounds;
    //player is  my window
    player.WindowState = WindowState.Maximized;
    player.WindowStyle = WindowStyle.None;
    
    player.Left = ractagle.X;
    player.Top = ractagle.Y;
    
    
    // MediaControl is an media elements
    MediaControl.Height = ractagle.Height;
    MediaControl.Width = ractagle.Width;
    

    But somehow it’s just playing on my first display. Any kind of help is very much appreciated.