如何在辅助显示中设置WPF窗口位置

时间:2012-04-02 06:56:33

标签: c# .net wpf media-player mediaelement

我有两个显示器。我想制作一个媒体播放器,我想在我的辅助显示器上全屏播放视频。所以我正在尝试使用WPF制作媒体播放器

以下是我编写的代码

Screen[] _screens = Screen.AllScreens;
System.Drawing.Rectangle ractagle = _screens[1].Bounds;
//player is  my window
player.WindowState = WindowState.Maximized;
player.WindowStyle = WindowStyle.None;

player.Left = ractagle.X;
player.Top = ractagle.Y;


// MediaControl is an media elements
MediaControl.Height = ractagle.Height;
MediaControl.Width = ractagle.Width;

但不知怎的,它只是在我的第一个显示器上播放。 非常感谢任何形式的帮助。

3 个答案:

答案 0 :(得分:18)

您需要确保WindowStartupLocation对于您正在显示的表单设置为Manual

否则你所做的一切都不会影响窗口的位置。

using System.Windows.Forms;
// reference System.Drawing
//

Screen s = Screen.AllScreens[1];

System.Drawing.Rectangle r  = s.WorkingArea;
Me.Top = r.Top;
Me.Left = r.Left;

我使用的Window XAML的标题。

<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="823" WindowStartupLocation="Manual">
    <Canvas Width="743">
        //Controls etc
    </Canvas>
</Window>

答案 1 :(得分:2)

5年后!但对于那些偶然发现我的人来说......

如果您不能或不想添加整个System.Windows.Forms dll参考,则可以WpfScreenHelper使用micdenny(在NuGet中搜索)

  Screen screen = WpfScreenHelper.AllScreens[0];
  Left = screen.Bounds.Left;
  Top = screen.Bounds.Top;
  Width = screen.Bounds.Width;
  Height = screen.Bounds.Height;

Micdenny已经为WPF移植了Windows Forms Screen帮助程序。当你有其他WPF引用不能很好地使用Forms(像WPF Live-Charts)时,这是非常好的。

答案 2 :(得分:0)

我在VS 2019中使用以下内容:

private void MaximizeToSecondaryScreen()
{
    this.Left = SystemParameters.VirtualScreenLeft;
    this.Top = SystemParameters.VirtualScreenTop;
    this.Height = SystemParameters.VirtualScreenHeight;
    this.Width = SystemParameters.VirtualScreenWidth;
}