简体   繁体   中英

WPF Application for Multi-Monitor and One CPU

I have developed WPF Application which is now going to be run Multi Monitor Env. But I don't know, How to set my application that can run in such Env.

在此处输入图片说明

My Case is like above image, One CPU and 3 Screen. Now my client want to run same application in these screens. And also for each screen he can operate different operation.

So i want to know is should i start 3 different instance of my application. but in that case on screen 1 user should not aware about other instance of Screen 2 and Screen 3 user.

Or there is any alternate way. As i don't know how to work with multiple screen.please help me

Khusbhu,

I am not sure if you really want to create 3 instances, as this scenario is for only single use who is working on single system, this is because the CPU have only one Keyboard and Mouse, so I don't understand why client what to run on 3 monitors same application.

If yes that is the case, then yes you will need 3 instances, its simple as 3 programs running on 3 different screens.

For more idea take a look on this http://www.pugetsystems.com/labs/articles/How-to-Set-Up-Multiple-Monitors-140/

Thanks Nipun

It seems a little overkill to fire up three instances of the same application if all you want to do is handle multiple monitors. It depends on the design of your GUI, but you can use Screen.AllScreens to find out about the screens that the system has and layout your windows accordingly. Screen has a few properties like PrimaryScreen and WorkingArea which will help you with this. I know you are using WPF, so you will need a reference to System.Windows.Forms in your project but I have done this without issues in the past.

Your problem is very simple if you make a MVVM application. For 1 ViewModel (the fonctionnality), you can attach many View (UI). In your case, on a single application, create one instance of a ViewModel and set the DataContext of each View to this viewmodel. Each view can be on different monitor, sizing different, content different ...

Full Example

First, create a model ( = Data)

using System;

namespace WpfApplication1
{
    public class MyModel
    {
        public String Text1 { get; set; }

        public Int32 Int1 { get; set; }

        public Int32 Int2 { get; set; }
    }
}

Then, a ViewModel (how the data interact and must live)

using System;

namespace WpfApplication1
{
    public class MyViewModel
    {
        private MyModel myModel;

        public MyViewModel()
        {
            this.myModel = new MyModel() { Int1 = 1, Int2 = 12, Text1 = "toto" };
        }

        public String MyText
        {
            get { return this.myModel.Text1; }
            set { this.myModel.Text1 = value; }
        }

        public Int32 MyInt1
        {
            get { return this.myModel.Int1; }
            set { this.myModel.Int1 = value; }
        }

        public Int32 MyInt2
        {
            get { return this.myModel.Int2; }
            set { this.myModel.Int2 = value; }
        }
    }
}

Then, first view (UI, how the data must be shown)

<Window x:Class="WpfApplication1.View1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="View1" Height="300" Width="300"
        Background="{StaticResource {x:Static SystemColors.ControlBrushKey}}"
        xmlns:local="clr-namespace:WpfApplication1"
        >

    <Window.DataContext>
        <local:MyViewModel />
    </Window.DataContext>

    <Grid>
        <TextBox Text="{Binding MyText, UpdateSourceTrigger=PropertyChanged}" />
    </Grid>
</Window>

The code behind for this view (only UI code)

using System.Windows;

namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for View1.xaml
    /// </summary>
    public partial class View1 : Window
    {
        public View1()
        {
            InitializeComponent();
        }

        public View1(MyViewModel viewModel)
            : this()
        {
            this.DataContext = viewModel;
        }
    }
}

The 2nd view (different of the first)

<Window x:Class="WpfApplication1.View2"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="View2" Height="300" Width="300"
        Background="{StaticResource {x:Static SystemColors.ControlBrushKey}}"
        xmlns:local="clr-namespace:WpfApplication1"
        >

    <Window.DataContext>
        <local:MyViewModel />
    </Window.DataContext>

    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>
        <TextBox Text="{Binding MyText, UpdateSourceTrigger=PropertyChanged}" />
        <Slider Value="{Binding MyInt1, UpdateSourceTrigger=PropertyChanged}"  Grid.Row="1" />
    </Grid>
</Window>

using System.Windows;

namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for View2.xaml
    /// </summary>
    public partial class View2 : Window
    {
        public View2()
        {
            InitializeComponent();
        }

        public View2(MyViewModel viewModel)
            : this()
        {
            this.DataContext = viewModel;
        }
    }
}

The 3rd view (different of the first and the second)

<Window x:Class="WpfApplication1.View3"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="View2" Height="300" Width="300"
        Background="{StaticResource {x:Static SystemColors.ControlBrushKey}}"
        xmlns:local="clr-namespace:WpfApplication1"
        >

    <Window.DataContext>
        <local:MyViewModel />
    </Window.DataContext>

    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>
        <TextBox Text="{Binding MyText, UpdateSourceTrigger=PropertyChanged}" />
        <TextBox Text="{Binding MyInt1, UpdateSourceTrigger=PropertyChanged}" Grid.Row="1" />
        <TextBox Text="{Binding MyInt2, UpdateSourceTrigger=PropertyChanged}" Grid.Row="2" />

    </Grid>

</Window>

using System.Windows;

namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for View3.xaml
    /// </summary>
    public partial class View3 : Window
    {
        public View3()
        {
            InitializeComponent();
        }

        public View3(MyViewModel viewModel)
            : this()
        {
            this.DataContext = viewModel;
        }
    }
}

And to finish, your starting point, where all views are called :

public MainWindow()
{
    MyViewModel myOnlyViewModel = new MyViewModel();
    View1 view1 = new View1(myOnlyViewModel);
    view1.Show();
    View2 view2 = new View2(myOnlyViewModel);
    view2.Show();
    View3 view3 = new View3(myOnlyViewModel);
    view3.Show();
}

As you can see, each change in a UI is shown in others UI (You don't need to launch 3 instances of your app, only 1 !). For example, moving the slider will modify value of MyInt1 in other views. All you have to do is to design all Views you want, and always think to separate how the data live and how the data are shown

If my understanding is correct. You need to have one exe and multiple windows for each monitors/users.

Say for example if you have a Button1 and click event that create a

Window1 obje = new Window1();Obje.Show();

Create a New window instance on each click, and give them an id for each window and do your process. If you want to persist each window instance in MainWindow keep them in List or Dictionary Have a class lever variables

Private Disctionary OpenWIndows = new Disctionary ();Private int Counter = 0;Window1 obje = new Window1();Counter ++;Obje.WIndowId = Counter;OpenWIndows.Add(Counter, obje);Obje.Show();

So now OpenWIndows are available for you Inside Window1 class you can write a code for specific type of user based on your counter.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM