我需要一种方法将当前窗口居中显示在屏幕上。
例如,如果用户按下一个按钮,我希望窗口能够自动居中在屏幕上。
我知道可以使用“startposition”属性来实现居中显示,但我无法找到方法使其在应用程序启动后也能生效。
那么如何将窗体居中显示在屏幕上呢?
this.Location = Screen.AllScreens[0].WorkingArea.Location;
- TK-421使用 属性 窗口
选择表单 → 转到属性窗口 → 选择 "起始位置" → 选择任何您想要的地方。
编程方式
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);
。 - ArtemixScreen.FromControl(this)
而保持使用PrimaryScreen
。(我正在开发一款受硬件限制的应用程序) :-) - Yeo在 Windows Forms 中:
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;
之前,它也能正常工作。其他答案在使用多个屏幕时移动应用程序时不起作用。 - TK-421居中运行时的表单
1. 将下列属性设置为Form:
-> StartPosition : CenterScreen
-> WindowState: Normal
这将使表单在运行时居中,但如果表单大小超出预期,请执行第二步。
2. 在InitializeComponent();之后添加自定义大小。
public Form1()
{
InitializeComponent();
this.Size = new Size(800, 600);
}
this.CenterToScreen(); // This will take care of the current form
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;
}
}
}
这可能与问题不完全相关,但也许可以帮助某些人。
对于我来说,居中屏幕的方法都不起作用。原因是我在动态向表单添加控件。从技术上讲,当它居中时,它是正确的,基于添加控件之前的表单。
所以这是我的解决方案。(应该适用于两种情况)
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);
实际样例
private void barButtonItem1_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
AccountAddForm f = new AccountAddForm();
f.StartPosition = FormStartPosition.CenterScreen;
f.Show();
}
StartPosition = FormStartPosition.CenterScreen;
或者this.CenterToScreen();
- AliNajafZadeh