C# 面板作为 MDI 容器

发布于 2024-07-23 05:08:25 字数 164 浏览 8 评论 0原文

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

我尝试过类似的方法

form.MDIParent = this.panel1;

但这不起作用。 有什么建议么?

In C# i want to create a panel that has the properties of a MDI container ie. isMdiContainer = true.

I tried something like this

form.MDIParent = this.panel1;

But that dont work. Any suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

宣告ˉ结束 2024-07-30 05:08:25

可以创建一个 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;

It is possible to create an MDI-panel and show forms in that panel, something like the code below will do the job

Mdi-Panel definiton:

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;
        }
    }
}

Usage:

/// mdiChildForm is the form that should be showed in the panel
/// mdiClientPanel is an instance of the MdiClientPanel
myMdiChildForm.MdiParent = mdiClientPanel1.MdiForm;
全部不再 2024-07-30 05:08:25

您可以创建自定义表单,删除所有边框和工具栏,使其看起来尽可能接近面板。 然后将该新的自定义表单设为 MdiContainer。

基本上,您只能在窗体上设置 IsMDIContainer 属性。 这意味着只有表单可以是 MdiContainer。

You could create custom form, remove all borders, and toolbars to make it look as closely to a panel as possible. Then make that new custom form a MdiContainer.

Basically, you can only set the IsMDIContainer property on a Form. This means that only a form can be a MdiContainer.

も让我眼熟你 2024-07-30 05:08:25

我使用了上面马蒂亚斯的答案,并且能够消除评论中提出的大部分问题。 我还为子表单创建了一个辅助类,以防有人想要使用它和/或使其变得更好。

    public class MdiClientPanel : Panel
{
    private MdiClient _ctlClient = new MdiClient();

    // Callback event when a child is activated
    public delegate void ActivateHandler(object sender, MdiClientForm child);
    public event ActivateHandler OnChildActivated;

    /// <summary>
    /// The current active child form
    /// </summary>
    public Form ActiveMDIWnd
    {
        get;
        set;
    }

    /// <summary>
    /// List of available forms
    /// </summary>
    public List<MdiClientForm> ChildForms = new List<MdiClientForm>();

    /// <summary>
    /// Std constructor
    /// </summary>
    public MdiClientPanel()
    {
        base.Controls.Add(_ctlClient);
    }

    private Form _mdiForm = null;
    public Form MdiForm
    {
        get
        {
            if (_mdiForm == null)
            {
                _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(_mdiForm, _ctlClient);
            }
            return _mdiForm;
        }
    }

    private void InitializeComponent()
    {
        SuspendLayout();
        ResumeLayout(false);
    }

    /// <summary>
    /// Add this Form to our list of children and set the MDI relationship up
    /// </summary>
    /// <param name="child">The new kid</param>
    /// <returns>The new kid</returns>
    public MdiClientForm AddChild(MdiClientForm child)
    {
        child.MyMdiContainer = this;
        child.MdiParent = MdiForm;
        ChildForms.Add(child);

        return child;
    }

    /// <summary>
    /// The user sent a "restore" command, so issue it to all children
    /// </summary>
    public void RestoreChildForms()
    {
        foreach (MdiClientForm child in ChildForms)
        {
            child.WindowState = FormWindowState.Normal;
            child.MaximizeBox = true;
            child.MinimizeBox = true;
        }
    }

    /// <summary>
    /// Send the Activated message to the owner of this panel (if they are listening)
    /// </summary>
    /// <param name="child">The child that was just activated</param>
    public void ChildActivated(MdiClientForm child)
    {
        ActiveMDIWnd = child;

        if (OnChildActivated != null)
            OnChildActivated(this, child);
    }

    /// <summary>
    /// The child closed so remove them from our available form list
    /// </summary>
    /// <param name="child">The child that closed</param>
    public void ChildClosed(MdiClientForm child)
    {
        ChildForms.Remove(child);
    }
}

/// <summary>
/// A wrapper class for any form wanting to be an MDI child of an MDI Panel
/// </summary>
public class MdiClientForm : Form
{
    /// <summary>
    /// My parent MDI container
    /// </summary>
    public MdiClientPanel MyMdiContainer { get; set; }

