Search code examples
c#wpfscreenlauncherlaunching-application

how to launch a second application in a second screen in WPF?


I actually work on two little wpf application for track, one is on the principal computer, you can wrote the time of the pilot on, the second must display in a big screen connect to the principal computer in hdmi and just display the time to the pilot.

I have ever made everything and all work fine.

But when I launch the first application, I want the second application automaticaly launch in the second screen. I only find a solution with the Forms packages like :

System.Windows.Forms.Screen.AllScreens

But I work with WPF so I can't access to System.Windows.Forms.

Anyone as an idea of possible solution? Thanks a lot!


Solution

  • Even though it is a WPF app you can still reference System.Windows.Forms. Simply right click on the project in solution explorer, add references, and select System.Windows.Forms.

    Then use something like this: (you will also need a reference to System.Drawing)

    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            this.MaximizeToSecondaryMonitor();
        }
    
        public void MaximizeToSecondaryMonitor()
        {
            var secondaryScreen = Screen.AllScreens.Where(s => !s.Primary).FirstOrDefault();
    
            if (secondaryScreen != null)
            {
                var workingArea = secondaryScreen.WorkingArea;
                this.Left = workingArea.Left;
                this.Top = workingArea.Top;
                this.Width = workingArea.Width;
                this.Height = workingArea.Height;
    
                if (this.IsLoaded)
                {
                    this.WindowState = WindowState.Maximized;
                }
            }
        }
    }
    

    code taken from here