C# 如何在辅助显示中设置 WPF 窗口位置
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9972044/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set WPF window position in secondary display
提问by rokonoid
I have two displays. I want to make a media player and I want to play video full screen on my secondary display. So I'm trying to make a media player using WPF
我有两个显示器。我想做一个媒体播放器,我想在我的辅助显示器上全屏播放视频。所以我正在尝试使用 WPF 制作媒体播放器
Here is the code so far I wrote
这是到目前为止我写的代码
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;
But somehow it's just playing on my first display. Any kind of help is very much appreciated.
但不知何故,它只是在我的第一台显示器上播放。非常感谢任何形式的帮助。
采纳答案by Paul Farry
You need to make sure that the WindowStartupLocationis set to Manualfor the form you are displaying
您需要确保为您正在显示的表单WindowStartupLocation设置Manual
Otherwise nothing you do will have any effect on the position of the window.
否则,您所做的任何事情都不会对窗口的位置产生任何影响。
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;
This header of the XAML of the Window I used.
我使用的窗口的 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>
回答by Eclectic
5 years later! But for anyone else that stumbles across this as I did ...
5年后!但是对于像我一样偶然发现这个问题的其他人......
If you can't or do not want to add the entire System.Windows.Forms dll reference, you can use WpfScreenHelperby micdenny(search in NuGet)
如果您不能或不想添加整个 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 has ported the Windows Forms Screen helper for WPF. This is excellent when you have other WPF refs that do not play nice with Forms (Like WPF Live-Charts).
Micdenny 已为 WPF 移植了 Windows 窗体屏幕助手。当您有其他与表单(如 WPF 实时图表)不兼容的 WPF 参考时,这非常好。
回答by Code T
I used following in VS2019;
我在 VS2019 中使用了以下内容;
private void MaximizeToSecondaryScreen()
{
this.Left = SystemParameters.VirtualScreenLeft;
this.Top = SystemParameters.VirtualScreenTop;
this.Height = SystemParameters.VirtualScreenHeight;
this.Width = SystemParameters.VirtualScreenWidth;
}

