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

rok*_*oid 19 .net c# wpf mediaelement media-player

我有两个显示器.我想制作一个媒体播放器,我想在我的辅助显示器上全屏播放视频.所以我正在尝试使用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;
Run Code Online (Sandbox Code Playgroud)

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

Pau*_*rry 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;
Run Code Online (Sandbox Code Playgroud)

我使用的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>
Run Code Online (Sandbox Code Playgroud)


Ecl*_*tic 6

5年后!但是对于像我一样偶然发现这个问题的其他人......

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

  Screen screen = WpfScreenHelper.AllScreens[0];
  Left = screen.Bounds.Left;
  Top = screen.Bounds.Top;
  Width = screen.Bounds.Width;
  Height = screen.Bounds.Height;
Run Code Online (Sandbox Code Playgroud)

Micdenny 已为 WPF 移植了 Windows 窗体屏幕助手。当您有其他与表单(如 WPF 实时图表)不兼容的 WPF 参考时,这非常好。