在其父窗体的中心显示一个DialogBox是一团糟。这是一个显示对话框的方法。
我将其父级定位到中心,但无法使DialogBox居中
private void OpenForm(Object point, Object height, Object width)
{
FormLoading frm = new FormLoading();
Point temp = (Point)point;
Point location = new Point(temp.X + (int)((int)width) / 2,
temp.Y + (int)((int)height) / 2);
frm.Location = location;
frm.ShowDialog();
}
private void btnView_Click(object sender, EventArgs e)
{
try
{
ThreadStart starter= delegate { OpenForm(currentScreenLocation,
this.Height, this.Width); };
Thread t = new Thread(starter);
t.Start();
////// Some functionality here...
t.Abort();
}
catch (Exception)
{
}
}
答案 0 :(得分:91)
您可能需要查看Form.StartPosition
属性。
http://msdn.microsoft.com/en-us/library/system.windows.forms.form.startposition.aspx
类似于:
private void OpenForm(Form parent)
{
FormLoading frm = new FormLoading();
frm.Parent = parent;
frm.StartPosition = FormStartPosition.CenterParent;
frm.ShowDialog();
}
这当然需要设置表单的父级。
答案 1 :(得分:10)
form1.StartPosition = FormStartPosition.CenterScreen;
请参阅http://msdn.microsoft.com/en-us/library/system.windows.forms.form.startposition(v=vs.110).aspx
答案 2 :(得分:4)
此外,如果您想设置任意位置,可以使用此
FormLoading frm = new FormLoading();
Point location = new Point(300, 400);
frm.StartPosition = System.Windows.Forms.FormStartPosition.Manual;
frm.Location = location;
frm.ShowDialog();
答案 3 :(得分:2)
如果你正在制作一个自定义MessageBox,你可以简单地把它:
CenterToParent();
在自定义MessageBox formload()
方法中。
答案 4 :(得分:1)
NewForm.Show();
NewForm.Top = (this.Top + (this.Height / 2)) - NewForm.Height / 2;
NewForm.Left = (this.Left + (this.Width / 2)) - NewForm.Width / 2;