use*_*295 27 .net windows wpf multiple-monitors fullscreen
如何编写dotNet Windows(或WPF)应用程序以使其在辅助监视器上全屏显示?
gra*_*tnz 33
最大化窗口到辅助监视器的扩展方法(如果有).不要假设辅助监视器是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;
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
我注意到一个主张在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;
}
Run Code Online (Sandbox Code Playgroud)
在辅助显示器上替换任何代号
请参阅此代码项目文章.
其中的代码可以使用,但默认为主监视器.要更改此设置,您需要替换对GetSystemMetrics的调用将调用GetMonitorInfo.使用GetMonitorInfo,您可以获取适当的RECT以传递给SetWindowPos.
GetMonitorInfo允许您获取任何监视器的RECT.
有一篇关于多监视器设置中的位置应用程序的MSDN文章可能有助于更好地解释事情.
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;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
25798 次 |
| 最近记录: |