我需要一种方法来居中当前窗口。因此,举例来说,如果用户按下一个按钮,我希望窗口屏幕中心本身。我知道你可以使用指定startPosition财产,但我不能想出一个办法使用比其他当应用程序首先启动。那么,如何在中心屏幕上的形式?
可以使用Screen.PrimaryScreen.Bounds
检索主监视器的尺寸(或者检查对象Screen
检索所有的监视器)。使用那些MyForms.Bounds
弄清楚在何处放置形式。
可能不是问题完全相关。但也许可以帮助别人。
对我来说,上述工作的中心屏幕非。原因是我被动态地添加到控制的形式。从技术上讲,当它居中它是正确的,基于添加控件之前在窗体上。
因此,这里是我的解决方案。 (应与这两种情况下工作)
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;
Form form1 = new Form();
form1.StartPosition = FormStartPosition.CenterScreen;
form1.ShowDialog();
注意:不要直接调用Form.CenterToScreen()从你的代码。阅读here。单行:
this.Location = new Point((Screen.PrimaryScreen.WorkingArea.Width - this.Width) / 2,
(Screen.PrimaryScreen.WorkingArea.Height - this.Height) / 2);
在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();
围绕在运行时的形式 1.设置如下形式的财产: - >中StartPosition:中心屏幕 - >的WindowState:正常
这将中心的形式在运行,但如果形式尺寸更大然后期望,做第二个步骤。 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
使用此表的位置属性。它设置为所需的左上点
期望的X =(desktop_width - form_witdh)/ 2
期望的Y =(desktop_height - from_height)/ 2