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

问题描述 投票:19回答:3

我有两个显示器。我想制作一个媒体播放器,并且想在辅助显示器上全屏播放视频。因此,我正在尝试使用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;

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

c# .net wpf media-player mediaelement
3个回答
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;

我使用的窗口的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>

3
投票

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

如果您不能或不想添加整个System.Windows.Forms dll参考,则可以使用WpfScreenHelper by 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窗体屏幕帮助程序。当您有其他WPF引用不能很好地与Forms配合使用时(如WPF Live-Charts),这非常好。


0
投票

我在VS2019中使用了以下内容;

private void MaximizeToSecondaryScreen()
    {
        this.Left = SystemParameters.VirtualScreenLeft;
        this.Top = SystemParameters.VirtualScreenTop;
        this.Height = SystemParameters.VirtualScreenHeight;
        this.Width = SystemParameters.VirtualScreenWidth;
    }
© www.soinside.com 2019 - 2024. All rights reserved.