2009-06-02 61 views
3

在C#中,我想創建一個具有MDI容器屬性的面板。 isMdiContainer = true。C#面板作爲MDI容器

我想是這樣的

form.MDIParent = this.panel1; 

但這不工作。有什麼建議麼?

回答

2

您可以創建自定義表單,刪除所有邊框和工具欄,使之儘可能接近看面板越好。然後使新的自定義窗體成爲MdiContainer。

基本上,你只能設置一個窗體上IsMdiContainer屬性。這意味着只有一個表單可以是MdiContainer。

+0

我沒有什麼你得到的,但你不能嵌套MDI形式:( – Ozzy 2009-06-02 19:28:53

+0

我終於得到你換貨有關自定義表單件事是什麼使它成爲一個用戶控件:d – Ozzy 2009-06-05 16:34:34

10

它可以創建一個MDI面板和顯示形式在面板上,像下面的代碼將做的工作

MDI面板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; 
     } 
    } 
} 

用法:

/// mdiChildForm is the form that should be showed in the panel 
/// mdiClientPanel is an instance of the MdiClientPanel 
myMdiChildForm.MdiParent = mdiClientPanel1.MdiForm; 
1

我用馬蒂亞斯的回答以上,並能擺脫大部分在意見中提出的問題。我還爲子表單創建了一個輔助類,以防有人想使用它和/或使其更好。

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 (DataTableForm 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); 
    } 
}