.net 在辅助监视器上全屏显示
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/753552/
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
Going fullscreen on secondary monitor
提问by user91295
How can you program a dotNet Windows (or WPF) Application in order to let it going fullscreen on the secondary monitor?
您如何编写 dotNet Windows(或 WPF)应用程序以使其在辅助监视器上全屏显示?
回答by grantnz
Extension method to Maximize a window to the secondary monitor (if there is one). Doesn't assume that the secondary monitor is System.Windows.Forms.Screen.AllScreens[2];
将窗口最大化到辅助监视器的扩展方法(如果有)。不假设辅助监视器是 System.Windows.Forms.Screen.AllScreens[2];
using System.Linq;
using System.Windows;
namespace ExtendedControls
{
static public class WindowExt
{
// NB : Best to call this function from the windows Loaded event or after showing the window
// (otherwise window is just positioned to fill the secondary monitor rather than being maximised).
public static void MaximizeToSecondaryMonitor(this Window window)
{
var secondaryScreen = System.Windows.Forms.Screen.AllScreens.Where(s => !s.Primary).FirstOrDefault();
if (secondaryScreen != null)
{
if (!window.IsLoaded)
window.WindowStartupLocation = WindowStartupLocation.Manual;
var workingArea = secondaryScreen.WorkingArea;
window.Left = workingArea.Left;
window.Top = workingArea.Top;
window.Width = workingArea.Width;
window.Height = workingArea.Height;
// If window isn't loaded then maxmizing will result in the window displaying on the primary monitor
if ( window.IsLoaded )
window.WindowState = WindowState.Maximized;
}
}
}
}
回答by Alex_P
For WPF apps look at this post. Ultimately it depends on when the WindowState is set to Maximized. If you set it in XAML or in window constructor (i.e. before the window is loaded) it will always be maximized onto primary display. If, on the other hand, you set WindowState to Maximized when the window is loaded - it will maximise on the screen on which it was maximized before.
对于 WPF 应用程序,请查看这篇文章。最终取决于 WindowState 何时设置为最大化。如果您在 XAML 或窗口构造函数中设置它(即在加载窗口之前),它将始终最大化显示在主显示器上。另一方面,如果您在加载窗口时将 WindowState 设置为 Maximized - 它将在之前最大化的屏幕上最大化。
回答by Jay Evans
I notice an answer which advocates setting the position in the Loaded event, but this causes flicker when the window is first shown normal then maximized. If you subscribe to the SourceInitialized event in your constructor and set the position in there it will handle maximizing onto secondary monitors without flicker - I'm assuming WPF here
我注意到一个主张在 Loaded 事件中设置位置的答案,但是当窗口首先显示为正常然后最大化时,这会导致闪烁。如果您在构造函数中订阅 SourceInitialized 事件并在其中设置位置,它将处理最大化到辅助监视器而不会闪烁 - 我在这里假设 WPF
public MyWindow()
{
SourceInitialized += MyWindow_SourceInitialized;
}
void MyWindow_SourceInitialized(object sender, EventArgs e)
{
Left = 100;
Top = 50;
Width = 800;
Height = 600;
WindowState = WindowState.Maximized;
}
Substitute coords for any on your secondary monitor
替换辅助显示器上的任何坐标
回答by Reed Copsey
请参阅此代码项目文章。
The code in there will work, but default to your primary monitor. To change this, you'll need to replace the calls to GetSystemMetrics will calls to GetMonitorInfo. Using GetMonitorInfo, you can get the appropriate RECT to pass to SetWindowPos.
那里的代码将起作用,但默认为您的主显示器。要更改此设置,您需要将调用 GetSystemMetrics 替换为调用GetMonitorInfo。使用 GetMonitorInfo,您可以获得适当的 RECT 以传递给 SetWindowPos。
GetMonitorInfo allows you to get the RECT for any monitor.
GetMonitorInfo 允许您获取任何监视器的 RECT。
There is an MSDN Article on Position Apps in Multi-Monitor Setupsthat might help explain things a bit better.
有一篇关于多显示器设置中的位置应用程序的MSDN 文章可能有助于更好地解释事情。
回答by Pavel Chuchuva
private void Form1_Load(object sender, EventArgs e)
{
this.FormBorderStyle = FormBorderStyle.None;
this.Bounds = GetSecondaryScreen().Bounds;
}
private Screen GetSecondaryScreen()
{
foreach (Screen screen in Screen.AllScreens)
{
if (screen != Screen.PrimaryScreen)
return screen;
}
return Screen.PrimaryScreen;
}
回答by sean e
It's not clear from your question if you are looking for a way to move the window to the secondary monitor and then go fullscreen, or if you are just looking to support fullscreen mode on whatever monitor the window is on (which may be primary or secondary).
如果您正在寻找一种将窗口移动到辅助监视器然后全屏显示的方法,或者您只是希望在窗口所在的任何监视器上支持全屏模式(可能是主监视器或辅助监视器),您的问题尚不清楚)。
If the later, for a WPF window, though not quite the same as fullscreen mode, you can remove the borders when it is maximized and restore the border when not maximized. No need to check for which monitor, etc. The display of the caption/title bar is controlled by the border state.
如果是后者,对于 WPF 窗口,虽然与全屏模式不太一样,但您可以在最大化时去除边框,并在未最大化时恢复边框。无需检查哪个监视器等。字幕/标题栏的显示由边框状态控制。
protected override void OnStateChanged(EventArgs e)
{
if (WindowState == WindowState.Maximized)
{
if (WindowStyle.None != WindowStyle)
WindowStyle = WindowStyle.None;
}
else if (WindowStyle != WindowStyle.SingleBorderWindow)
WindowStyle = WindowStyle.SingleBorderWindow;
base.OnStateChanged(e);
}
Credit goes to Pavel for his Forms-based answer in the current question and to Nir for his answer in this question.
感谢 Pavel 在当前问题中基于表单的回答,感谢 Nir 在这个问题中的回答。
回答by Juan Carlos Velez
In WPF: Set the WindowState property in Normal (not Maximixed) and create the event Loaded. In the event write this code:
在 WPF 中:将 WindowState 属性设置为 Normal(不是 Maximixed)并创建事件 Loaded。在事件写这个代码:
this.Left = SystemParameters.PrimaryScreenWidth + 100;
this.WindowState = System.Windows.WindowState.Maximized;

