在其父框的中心显示对话框

本文关键字:显示 对话框 | 更新日期: 2023-09-27 18:01:37

将对话框显示在其父窗体的中心是一种混乱。下面是一个显示对话框的方法。

我将它的父元素定位为居中,但无法将对话框居中

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)
    {
    }
}

在其父框的中心显示对话框

您可能需要检查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();
}
这当然需要设置表单的父元素
form1.StartPosition = FormStartPosition.CenterScreen;

见http://msdn.microsoft.com/en-us/library/system.windows.forms.form.startposition (v = vs.110) . aspx

如果您正在创建一个自定义的MessageBox,您可以简单地输入:

CenterToParent();

在您的自定义MessageBox formload()方法

另外,如果您想设置任意位置,您可以使用

FormLoading frm = new FormLoading();
Point location = new Point(300, 400);
frm.StartPosition = System.Windows.Forms.FormStartPosition.Manual;
frm.Location = location;
frm.ShowDialog();
NewForm.Show();
NewForm.Top = (this.Top + (this.Height / 2)) - NewForm.Height / 2;
NewForm.Left = (this.Left + (this.Width / 2)) - NewForm.Width / 2;