    /// <summary>
    /// Standard Constructor
    /// </summary>
    public MdiClientForm()
    {
        Activated += OnFormActivated;
        FormClosed += OnFormClosed;
    }

    /// <summary>
    /// Reports back to the container when we close
    /// </summary>
    void OnFormClosed(object sender, FormClosedEventArgs e)
    {
        MyMdiContainer.ChildClosed(this);
    }

    /// <summary>
    /// Reports back to the parent container when we are activated
    /// </summary>
    private void OnFormActivated(object sender, EventArgs e)
    {
        MyMdiContainer.ChildActivated(this);
    }
}

I used Mathias's answer above and was able to get rid of most of the issues raised in the comments. I created a helper class for the child forms as well, in case anyone wanted to use it and/or make it better.

    public class MdiClientPanel : Panel
{
    private MdiClient _ctlClient = new MdiClient();

    // Callback event when a child is activated
    public delegate void ActivateHandler(object sender, MdiClientForm child);
    public event ActivateHandler OnChildActivated;

    /// <summary>
    /// The current active child form
    /// </summary>
    public Form ActiveMDIWnd
    {
        get;
        set;
    }

    /// <summary>
    /// List of available forms
    /// </summary>
    public List<MdiClientForm> ChildForms = new List<MdiClientForm>();

    /// <summary>
    /// Std constructor
    /// </summary>
    public MdiClientPanel()
    {
        base.Controls.Add(_ctlClient);
    }

    private Form _mdiForm = null;
    public Form MdiForm
    {
        get
        {
            if (_mdiForm == null)
            {
                _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(_mdiForm, _ctlClient);
            }
            return _mdiForm;
        }
    }

    private void InitializeComponent()
    {
        SuspendLayout();
        ResumeLayout(false);
    }

    /// <summary>
    /// Add this Form to our list of children and set the MDI relationship up
    /// </summary>
    /// <param name="child">The new kid</param>
    /// <returns>The new kid</returns>
    public MdiClientForm AddChild(MdiClientForm child)
    {
        child.MyMdiContainer = this;
        child.MdiParent = MdiForm;
        ChildForms.Add(child);

        return child;
    }

    /// <summary>
    /// The user sent a "restore" command, so issue it to all children
    /// </summary>
    public void RestoreChildForms()
    {
        foreach (MdiClientForm child in ChildForms)
        {
            child.WindowState = FormWindowState.Normal;
            child.MaximizeBox = true;
            child.MinimizeBox = true;
        }
    }

    /// <summary>
    /// Send the Activated message to the owner of this panel (if they are listening)
    /// </summary>
    /// <param name="child">The child that was just activated</param>
    public void ChildActivated(MdiClientForm child)
    {
        ActiveMDIWnd = child;

        if (OnChildActivated != null)
            OnChildActivated(this, child);
    }

    /// <summary>
    /// The child closed so remove them from our available form list
    /// </summary>
    /// <param name="child">The child that closed</param>
    public void ChildClosed(MdiClientForm child)
    {
        ChildForms.Remove(child);
    }
}

/// <summary>
/// A wrapper class for any form wanting to be an MDI child of an MDI Panel
/// </summary>
public class MdiClientForm : Form
{
    /// <summary>
    /// My parent MDI container
    /// </summary>
    public MdiClientPanel MyMdiContainer { get; set; }

    /// <summary>
    /// Standard Constructor
    /// </summary>
    public MdiClientForm()
    {
        Activated += OnFormActivated;
        FormClosed += OnFormClosed;
    }

    /// <summary>
    /// Reports back to the container when we close
    /// </summary>
    void OnFormClosed(object sender, FormClosedEventArgs e)
    {
        MyMdiContainer.ChildClosed(this);
    }

    /// <summary>
    /// Reports back to the parent container when we are activated
    /// </summary>
    private void OnFormActivated(object sender, EventArgs e)
    {
        MyMdiContainer.ChildActivated(this);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文