Set wpf application screen height on secondary mon

2019-07-19 03:12发布

问题:

I want to set up different screen sizes for Different Monitors.

Resolution

primary - 1600*900,

secondary - 1920*1080

My application is working fine on primary screen, but when i drag the application on secondary screen and maximize ,it maximize only as per primary screen height.

I want the application screen size as per current screen.

回答1:

I suggest you to use Screen class from System.Windows.Forms to define whether your application is on the second screen. It is necessary to know when a user moves your application to the second display and to know it, I use LocationChanged event:

Code-behind:

private Screen GetSecondaryScreen()
    {
        foreach (Screen screen in Screen.AllScreens)
        {
            if (screen != Screen.PrimaryScreen)
                return screen;
        }
        return Screen.PrimaryScreen;
    }        

    private void Window_LocationChanged(object sender, EventArgs e)
    {            
        if (Screen.PrimaryScreen != GetSecondaryScreen())
        {
            this.WindowState = WindowState.Maximized;
        }
    }

XAML:

<Window x:Class="DateTimePickerDataGridWPF.MainWindow"
    ...the code omitted for the brevity...
    Title="MainWindow" Height="350" Width="525" LocationChanged="Window_LocationChanged">
</Window>