C#Panel作为MDI容器

Ozz*_*zzy 4 c# forms mdi panel

在C#中,我想创建一个具有MDI容器属性的面板,即.isMdiContainer = true.

我试过这样的事

form.MDIParent = this.panel1;
Run Code Online (Sandbox Code Playgroud)

但那不行.有什么建议?

小智 11

可以创建一个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;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

用法:

/// mdiChildForm is the form that should be showed in the panel
/// mdiClientPanel is an instance of the MdiClientPanel
myMdiChildForm.MdiParent = mdiClientPanel1.MdiForm;
Run Code Online (Sandbox Code Playgroud)