none
Show and Maximize WPF Window on a specific screen

    Question

  • I'm using the following method to display a WPF Window on a specific screen:

    private void ShowOnMonitor(int monitor,Window window)
    {
        Screen[] screens = Screen.AllScreens;
    
        window.WindowStyle = WindowStyle.None;
        window.WindowStartupLocation = WindowStartupLocation.Manual;
    
        window.Left = screens[monitor].Bounds.Left;
        window.Top = screens[monitor].Bounds.Top;
        //window.WindowState = WindowState.Maximized;
        window.Show();
    }

    The problem is I need the Window to be Maximized to fill the whole screen but as soon as I set:

    window.WindowState = WindowState.Maximized;
    It keeps showing the Window Maximized but only on the first screen no matter what number I pass through to the ShowOnMonitor() method.

    Monday, January 13, 2014 7:03 AM

Answers

  • Hi,

    Found the answer for this problem from the below link

    http://mostlytech.blogspot.in/2008/01/maximizing-wpf-window-to-second-monitor.html

    We cannot maximize the window until the window is loaded when using multiple screens. so hook up a window loaded event and set the window state to Maximized.

    private void ShowOnMonitor(int monitor, Window window) { var screen = ScreenHandler.GetScreen(monitor); var currentScreen = ScreenHandler.GetCurrentScreen(this); window.WindowState = WindowState.Normal; window.Left = screen.WorkingArea.Left; window.Top = screen.WorkingArea.Top; window.Width = screen.WorkingArea.Width; window.Height = screen.WorkingArea.Height; window.Loaded += Window_Loaded; }

    /*You can use this event for all the Windows*/ private void Window_Loaded(object sender, RoutedEventArgs e) { var senderWindow = sender as Window; senderWindow.WindowState = WindowState.Maximized; }

    ScreenHandler.cs

    public static class ScreenHandler
        {
            public static Screen GetCurrentScreen(Window window)
            {
                var parentArea = new System.Drawing.Rectangle((int)window.Left, (int)window.Top, (int)window.Width, (int)window.Height);
                return Screen.FromRectangle(parentArea);
            }
    
            public static Screen GetScreen(int requestedScreen)
            {
                var screens = Screen.AllScreens;
                var mainScreen = 0;
                if (screens.Length > 1 && mainScreen < screens.Length)
                {
                    return screens[requestedScreen];
                }
                return screens[0];
            }
        }

    Have a nice day :)


    srithar

    • Marked as answer by Denys W Monday, January 13, 2014 7:47 AM
    Monday, January 13, 2014 7:28 AM

All replies

  • Hi,

    Found the answer for this problem from the below link

    http://mostlytech.blogspot.in/2008/01/maximizing-wpf-window-to-second-monitor.html

    We cannot maximize the window until the window is loaded when using multiple screens. so hook up a window loaded event and set the window state to Maximized.

    private void ShowOnMonitor(int monitor, Window window) { var screen = ScreenHandler.GetScreen(monitor); var currentScreen = ScreenHandler.GetCurrentScreen(this); window.WindowState = WindowState.Normal; window.Left = screen.WorkingArea.Left; window.Top = screen.WorkingArea.Top; window.Width = screen.WorkingArea.Width; window.Height = screen.WorkingArea.Height; window.Loaded += Window_Loaded; }

    /*You can use this event for all the Windows*/ private void Window_Loaded(object sender, RoutedEventArgs e) { var senderWindow = sender as Window; senderWindow.WindowState = WindowState.Maximized; }

    ScreenHandler.cs

    public static class ScreenHandler
        {
            public static Screen GetCurrentScreen(Window window)
            {
                var parentArea = new System.Drawing.Rectangle((int)window.Left, (int)window.Top, (int)window.Width, (int)window.Height);
                return Screen.FromRectangle(parentArea);
            }
    
            public static Screen GetScreen(int requestedScreen)
            {
                var screens = Screen.AllScreens;
                var mainScreen = 0;
                if (screens.Length > 1 && mainScreen < screens.Length)
                {
                    return screens[requestedScreen];
                }
                return screens[0];
            }
        }

    Have a nice day :)


    srithar

    • Marked as answer by Denys W Monday, January 13, 2014 7:47 AM
    Monday, January 13, 2014 7:28 AM
  • Thank you

    I found another post on MSDN which also works:

            private void ShowOnMonitor(int monitor,Window window)
            {
                Screen[] screens = Screen.AllScreens;
    
                window.WindowStyle = WindowStyle.None;
                window.WindowStartupLocation = WindowStartupLocation.Manual;
    
                window.Left = screens[monitor].Bounds.Left;
                window.Top = screens[monitor].Bounds.Top;
    
                window.SourceInitialized += (snd, arg) => 
                    window.WindowState = WindowState.Maximized;
    
                window.Show();
            }

     

    Monday, January 13, 2014 7:49 AM
  • Thanks for finding this.  

    I am running into what I'm sure is a problem with an obvious solution but where do I run ShowOnMonitor(monitor,window)? after InitializeComponent() in the MainWindow() class function but that doesn't seem to be the case.     I thought I would just need to run ShowOnMonitor(0,MainWindow); but it's saying 'MainWindow is a type not a variable' which is true, so I'm not sure how to access to the current MainWindow. 

    EDIT:

    Found the solution.  'this' is the window to pass.   ShowOnMonitor(0,this);

    Also I tweaked ScreenHandler.cs:

                if (screens.Length > 1 && mainScreen < screens.Length)
                {
                    if (screens.Length > requestedScreen)
                    {
                        return screens[requestedScreen];
                    }
                    else
                    {
                        return screens[screens.Length - 1];
                    }
                }
    This conditional ensures that the requested monitor is within bounds and avoids a 'dumb' crash in case a config xml or startup flag is malformed. 
    Wednesday, November 19, 2014 7:54 PM