dual monitor dual window app in wpf

假如想象 提交于 2020-01-04 14:00:35

问题


I'm trying to create a wpf video player with media element. My goal is making muliple windows for that application. each of the windows will show up on different monitors. Like the MainWindow will get the resolution of primary monitor and resize itself to go full screen. The Second window on secondary monitor and so...

So far, I've made the MainWindow fullscreen on the primary monitor. but I've no idea how to show the second window on second monitor with it's resolution. please help.


回答1:


Easiest option is to display it. And after the show() method, resize it.

            // display on second window
        NewWindow win = new NewWindow();
        System.Windows.Forms.Screen s1 = System.Windows.Forms.Screen.AllScreens[2];

        System.Drawing.Rectangle r1 = s1.WorkingArea;
        win.WindowState = System.Windows.WindowState.Normal;
        win.WindowStartupLocation = System.Windows.WindowStartupLocation.Manual;
        win.Top = r1.Top;
        win.Left = r1.Left;

        win.Show();
        win.WindowState = System.Windows.WindowState.Maximized;



回答2:


Each screen has coordinates in space, you don't actually put a window on another screen, at the coordinates of the other screen(s).

On this machine -1280,0 is the left monitor, 0,0 is the main monitor and 1280,0 is the right monitor. Programs with no knowledge of multi-screen operation generally have no problem with this, although old enough stuff can do some strange things when placed on the left monitor (trying to keep pop-ups from going off the edge of the screen, unaware that negative coordinates can be visible.)

As for locating the screens, see: How to easily find screen location of form Location in multi-monitor environment?




回答3:


You can try something like this:

    private System.Windows.Forms.Screen findScreen(string screenName) {
        System.Windows.Forms.Screen res = System.Windows.Forms.Screen.AllScreens.FirstOrDefault(s => s.DeviceName == screenName);
        if (res == null)
            res = System.Windows.Forms.Screen.AllScreens[0];
        return res;
    }


    private void setupForms() {
        System.Windows.Forms.Screen mainScreen = findScreen(@"\\.\DISPLAY2");
        FrmMain frmMain = new FrmMain()
        {
            WindowStartupLocation = WindowStartupLocation.Manual,
            WindowState = System.Windows.WindowState.Normal,
            Left = mainScreen.WorkingArea.Left,
            Top = mainScreen.WorkingArea.Top,
            Width = mainScreen.WorkingArea.Width,
            Height = mainScreen.WorkingArea.Height
        };

        System.Windows.Forms.Screen secondaryScreen = findScreen(@"\\.\DISPLAY1");
        FrmSecondary frmSecondary = new FrmSecondary()
        {
            WindowStartupLocation = WindowStartupLocation.Manual,
            WindowState = System.Windows.WindowState.Normal,
            Left = secondaryScreen.WorkingArea.Left,
            Top = secondaryScreen.WorkingArea.Top,
            Width = secondaryScreen.WorkingArea.Width,
            Height = secondaryScreen.WorkingArea.Height
        };


    }



回答4:


I will update @romanoza's answer

private void SetupWindows()
    {
        System.Windows.Forms.Screen mainScreen = Screen.AllScreens[0]; ;
        WinLeft leftWin = new WinLeft()
        {
            WindowStartupLocation = WindowStartupLocation.Manual,
            WindowState = System.Windows.WindowState.Normal,
            Left = mainScreen.WorkingArea.Left,
            Top = mainScreen.WorkingArea.Top,
            Width = mainScreen.WorkingArea.Width,
            Height = mainScreen.WorkingArea.Height
        };           

        System.Windows.Forms.Screen secondaryScreen = Screen.AllScreens[1];
        WinRight rightWin = new WinRight()
        {
            WindowStartupLocation = WindowStartupLocation.Manual,
            WindowState = System.Windows.WindowState.Normal,
            Left = secondaryScreen.WorkingArea.Left,
            Top = secondaryScreen.WorkingArea.Top,
            Width = secondaryScreen.WorkingArea.Width,
            Height = secondaryScreen.WorkingArea.Height
        };


        leftWin.Show();
        leftWin.WindowState = WindowState.Maximized;
        rightWin.Show();
        rightWin.WindowState = WindowState.Maximized;
        rightWin.Owner = leftWin;
    }


来源:https://stackoverflow.com/questions/25091414/dual-monitor-dual-window-app-in-wpf

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