D.K.
D.K.

Reputation: 426

dual monitor dual window app in wpf

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.

Upvotes: 3

Views: 13303

Answers (4)

Fred
Fred

Reputation: 1

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;
    }

Upvotes: 0

tkelch
tkelch

Reputation: 321

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;

Upvotes: 9

romanoza
romanoza

Reputation: 4862

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
        };


    }

Upvotes: 0

Loren Pechtel
Loren Pechtel

Reputation: 9093

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?

Upvotes: 0

Related Questions