C# 如何使 mdichild 加载到 mdiparent 窗口的中心

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1228120/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 13:56:45  来源:igfitidea点击:

How to make mdichild load at the center of mdiparent window

c#mdi

提问by Srikanth V M

HI all,

大家好,

I have this code in which the window property of making child window load at the center of mdiparent.

我有这段代码,其中使子窗口加载的窗口属性位于 mdiparent 的中心。

        Form2 f = new Form2();

        f.MdiParent = this;

        //center screen is working.

        //f.StartPosition = FormStartPosition.CenterScreen;

        f.StartPosition = FormStartPosition.CenterParent;

but instead of making the child window pop at the center it loads at the left side. Can anyone help me on this. Please refer the screenshot below.

但不是让子窗口在中心弹出,而是在左侧加载。谁可以帮我这个事。请参考下面的截图。

I have even tried doing the same in vb. Even there i get the same error. I think the property of FormStartPosition.CenterParent is dummy.

我什至尝试在 vb 中做同样的事情。即使在那里我也得到同样的错误。我认为 FormStartPosition.CenterParent 的属性是虚拟的。

alt text http://img13.imageshack.us/img13/7003/errorprb.jpg

替代文字 http://img13.imageshack.us/img13/7003/errorprb.jpg

回答by Patrick McDonald

I tried showing the child with the MDI container form as owner, but caused an exception for me. You could manually set the location before showing the child as follows:

我尝试将 MDI 容器表单显示为所有者的孩子,但对我造成了异常。您可以在显示孩子之前手动设置位置,如下所示:

Form2 f = new Form2();
f.MdiParent = this;
f.StartPosition = FormStartPosition.Manual;
f.Location = new Point((this.ClientSize.Width - f.Width) / 2,
                       (this.ClientSize.Height - f.Height) / 2);
f.Show();

EDIT:

编辑:

f.StartPosition = FormStartPosition.CenterScreen;

is the correct way to center an mdichild on its parent form.

是将 mdichild 放在其父窗体上的正确方法。

回答by Fredrik M?rk

I experimented a bit with this, and first came to the same solution as Patrick. But I was bugged by the following statement in the documentation of the StartPositionproperty:

我对此进行了一些试验,首先得出了与 Patrick 相同的解决方案。但是我被属性文档中的StartPosition以下语句所困扰:

You can also position the form to display in the center of the screen or in the center of its parent form for forms such as multiple-document interface (MDI) child forms.

对于多文档界面 (MDI) 子窗体等窗体,您还可以将窗体放置在屏幕中央或父窗体的中央显示。

So, I decided that there must be a way. And there is, though I don't find it all intuitive: set the StartPositionto CenterScreen(notCenterParent):

所以,我决定一定有办法。还有,虽然我觉得这不是很直观:设置StartPositionCenterScreen不是CenterParent):

MdiChildUI form = new MdiChildUI();
form.MdiParent = this;
form.StartPosition = FormStartPosition.CenterScreen;
form.Show();

Of course, you can also set it in the designer instead of in code.

当然,你也可以在设计器中设置,而不是在代码中设置。

回答by danish

Setting start position as center screen works perfect for me.

将开始位置设置为中心屏幕对我来说非常适合。

回答by Aram Afsar

A form has an MdiParentproperty and a Parentproperty.

表单具有MdiParent属性和Parent属性。

Please notice that an MdiParent IS NOTthe parent of MdiChild form.

请注意 MdiParent不是MdiChild 表单的父级。

It is, its MdiParent;

它是,它的 MdiParent;

So CenterToParentMethod is meaningless for these types of forms.

所以CenterToParent方法对于这些类型的表单毫无意义。

You can see the functionality of CenterToParentMethod when you program for multiple monitors application, or when you host a form inside another form by setting its Toplevelproperty to false, and setting its Parentproperty to another form.

当您为多个监视器应用程序编程时,或者当您通过将其Toplevel属性设置为 false 并将其Parent属性设置为另一个表单在另一个表单中托管一个表单时,您可以看到CenterToParent方法的功能。

Good Luck,

祝你好运,

Aram Afsar

阿拉姆·阿夫萨