Tuesday, June 15, 2010

WPF - Multiple Display

The Method

Use the System.Windows.Forms.Screen API to setup the displays.

The PrimaryScreen Property is the primary display, Other Displays are given by the AllScreens Property (System.Windows.Forms.Screen[])

Example with three windows (ImageViewer Application)

1)Take the StartUri element out of App.xaml and add the Startup event

<Application x:Class="DicomImageViewer.App"
             xmlns="
http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             StartupUri="MainWindow.xaml"
             Startup="Application_Startup">
    <Application.Resources>

    </Application.Resources>
</Application>

2)Add the Startup event handler

private void Application_Startup(object sender, StartupEventArgs e)
{
    setDisplayRects(MainDisplayWindow, ImageBox1, ImageBox2);
}

2)This is an example of the ImageViewer for triple display
(note if 1 or 2 displays it subdivides the displays)

public void setDisplayRects(MainWindow main, ImageWindow img1, ImageWindow img2)
{
    System.Windows.Forms.Screen s0;
    System.Windows.Forms.Screen s1;
    System.Windows.Forms.Screen s2;
    switch (System.Windows.Forms.Screen.AllScreens.Count())
    {
        case 3:
            s0 = System.Windows.Forms.Screen.PrimaryScreen;
            main.Top = s0.WorkingArea.Top;
            main.Left = s0.WorkingArea.Left;
            main.WindowState = WindowState.Maximized;
            main.Show();

            s1 = System.Windows.Forms.Screen.AllScreens[1];
            img1.Top = s1.WorkingArea.Top;
            img1.Left = s1.WorkingArea.Left;
            img1.WindowState = WindowState.Maximized;
            img1.Show();
            img1.Owner = main;

            s2 = System.Windows.Forms.Screen.AllScreens[2];
            img2.Top = s2.WorkingArea.Top;
            img2.Left = s2.WorkingArea.Left;
            img2.WindowState = WindowState.Maximized;
            img2.Show();
            img2.Owner = main;

            break;

        case 2:
            s0 = System.Windows.Forms.Screen.PrimaryScreen;
            main.Top = s0.WorkingArea.Top;
            main.Left = s0.WorkingArea.Left;
            main.Show();

            s1 = System.Windows.Forms.Screen.AllScreens[1];
            img1.Top = s1.WorkingArea.Top;
            img1.Left = s1.WorkingArea.Left;
            img1.Width = s1.WorkingArea.Width;
            img1.Height = s1.WorkingArea.Height / 2;
            img1.Show();
            img1.Owner = main;

            img2.Top = s1.WorkingArea.Top + s1.WorkingArea.Height / 2;
            img2.Left = s1.WorkingArea.Left;
            img2.Width = s1.WorkingArea.Width;
            img2.Height = s1.WorkingArea.Height / 2;
            img2.Show();
            img2.Owner = main;

            break;

        case 1:
            s0 = System.Windows.Forms.Screen.PrimaryScreen;
            main.Top = s0.WorkingArea.Top;
            main.Left = s0.WorkingArea.Left;
            main.Width = s0.WorkingArea.Width;
            main.Height = s0.WorkingArea.Height / 3;
            main.Show();

            img1.Top = s0.WorkingArea.Top + s0.WorkingArea.Height / 3;
            img1.Left = s0.WorkingArea.Left;
            img1.Width = s0.WorkingArea.Width;
            img1.Height = s0.WorkingArea.Height / 3;
            img1.Show();
            img1.Owner = main;

            img2.Top = s0.WorkingArea.Top + (2 * s0.WorkingArea.Height / 3);
            img2.Left = s0.WorkingArea.Left;
            img2.Width = s0.WorkingArea.Width;
            img2.Height = s0.WorkingArea.Height / 3;
            img2.Show();
            img2.Owner = main;

            break;
    }
}

No comments:

Post a Comment