从C#代码以编程方式控制扩展监视器 [英] control extended monitor programatically from c# code
本文介绍了从C#代码以编程方式控制扩展监视器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
请控制我的扩展显示器,并在主显示器上显示两个不同的应用程序,在外部显示器上显示另一个应用程序,我该如何使用c#代码执行此操作,我需要在主显示器上显示特定的窗口,并在主显示器上显示另一个窗口在Advanced
Please I need to control my extended monitor and to show two different application one on main and another on the external monitor, how can i do it using c# code I need to show specific window on the main , and another one on the extended thank you in advanced
推荐答案
您好,谢谢.
这是将表单移动到第一个非主屏幕上的扩展方法.
它返回一个 Nullable< bool> ,以告知表单是否已移动,未移动或是否发生错误.
Hi there.
This is an extension method to move a form onto the first non-primary screen.
It returns a Nullable<bool> to tell whether the form have been moved, not moved or an error did occur.
namespace System.Windows.Forms
{
public static class FormExtensions
{
public static bool? SetScreenToFirstNonPrimary(this Form self)
{
try
{
// Retrieves the collection of available screens (monitors)
var aScreens = Screen.AllScreens;
// If count is not greater than 1 then exit
if (aScreens.Length <= 1)
return false; // screen kept original
// Saves current screen reference
var screenOld = Screen.FromControl(self);
foreach (var screen in aScreens)
{
// Skips primary and current screen
if (screen.Primary || screen.Equals(screenOld))
continue;
var boundsScreen = screen.Bounds;
var oldState = self.WindowState;
// If form is currently maximized ...
if (oldState == FormWindowState.Maximized)
{
self.WindowState = FormWindowState.Normal;
self.StartPosition = FormStartPosition.Manual;
self.Location = boundsScreen.Location;
self.WindowState = FormWindowState.Maximized;
}
else
{
self.StartPosition = FormStartPosition.Manual;
// Center into new screen
var sizeGaps = boundsScreen.Size - self.Size;
var x = boundsScreen.Left + (sizeGaps.Width / 2);
var y = boundsScreen.Top + (sizeGaps.Height / 2);
self.Location = new Point(x, y);
}
return true; // screen has been changed
}
return false; // screen kept original
}
catch (Exception ex)
{
// process exception ex
// ...
return null; // error occurred
}
}
}
}
您可以在 InitializeComponent(); 之后作为
从窗体的构造函数调用它
You can call it from the form''s constructor just after InitializeComponent(); as
this.SetScreenToFirstNonPrimary();
这篇关于从C#代码以编程方式控制扩展监视器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文