Set wpf application screen height on secondary monitor on maximize

℡╲_俬逩灬. 提交于 2019-12-11 01:15:19

问题


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>   


来源:https://stackoverflow.com/questions/36619461/set-wpf-application-screen-height-on-secondary-monitor-on-maximize

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!