在 C# 中,我想创建一个具有 MDI 容器属性的面板,即。 isMdiContainer = true。
我试过这样的东西
form.MDIParent = this.panel1;
但这行不通。有什么建议吗?
最佳答案
可以创建一个 MDI 面板并在该面板中显示表单,类似下面的代码就可以完成这项工作
Mdi-Panel 定义:
public class MdiClientPanel : Panel
{
private Form mdiForm;
private MdiClient ctlClient = new MdiClient();
public MdiClientPanel()
{
base.Controls.Add(this.ctlClient);
}
public Form MdiForm
{
get
{
if (this.mdiForm == null)
{
this.mdiForm = new Form();
/// set the hidden ctlClient field which is used to determine if the form is an MDI form
System.Reflection.FieldInfo field = typeof(Form).GetField("ctlClient", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
field.SetValue(this.mdiForm, this.ctlClient);
}
return this.mdiForm;
}
}
}
用法:
/// mdiChildForm is the form that should be showed in the panel
/// mdiClientPanel is an instance of the MdiClientPanel
myMdiChildForm.MdiParent = mdiClientPanel1.MdiForm;
关于C# 面板作为 MDI 容器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/941306/