Answers:
使用属性窗口
选择表单→转到属性窗口→选择“开始位置”→选择您想要的任何地方。
以编程方式
Form form1 = new Form();
form1.StartPosition = FormStartPosition.CenterScreen;
form1.ShowDialog();
注意:不要直接从代码中调用Form.CenterToScreen()。在这里阅读。
一行:
this.Location = new Point((Screen.PrimaryScreen.WorkingArea.Width - this.Width) / 2,
(Screen.PrimaryScreen.WorkingArea.Height - this.Height) / 2);
Screen screen = Screen.FromControl(this);
在这里使用。
Screen.FromControl(this)
而是将其保留下来PrimaryScreen
。(我正在硬件限制下开发应用程序):-)
在Windows窗体中:
this.StartPosition = FormStartPosition.CenterScreen;
在WPF中:
this.WindowStartupLocation = WindowStartupLocation.CenterScreen;
那就是你要做的...
如果要在运行时使窗口居中,请使用以下代码,将其复制到应用程序中:
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)};
}
最后调用上面的方法使其工作:
ReallyCenterToScreen();
this.Location = Screen.AllScreens[0].WorkingArea.Location;
它也可以使用,在使用多个屏幕移动应用程序的情况下,其他答案将不起作用。
在运行时中将表单居中1.设置表单的
以下属性:
-> StartPosition:CenterScreen-
> WindowState:Normal
这将使表单在运行时居中,但是如果表单大小大于预期大小,请执行第二步。
2.在InitializeComponent()之后添加自定义大小;
public Form1()
{
InitializeComponent();
this.Size = new Size(800, 600);
}
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;
}
}
}
居中您可以获取的任何窗口
用这个:
this.CenterToScreen(); // This will take care of the current form
可能与问题不完全相关。但也许可以帮助某人。
中心屏幕非上述工作适合我。原因是我要向表单动态添加控件。从技术上讲,居中位置是正确的(基于添加控件之前的表格)。
所以这是我的解决方案。(应该同时适用于两种情况)
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);
因此,您会注意到我使用的是“ PreferredSize”,而不仅仅是使用“高度/宽度”。添加控件后,首选大小将保留表单的值。高度/宽度不会的地方。
希望这对某人有帮助。
干杯
如果是多显示器,并且如果您希望以正确的显示器/屏幕为中心,则可以尝试以下操作:
// Save values for future(for example, to center a form on next launch)
int screen_x = Screen.FromControl(Form).WorkingArea.X;
int screen_y = Screen.FromControl(Form).WorkingArea.Y;
// Move it and center using correct screen/monitor
Form.Left = screen_x;
Form.Top = screen_y;
Form.Left += (Screen.FromControl(Form).WorkingArea.Width - Form.Width) / 2;
Form.Top += (Screen.FromControl(Form).WorkingArea.Height - Form.Height) / 2;