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