C# 在辅助监视器上显示Windows窗体?
我正在尝试在辅助监视器上设置Windows窗体,如下所示:C# 在辅助监视器上显示Windows窗体?,c#,winforms,multiple-monitors,setbounds,C#,Winforms,Multiple Monitors,Setbounds,我正在尝试在辅助监视器上设置Windows窗体,如下所示: private void button1_Click(object sender, EventArgs e) { MatrixView n = new MatrixView(); Screen[] screens = Screen.AllScreens; setFormLocation(n, screens[1]); n.Show(); } private void setFormLocation(Fo
private void button1_Click(object sender, EventArgs e)
{
MatrixView n = new MatrixView();
Screen[] screens = Screen.AllScreens;
setFormLocation(n, screens[1]);
n.Show();
}
private void setFormLocation(Form form, Screen screen)
{
// first method
Rectangle bounds = screen.Bounds;
form.SetBounds(bounds.X, bounds.Y, bounds.Width, bounds.Height);
// second method
//Point location = screen.Bounds.Location;
//Size size = screen.Bounds.Size;
//form.Left = location.X;
//form.Top = location.Y;
//form.Width = size.Width;
//form.Height = size.Height;
}
边界的属性似乎是正确的,但在我尝试过的两种方法中,这会使主监视器上的窗体最大化。有什么想法吗?在
SetFormLocation
方法中尝试将StartPosition参数设置为FormStartPosition.Manual
。您确定screens[1]
是您的第二选择吗?尝试一下屏幕[0]
。你的代码基本上是正确的
好的,我查过了,你必须在节目结束后再做() 这会产生一些不必要的闪烁。但你可能可以做到:
n.SetBounds(-100, -100, 10, 10); // or similar
n.Show();
setFormLocation(n, screens[1]);
这是表单参考。我将其用于XNA 4双屏应用程序(全屏XNA游戏窗口+WinForm) 在Form_Load()方法中,放置以下代码:
var primaryDisplay = Screen.AllScreens.ElementAtOrDefault(0);
var extendedDisplay = Screen.AllScreens.FirstOrDefault(s => s != primaryDisplay) ?? primaryDisplay;
this.Left = extendedDisplay.WorkingArea.Left + (extendedDisplay.Bounds.Size.Width / 2) - (this.Size.Width / 2);
this.Top = extendedDisplay.WorkingArea.Top + (extendedDisplay.Bounds.Size.Height / 2) - (this.Size.Height / 2);
将表单启动位置属性设置为手动
public void MoveWindowToProjector ()
{
Rectangle rectMonitor;
// Create New Process
Process objProcess = new Process();
//Get All the screens associated with this Monitor
Screen[] screens = Screen.AllScreens;
// Get Monitor Count
int iMonitorCount = Screen.AllScreens.Length;
// Get Parameters of Current Project
string[] parametros = Environment.GetCommandLineArgs();
// if (parametros.Length > 0)
// {
//objProcess.StartInfo.FileName = parametros[0];
// objProcess.Start();
// }
// Get Window Handle of this Form
IntPtr hWnd = this.Handle;
Thread.Sleep(1000);
if (IsProjectorMode)
{
if (iMonitorCount > 1) // If monitor Count 2 or more
{
//Get the Dimension of the monitor
rectMonitor = Screen.AllScreens[1].WorkingArea;
}
else
{
//Get the Dimension of the monitor
rectMonitor = Screen.AllScreens[0].WorkingArea;
}
}
else
{
rectMonitor = Screen.AllScreens[0].WorkingArea;
}
if (hWnd != IntPtr.Zero)
{
SetWindowPos(hWnd, 0,
rectMonitor.Left, rectMonitor.Top, rectMonitor.Width,
rectMonitor.Height, SWP_SHOWWINDOW);
}
}
要在辅助屏幕上显示表单,请执行以下操作:
Screen primaryFormScreen = Screen.FromControl(primaryForm);
//Use this if you are looking for secondary screen that is not primary
Screen secondaryFormScreen = Screen.AllScreens.FirstOrDefault(s => !s.Primary) ?? primaryFormScreen;
//Use this if you are looking for screen that is not being used by specific form
Screen secondaryFormScreen = Screen.AllScreens.FirstOrDefault(s => !s.Equals(primaryFormScreen)) ?? primaryFormScreen;
//Putting the form on the other screen
secondaryForm.Left = secondaryFormScreen.Bounds.Width;
secondaryForm.Top = secondaryFormScreen.Bounds.Height;
//Recommended to use, You can change it back later to the settings you wish
secondaryForm.StartPosition = FormStartPosition.Manual;
secondaryForm.Location = secondaryFormScreen.Bounds.Location;
Point p = new Point(secondaryFormScreen.Bounds.Location.X, secondaryFormScreen.Bounds.Location.Y);
secondaryForm.Location = p;
secondaryForm.Show();
如果你期待一个特定的屏幕,你可以在“screen.AllScreens”上循环并使用上面的过程。@Gengi的回答很简洁,效果很好。如果车窗最大化,则不会移动车窗。这段代码解决了这个问题(尽管我怀疑windows“正常”尺寸必须小于新屏幕尺寸才能工作):
void show屏幕(整数屏幕编号)
{
Screen[]screens=Screen.AllScreens;
如果(屏幕编号>=0&&screenNumber
最大化窗口状态
对于任意X,Y位置
此方法从左到右在所选屏幕上显示表单:
void ShowFormsOnScreenLeftToRight(Screen screen, params Form[] forms)
{
if (forms == null || forms.Length == 0)
return;
var formsCnt = forms.Length;
var formSize = new Size(screen.WorkingArea.Size.Width / formsCnt, screen.WorkingArea.Size.Height);
for (var i = 0; i < formsCnt; i++)
{
var form = forms[i];
if (form == null)
continue;
form.WindowState = FormWindowState.Normal;
form.Location = new Point(screen.WorkingArea.Left + screen.WorkingArea.Size.Width / formsCnt * i, 0);
form.Size = formSize;
form.BringToFront();
}
}
可以肯定的是,MatrixView上的窗口状态没有最大化,是吗?@Austin没有,窗口状态是正常的。相对确定的是,使用屏幕[0]和屏幕[1]会得到相同的结果。是的,执行form.StartPosition=FormStartPosition.Manual;成功了。知道为什么吗?@Henk不,是Windows窗体。下面是指向它的链接:来自MSDN:“将WindowsStartUplocation设置为手动会导致根据窗口的左属性值和顶部属性值定位窗口。如果未指定左属性或顶部属性,则其值由窗口确定。”WindowsStartUplocation在何处?我找不到它如果你的表单最大化了,你必须首先设置
this.WindowState=FormWindowState.Normal
然后将表单移动到其他屏幕,但它似乎是根据窗口的监视器配置以外的其他条件排序的。例如,我的主监视器位于位置[1],而我的第二个监视器位于位置[0]。如果设置创建表单实例的位置,则将无法工作。like适用于表单加载什么是secondaryForm
以及它在您或OP的代码中声明的位置?secondaryForm
是一种表单
类型的对象,我将其位置设置在secondaryFormScreen
范围内。它不必是Form
,这取决于您使用的是什么,您可以设置类似的边界,就像我在另一个屏幕中对表单所做的那样分配。我知道我做得晚了,但是对于其他看到这个的人来说。主显示并不总是列表中的元素0,因此无法可靠地获取主显示。然而,这确实让我找到了使用Screen的主要属性的解决方案。var primaryMonitor=Screen.AllScreens.Where(s=>s.Primary.FirstOrDefault();var secondaryscreenses=Screen.AllScreens.Where(s=>!s.Primary.ToList();
Screen primaryFormScreen = Screen.FromControl(primaryForm);
//Use this if you are looking for secondary screen that is not primary
Screen secondaryFormScreen = Screen.AllScreens.FirstOrDefault(s => !s.Primary) ?? primaryFormScreen;
//Use this if you are looking for screen that is not being used by specific form
Screen secondaryFormScreen = Screen.AllScreens.FirstOrDefault(s => !s.Equals(primaryFormScreen)) ?? primaryFormScreen;
//Putting the form on the other screen
secondaryForm.Left = secondaryFormScreen.Bounds.Width;
secondaryForm.Top = secondaryFormScreen.Bounds.Height;
//Recommended to use, You can change it back later to the settings you wish
secondaryForm.StartPosition = FormStartPosition.Manual;
secondaryForm.Location = secondaryFormScreen.Bounds.Location;
Point p = new Point(secondaryFormScreen.Bounds.Location.X, secondaryFormScreen.Bounds.Location.Y);
secondaryForm.Location = p;
secondaryForm.Show();
void showOnScreen(int screenNumber)
{
Screen[] screens = Screen.AllScreens;
if (screenNumber >= 0 && screenNumber < screens.Length)
{
bool maximised = false;
if (WindowState == FormWindowState.Maximized)
{
WindowState = FormWindowState.Normal;
maximised = true;
}
Location = screens[screenNumber].WorkingArea.Location;
if (maximised)
{
WindowState = FormWindowState.Maximized;
}
}
}
Screen[] screens = Screen.AllScreens;
sc aoc = new sc();
aoc.Show();
aoc.Location = Screen.AllScreens[INDEX OF YOUR AVAILABLE SCREENS TARGET].WorkingArea.Location;
aoc.WindowState = FormWindowState.Maximized;
aoc.Location = new Point(TARGET X POSITION, TARGET Y POSITION);
void ShowFormsOnScreenLeftToRight(Screen screen, params Form[] forms)
{
if (forms == null || forms.Length == 0)
return;
var formsCnt = forms.Length;
var formSize = new Size(screen.WorkingArea.Size.Width / formsCnt, screen.WorkingArea.Size.Height);
for (var i = 0; i < formsCnt; i++)
{
var form = forms[i];
if (form == null)
continue;
form.WindowState = FormWindowState.Normal;
form.Location = new Point(screen.WorkingArea.Left + screen.WorkingArea.Size.Width / formsCnt * i, 0);
form.Size = formSize;
form.BringToFront();
}
}
ShowFormsOnScreenLeftToRight(n, Screen.AllScreens.First(s => !s.Primary));