繁体   English   中英

在C#中在父窗体的中心显示子窗体

[英]Show a child form in the centre of Parent form in C#

我创建一个新表单并从父表单调用,如下所示:

loginForm = new SubLogin();   
loginForm.Show();

我需要在父窗体的中心显示子窗体。 因此,在子窗体加载中,我执行以下操作:`

Point p = new Point(this.ParentForm.Width / 2 - this.Width / 2, this.ParentForm.Height / 2 - this.Height / 2);
this.Location = p;

但这会引发错误,因为父表单为空。 我也尝试设置 Parent 属性,但没有帮助。 对此有任何意见吗?

尝试:

loginForm.StartPosition = FormStartPosition.CenterParent;
loginForm.ShowDialog(this);

当然,子 for 现在将是父窗口的阻塞窗体(对话框),如果不需要,则只需将ShowDialog替换为Show ..

loginForm.Show(this);

不过,您仍然需要指定 StartPosition。

除非我使用form.ShowDialog();否则 parent 的设置对我form.ShowDialog(); .

当使用form.Show(); form.Show(this); 在我使用this.CenterToParent();之前没有任何效果this.CenterToParent(); . 我只是把它放在表单的Load方法中。 一切都很好。

设置到父级中心的起始位置并且在使用阻塞显示对话框时有效。

“父母”和“所有者”之间似乎存在混淆。 如果您以 MDI 形式打开一个表单,即嵌入在另一个表单中,那么这个周围的表单就是父表单。 具有值 FormStartPosition.CenterParent 的表单属性 StartPosition 指的是这个。 您可以传递给 Show 方法的参数是 Owner,而不是 Parent! 这就是为什么 frm.StartPosition = FormStartPosition.CenterParent 不像您预期​​的那样工作。

如果其 StartPosition 设置为 Manual,则放置在表单中的以下代码将其相对于其所有者居中并带有一些偏移。 小偏移量以平铺方式打开表单。 如果所有者和拥有的表单具有相同的大小或者如果您打开多个拥有的表单,这是一个优势。

protected override void OnShown(EventArgs e)
{
    base.OnShown(e);
    if (Owner != null && StartPosition == FormStartPosition.Manual) {
        int offset = Owner.OwnedForms.Length * 38;  // approx. 10mm
        Point p = new Point(Owner.Left + Owner.Width / 2 - Width / 2 + offset, Owner.Top + Owner.Height / 2 - Height / 2 + offset);
        this.Location = p;
    }
}

假设您的代码在您的父表单中运行,那么您可能正在寻找这样的东西:

loginForm = new SubLogin();
loginForm.StartPosition = FormStartPosition.CenterParent
loginForm.Show(this);

作为记录,还有一个Form.CenterToParent()函数,如果您出于任何原因也需要在创建后将其居中。

MDIForm表单中启动表单时,您需要使用.CenterScreen而不是.CenterParent

FrmLogin f = new FrmLogin();
f.MdiParent = this;
f.StartPosition = FormStartPosition.CenterScreen;
f.Show();
childform = new Child();
childform.Show(this);

在事件子窗体加载

this.CenterToParent();

你需要这个:

用 this.parent 替换 Me,但您需要在显示该表单之前设置 parent。

  Private Sub ÜberToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ÜberToolStripMenuItem.Click

        'About.StartPosition = FormStartPosition.Manual ' !!!!!
        About.Location = New Point(Me.Location.X + Me.Width / 2 - About.Width / 2, Me.Location.Y + Me.Height / 2 - About.Height / 2)
        About.Show()
    End Sub

当您想使用非阻塞窗口(show() 而不是 showDialog())时,这不起作用:

//not work with .Show(this) but only with .ShowDialog(this)
loginForm.StartPosition = FormStartPosition.CenterParent;
loginForm.Show(this);

在这种情况下,您可以使用此代码在显示表单之前将子表单居中:

//this = the parent
frmDownloadPercent frm = new frmDownloadPercent();
frm.Show(this); //this = the parent form
//here the tips
frm.Top = this.Top + ((this.Height / 2) - (frm.Height / 2));
frm.Left = this.Left + ((this.Width / 2) - (frm.Width / 2));

它适用于所有情况,将 Form1 换成您的主表单。

Popup popup = new Popup();
popup.StartPosition = System.Windows.Forms.FormStartPosition.Manual;
popup.Location = new System.Drawing.Point((Form1.ActiveForm.Location.X + Form1.ActiveForm.Width / 2) - (popup.Width / 2),(Form1.ActiveForm.Location.Y + Form1.ActiveForm.Height / 2) - (popup.Height / 2));
popup.Show(Form1.ActiveForm);

如果要计算自己的位置,请先将StartPosition设置为FormStartPosition.Manual

Form Child = new Form();
Child.StartPosition = FormStartPosition.Manual;
Child.Location = new Point(Location.X + (Width - Child.Width) / 2, Location.Y + (Height - Child.Height) / 2);
Child.Show(this);

这是主/父表单,就像 Location.X 一样。

StartPosition默认值是FormStartPosition.CenterParent ,因此它会在显示后更改孩子的位置。

在 SubLogin 表单上,我将公开一个 SetLocation 方法,以便您可以从父表单设置它:

public class SubLogin : Form
{
   public void SetLocation(Point p)
   {
      this.Location = p;
   }
} 

然后,从您的主要形式:

loginForm = new SubLogin();   
Point p = //do math to get point
loginForm.SetLocation(p);
loginForm.Show();

制作一个 Windows 窗体,然后为其添加选项:CenterParent 然后使用此代码:

yourChildFormName x = new yourChildFormName();
x.ShowDialog();

您可以在子窗体的构造函数中设置 StartPosition,以便窗体的所有新实例都以它的父窗体为中心:

public MyForm()
{
    InitializeComponent();

    this.StartPosition = FormStartPosition.CenterParent;
}

当然,您也可以在设计器属性中为子窗体设置 StartPosition 属性。 当您想将子窗体显示为模态对话框时,只需在 ShowDialog 方法的参数中设置窗口所有者即可:

private void buttonShowMyForm_Click(object sender, EventArgs e)
{
    MyForm form = new MyForm();
    form.ShowDialog(this);
}

如果从主窗口(父窗体)的新线程打开任何窗体(子窗体),则无法将子窗口保持在主窗口的中心,因此我们需要手动固定子窗口的位置X 和 Y 坐标的平均值。

在子窗口的属性中,将“StartPosition”更改为“Manual”

主窗口中的代码

private void SomeFunction()
{
    Thread m_Thread = new Thread(LoadingUIForm);
    m_Thread.Start();
    OtherParallelFunction();
    m_Thread.Abort();
}

private void LoadingUIForm()
{
    m_LoadingWindow = new LoadingForm(this);
    m_LoadingWindow.ShowDialog();
}

子窗口中的代码,用于通过父当前位置和大小定义自己的位置

public LoadingForm(Control m_Parent)
{
   InitializeComponent();
   this.Location = new Point( m_Parent.Location.X+(m_Parent.Size.Width/2)-(this.Size.Width/2),
                              m_Parent.Location.Y+(m_Parent.Size.Height/2)-(this.Size.Height/2)
                            );
}

这里计算父窗口的中心坐标,并且通过 (this.height/2) 和 (this.width/2) 计算自己的中心,子窗口精确地保持在父窗口的中心,这个函数可以是进一步采取父搬迁事件也。

作为子表单,我认为它不会在父表单中间开始,直到您将其显示为对话框。 .......... Form2.ShowDialog();

我正要制作关于表格。 这是我正在寻找的完美。 直到你关闭 About_form 你不能触摸/点击父母表单的任何东西,一旦你点击 About_Form(在我的情况下)。因为它的显示为对话框

如果您必须将 childForm 居中,从 childForm 开始,那么代码将是这样的。 此代码在 childForm.cs 中

this.Show(parent as Form);    // I received the parent object as Object type
this.CenterToParent();
    protected override void OnLoad(EventArgs e) {
        base.OnLoad(e);

        CenterToParent();
    }

当您尝试访问它时,父级可能尚未设置。

试试这个:

loginForm = new SubLogin();
loginForm.Show(this);
loginForm.CenterToParent()

为什么不使用这个?

LoginForm.WindowStartupLocation = Windows.WindowStartupLocation.CenterOwner 

(vb.net)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM