如何在C#中将窗口置于屏幕中心?

Sea*_*ean 123 c# screen center winforms

我需要一种方法来居中当前窗口.因此,例如,如果用户按下按钮,我希望窗口在屏幕上居中.我知道你可以使用startposition属性,但除了应用程序第一次启动时,我无法找到使用它的方法.那么我如何将表格置于屏幕中心?

dze*_*ras 193

使用Form.CenterToScreen()方法.

  • 哇,你在跟我开玩笑吗?我们所有其他人都在试图解决这个问题.这很完美.谢谢! (9认同)
  • @Sean:永远了解您的图书馆.你携带所有这些行李是有原因的.;-) (9认同)
  • 在具有两个监视器的系统上,窗体将以当前具有光标的窗体为中心.因此,表单可能会突然跳转到其他监视器.见[此处] [http://stackoverflow.com/questions/6837463/how-come-centertoscreen-method-centers-the-form-on-the-screen-where-the-cursor-i/6837499#6837499 ). (7认同)
  • 您引用的文档确实说"请勿直接从您的代码中调用它".虽然它没有说明原因. (6认同)
  • 文档确实说使用表单的StartPosition属性来居中.它对我有用. (4认同)

Nay*_*iya 143

  1. Using the Property window

    Select form → go to property window → select "start position" → select whatever the place you want.

  2. Programmatically

    Form form1 = new Form(); form1.StartPosition = FormStartPosition.CenterScreen; form1.ShowDialog();

    Note: Do not directly call Form.CenterToScreen() from your code. Read here.


har*_*eyt 34

一行:

this.Location = new Point((Screen.PrimaryScreen.WorkingArea.Width - this.Width) / 2,
                          (Screen.PrimaryScreen.WorkingArea.Height - this.Height) / 2);
Run Code Online (Sandbox Code Playgroud)

  • 为什么选择Screen.PrimaryScreen?如果表格在"SecondaryScreen"上怎么办?你应该使用`Screen screen = Screen.FromControl(this);`这里. (9认同)

小智 27

在Windows窗体中:

this.StartPosition = FormStartPosition.CenterScreen;
Run Code Online (Sandbox Code Playgroud)

在WPF中:

this.WindowStartupLocation = WindowStartupLocation.CenterScreen;
Run Code Online (Sandbox Code Playgroud)

这就是你要做的一切......


Sar*_*r.A 16

如果您想在运行时使用以下代码使窗口居中,请将其复制到您的应用程序中:

protected void ReallyCenterToScreen()
{
  Screen screen = Screen.FromControl(this);

  Rectangle workingArea = screen.WorkingArea;
  this.Location = new Point() {
    X = Math.Max(workingArea.X, workingArea.X + (workingArea.Width - this.Width) / 2),
    Y = Math.Max(workingArea.Y, workingArea.Y + (workingArea.Height - this.Height) / 2)};
}
Run Code Online (Sandbox Code Playgroud)

最后调用上面的方法让它工作:

ReallyCenterToScreen();
Run Code Online (Sandbox Code Playgroud)


Fai*_*qui 8

 在运行时居中表单1.

设置Form的以下属性:
   - > StartPosition:CenterScreen
   - > WindowState:Normal

这将在运行时将表单居中,但如果表单大小超出预期,请执行第二步.

2.在InitializeComponent()之后添加自定义大小;

public Form1()
{
    InitializeComponent();
    this.Size = new Size(800, 600);
}
Run Code Online (Sandbox Code Playgroud)


UJS*_*UJS 7

用这个:

this.CenterToScreen();  // This will take care of the current form
Run Code Online (Sandbox Code Playgroud)


Rob*_*Rob 6

using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace centrewindow
{
    public partial class Form1 : Form
    {
        public struct RECT
        {
            public int Left;        // x position of upper-left corner
            public int Top;         // y position of upper-left corner
            public int Right;       // x position of lower-right corner
            public int Bottom;      // y position of lower-right corner
        }

        [DllImport("user32.dll")]
        public static extern IntPtr SetWindowPos(IntPtr hWnd, int hWndInsertAfter, int x, int Y, int cx, int cy, int wFlags);

        [DllImport("user32.dll")]
        public static extern bool GetWindowRect(HandleRef hwnd, out RECT lpRect);

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            CentreWindow(Handle, GetMonitorDimensions());
        }

        private void CentreWindow(IntPtr handle, Size monitorDimensions)
        {
            RECT rect;
            GetWindowRect(new HandleRef(this, handle), out rect);

            var x1Pos = monitorDimensions.Width/2 - (rect.Right - rect.Left)/2;
            var x2Pos = rect.Right - rect.Left;
            var y1Pos = monitorDimensions.Height/2 - (rect.Bottom - rect.Top)/2;
            var y2Pos = rect.Bottom - rect.Top;

            SetWindowPos(handle, 0, x1Pos, y1Pos, x2Pos, y2Pos, 0);
        }

        private Size GetMonitorDimensions()
        {
            return SystemInformation.PrimaryMonitorSize;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

集中任何窗口,你可以得到的句柄


De *_*lis 6

可能与问题不完全相关。但也许可以帮助别人。

中心屏幕以上对我来说都不起作用。原因是我正在向表单动态添加控件。从技术上讲,当它居中时,它是正确的,基于添加控件之前的表单。

这是我的解决方案。(应该适用于这两种情况)

int x = Screen.PrimaryScreen.Bounds.Width - this.PreferredSize.Width;
int y = Screen.PrimaryScreen.Bounds.Height - this.PreferredSize.Height;

this.Location = new Point(x / 2, y / 2);
Run Code Online (Sandbox Code Playgroud)

所以你会注意到我使用“PreferredSize”而不是仅仅使用高度/宽度。添加控件后,首选大小将保留表单的值。高度/宽度不会的地方。

希望这对某人有帮助。

干杯