在两个单独的屏幕上加载两个窗口

时间:2012-04-24 14:54:55

标签: c# wpf mvvm

我正在尝试创建一个WPF演示应用程序,其中主屏幕上的主窗口将用于更新演示窗口上的内容,该窗口需要位于辅助屏幕上。我正在寻找可以帮助我做到这一点的示例或代码片段。

enter image description here

此外,我尝试了以下操作但无效:

protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);

            var screens = System.Windows.Forms.Screen.AllScreens;
            if (screens.Length > 1)
            {
                var window = new MainWindow
                {
                    WindowStartupLocation = WindowStartupLocation.Manual,
                    WindowState = WindowState.Maximized,

                    Top = screens[0].WorkingArea.Top,
                    Left = screens[0].WorkingArea.Left,
                    Topmost = true
                };
                window.Show();

                var pesentationWindow = new PresentationWindow
                {
                    WindowStartupLocation = WindowStartupLocation.Manual,
                    ShowInTaskbar = false,
                    Top = screens[1].WorkingArea.Top,
                    Left = screens[1].WorkingArea.Left,
                    Topmost = true
                };
                pesentationWindow.Show();
            }
        }

2 个答案:

答案 0 :(得分:1)

使用System.Windows.Forms.Screen.AllScreens静态属性,您可以计算出有多少个屏幕以及它们的分辨率。找出哪个是PrimaryScreen并将辅助窗口设置为正确的大小和位置。 有关详细信息,请参阅:Trouble creating a WPF window in a secondary monitor

然后简单地创建两个单独的Window类,以不同方式绑定DataContext的绑定。例如:

class DataContext
{
   public string Header { get; set; }
}
<Window Title="Primary Window">
   <TextBox Text="{Binding Header}"/>
</Window>
<Window Title="Presentation Window">
  <TextBlock Content="{Binding Header}"/>
</Window>

答案 1 :(得分:1)

您可以这样使用Screen对象

protected override void OnStartup(StartupEventArgs e)
{
    base.OnStartup(e);

    Window1 w1 = new Window1();
    Window2 w2 = new Window2();


    Screen s1 = Screen.AllScreens[0];
    Screen s2 = Screen.AllScreens[1];

    Rectangle r1 = s1.WorkingArea;
    Rectangle r2 = s2.WorkingArea;

    w1.Top = r1.Top;
    w1.Left = r1.Left;

    w2.Top = r2.Top;
    w2.Left = r2.Left;

    w1.Show();
    w2.Show();

    w2.Owner = w1;


}

请按照此link了解详情。