显示在辅助监视器上一个Windows窗体?
-
20-09-2019 - |
题
我试图设置一个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(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;
}
界的性质似乎是正确的,但在这两种方法我尝试过,这最大化在主监视器上的形式。任何想法?
解决方案
尝试设置WindowStartUpLocation参数为 “手动” 您的SetFormLocation方法内。
其他提示
this.Location = Screen.AllScreens[1].WorkingArea.Location;
这是在参考表格
@ Gengi的回答是简洁的,并且效果很好。如果最大化了窗口,它不会移动窗口。这个片段解决了(虽然我怀疑窗户“正常”尺寸必须比新屏幕尺寸较小的这个工作):
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;
}
}
}
您确定screens[1]
是您的辅助?给screens[0]
一试。代码基本上是正确的。
好吧,我查了一下,你将有展()之后做到这一点:
n.Show();
setFormLocation(n, screens[1]);
其给出一些不需要的闪烁。但你也许可以这样做:
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);
要显示辅助屏幕上的形式:
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”,并使用上述过程。
设置形式启动位置属性为手动
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[] screens = Screen.AllScreens;
sc aoc = new sc();
aoc.Show();
aoc.Location = Screen.AllScreens[INDEX OF YOUR AVAILABLE SCREENS TARGET].WorkingArea.Location;
有关MAXIMIZED窗口状态
aoc.WindowState = FormWindowState.Maximized;
对于任何x,y位置
aoc.Location = new Point(TARGET X POSITION, TARGET Y POSITION);
不隶属于 StackOverflow