26

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.

K Mehta
  • 10,323
  • 4
  • 46
  • 76
A N M Bazlur Rahman
  • 2,280
  • 6
  • 38
  • 51

5 Answers5

25

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>
Tobias
  • 35,886
  • 1
  • 20
  • 23
Paul Farry
  • 4,730
  • 2
  • 35
  • 61
  • hey its not working, I double checked, I set the WindowStartupLocation Manual – A N M Bazlur Rahman Apr 02 '12 at 18:46
  • I knocked up a small sample to demonstrate and it works for me. What isn't working? See edit for the XAML I used. – Paul Farry Apr 02 '12 at 22:46
  • Nice solution. However it only gives you a full screen window on the secondary screen (you still see the windows border etc.). In case someone else stumbles over this post and wants complete full screen I found the solution here: http://mostlytech.blogspot.de/2008/01/maximizing-wpf-window-to-second-monitor.html – Heribert Jun 10 '15 at 10:21
9

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).

Eclectic
  • 847
  • 1
  • 11
  • 26
  • 1
    Not working for me, I had to change the code for it to compile. (ex: Screen screen = Screen.AllScreens.ToList()[0]; ...). Anyway, the change of index 0 to 1 didn't have an effect. Is something else needed for it to work? I installed the 0.3.0 (current version) of WpfScreenHelper. – Exel Gamboa Apr 05 '18 at 14:49
  • 1
    @ExelGamboa Sorry it is not working for you. In my case, Nuget installed fine. I then built a ScreenManager class that only uses WpfScreenHelper and generic collections. I have "public List Screens;" which I populate in the constructor with "Screens = new List(Screen.AllScreens);" I then created a method "public Screen GetScreen(int index){ return Screens[index]; }", I then position and size the App with a Rect populated from returned "screen.Bounds". I had no build issues at all. What else have you installed / referenced? Perhaps there is a conflict somewhere? – Eclectic Apr 06 '18 at 07:02
  • Maybe that's true. Thanks for the detail, I will try it again like that and check if I did something different. – Exel Gamboa Apr 06 '18 at 16:01
  • There can be problems with this approach if the secondary monitor has a different scale ratio or DPI to the primary monitor. On identical displays this works perfectly, though. – GrandMasterFlush Sep 15 '21 at 10:04
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;
}
Pang
  • 9,564
  • 146
  • 81
  • 122
Code T
  • 61
  • 3
  • This is useful to know, some of the previous answers around this topic don't work if additional monitors are a different size and have a different DPI / scale factor. – GrandMasterFlush Sep 15 '21 at 10:00
  • This worked for me but if your screens have different refresh rates it will cause the Canvas to lag. –  Mar 15 '23 at 06:40
3

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;    
        }
     }
2

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;
}
Icad
  • 101
  • 1
  • 6