.NET/WinForms: 在特定屏幕上最大化窗口

4
我有一个双显示器设置,我希望我的C#应用程序可以在特定的屏幕上最大化窗口。
我该怎么做?
谢谢!

这个解决方案对我有用:https://dev59.com/H3PYa4cB1Zd3GeqPhU8Z - arnej
4个回答

4
这是我在一个项目中使用的类似范围的屏幕管理代码:
        // screenId in my case is 1(first) or 2(second)
        int screenId = RegistryManager.ScreenId;
        // DualScreen management            
        if (screenId > 0)
        {
            // Have 2 screens
            if (System.Windows.Forms.Screen.AllScreens.Length == 2)
            {
                if (screenId == 1) // first
                    this.Location = new System.Drawing.Point(System.Windows.Forms.Screen.AllScreens[0].Bounds.Left, 0);
                else // second
                    this.Location = new System.Drawing.Point(System.Windows.Forms.Screen.AllScreens[1].Bounds.Left, 0);
            }
        }

2
您可以使用Screen类来查找第二个显示器。 链接 代码可在此处找到 这里
function void showOnMonitor2()
{
Screen[] sc;
sc = Screen.AllScreens;
//get all the screen width and heights
Form2 f = new Form2();
f.FormBorderStyle = FormBorderStyle.None;
f.Left = sc[1].Bounds.Width;
f.Top = sc[1].Bounds.Height;
f.StartPosition = FormStartPosition.Manual;
f.Location = sc[1].Bounds.Location;
Point p = new Point(sc[1].Bounds.Location.X, sc[1].Bounds.Location.Y);
f.Location = p;
f.WindowState = FormWindowState.Maximized;
f.Show();
}

你需要一些错误检查来确保有超过1个屏幕。 - Matt
是的,我知道,这只是一个复制粘贴来展示它的工作原理。在使用他人的代码时,应始终应用和检查任何合理性检查。 - RvdK

1

在最大化之前,您可以将窗口移动到最右侧或最左侧,这样应该会导致窗口在最大化时最大化到那个屏幕。


如果您在移动窗口后将其设置为可见,则此方法有效。谢谢! - clamp

0
  Public Shared Sub MoveForm(Item As Form, ScreenNumber As Integer, Optional X As Integer = 0, Optional Y As Integer = 0)
    With Screen.AllScreens(ScreenNumber).Bounds
      X -= .Left 'translate overall coordinates to screen coordinates
      Y -= .Top
    End With
    Item.Location = New System.Drawing.Point(X, Y)
  End Sub

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接