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

本文关键字:设置 WPF 窗口 位置 显示 | 更新日期: 2023-09-27 18:29:39

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

但不知怎么的,它只是在我的第一次展示中播放。任何形式的帮助都将不胜感激。

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

您需要确保显示的表单的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>

5年后!但对于其他像我一样偶然发现这一点的人来说。。。

如果您不能或不想添加整个System.Windows.Forms dll引用,您可以使用micdenny的WpfScreenHelper(在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裁判对表单不太好时,这是非常好的(比如WPF实时图表)。

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

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

我的解决方案结合了这些想法,并确保辅助屏幕存在:

    using System.Linq;
    // ...
    public partial class MainWindow : Window
    {
        public MainWindow()
        {                
            InitializeComponent();
            var screens = System.Windows.Forms.Screen.AllScreens;
            var firstSecondary = screens.FirstOrDefault(s => s.Primary == false);
            if (firstSecondary != null)
            {
                WindowStartupLocation = WindowStartupLocation.Manual; 
                // Ensure Window is minimzed on creation
                WindowState = WindowState.Minimized; XAML
                // Define Position on Secondary screen, for "Normal" window-mode
                // ( Here Top/Left-Position )
                Left = firstSecondary.Bounds.Left;
                Top = firstSecondary.Bounds.Top;
            }            
            Loaded += MainWindow_Loaded;
        }    
        private void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            // Maximize after position is defined in constructor
            WindowState = WindowState.Maximized;    
        }
     }

如果立即将窗口设置为最大化,它将无法工作。使用加载的事件,您就不需要Windows窗体

var secondaryScreenLeft = SystemParameters.PrimaryScreenWidth - SystemParameters.VirtualScreenWidth;
window.WindowStartupLocation = WindowStartupLocation.Manual;
window.Left = secondaryScreenLeft ;
window.Top = 0;
window.Loaded += Window_Loaded;
private void Window_Loaded(object sender, RoutedEventArgs e)
{
    var senderWindow = sender as Window;
    senderWindow.WindowState = WindowState.Maximized;
}