我這裏說的多窗口是指一個父窗口包含多個子窗口,在winform中實現這種效果很簡單,即將某個窗口的IsMdiContainer設爲true,而後將其它子窗口的MdiParent設爲其父窗口對象便可,這樣就完成了一個多窗口界面,效果以下:html
點擊NEW新打開一個窗口,其效果以下:web
請看我上圖紅色標註的地方,Windows菜單項下面顯示的是當前全部已打開的子窗口,點擊某個菜單,便可快速切換到其它窗口,若關閉某個子窗口,與之相對應的菜單項也會自動被移除,實現這個功能也很簡單,只須要將菜單的MdiWindowListItem屬性設爲須要顯示活動窗口列表的菜單項便可,例如:上圖中我想讓Windows菜單顯示全部打開的窗口列表,而Windows菜單的(name)屬性是windowsToolStripMenuItem,因此只須要以下代碼,就可讓Windows菜單顯示打開的窗口列表。windows
this.menuStrip1.MdiWindowListItem = this.windowsToolStripMenuItem; //讓windowsToolStripMenuItem菜單對象顯示窗口列表框架
上述示例完整的實現代碼以下:編輯器
public partial class FormMdi : Form { private int formCount = 0; public FormMdi() { InitializeComponent(); this.menuStrip1.MdiWindowListItem = this.windowsToolStripMenuItem; } private void newToolStripMenuItem_Click(object sender, EventArgs e) { ShowChildForm<FormChild>(); } private void ShowChildForm<TForm>() where TForm : Form, new() { TForm childForm = new TForm(); childForm.Name = "frm" + Guid.NewGuid().ToString("N"); childForm.Text = string.Format("Child Form -{0}", ++formCount); childForm.MdiParent = this; childForm.WindowState = FormWindowState.Maximized; childForm.Show(); } }
相信實現上面這部份功能通常用過winform的人都會操做,我這裏就當是複習順便給新手一個參考,同時也爲下面要實現的左右佈局功能作一個鋪墊吧。ide
要實現左右佈局,而且可以支持可動態調整左右佔比的功能,非SplitContainer控件莫屬了,若是不瞭解該控件用法請自行在網上查找相關資料,我這裏就不做說明,若是要顯示WINDOWS已打開的子窗口狀況,一樣也須要用到MenuStrip控件,函數
最終設計的主窗口(FormMain)效果以下:佈局
我這裏由於只是演示,因此菜單控件上我只添加了兩個菜單項,分別爲:WINDOWS,用於顯示WINDOWS已打開的子窗口列表,NEW,用於打開一個子窗口;SplitContainer控件所有采用默認的,沒有放置任何控件在其中,若是用在正式系統中,通常左邊Panel1中會放置一個樹形菜單,右邊Panel2中保持空便可,由於這個是用來做爲子窗口的容器。學習
控件層次結構以下圖示:測試
界面設計好了,下面就實現最重要的兩個功能。
第一個功能:在右邊Panel2中顯示子窗口,實現代碼以下:
public FormMain() { this.IsMdiContainer = true; } private void ShowChildForm<TForm>() where TForm : Form, new() { TForm childForm = new TForm(); childForm.Name = "frm" + Guid.NewGuid().ToString("N"); childForm.Text = string.Format("Child Form -{0}", ++formCount); childForm.MdiParent = this; childForm.Parent = splitContainer1.Panel2; childForm.WindowState = FormWindowState.Maximized; childForm.Show(); }
簡要說明:
1.在窗口構造函數中動態的將IsMdiContainer設爲true,固然也能夠設計視圖中設置;
2.編寫一個顯示寫子窗口的方法,方法中需注意的地方:childForm.MdiParent = this;childForm.Parent = splitContainer1.Panel2,意思是:將當前窗口做爲子窗口的父窗口,同時將Panel2指定爲子窗口的父對象,這樣就能實現子窗口在Panel2中打開了。
第二個功能:在WINDOWS菜單項下顯示已打開的子窗口列表,這裏實現就沒有像文章一開始介紹的那樣簡單,使用那個方法是無效的,須要咱們來自行實現,稍微有點複雜,但若是明白其實現原理,也就簡單明白了。
實現思路:當childForm加載到Panel2時,會觸發Panel2.ControlAdded事件,當childForm被關閉時,會觸發Panel2.ControlRemoved事件,咱們能夠統一訂閱這兩個事件,當childForm加載時,那麼就在WINDOWS菜單項下增長一個菜單項,反之則移除該菜單項,實現代碼以下:
this.splitContainer1.Panel2.ControlAdded += Panel2_ControlChanged; this.splitContainer1.Panel2.ControlRemoved += Panel2_ControlChanged; void Panel2_ControlChanged(object sender, ControlEventArgs e) { var frm = e.Control as Form; string menuName = "menu_" + frm.Name; bool exists = this.splitContainer1.Panel2.Controls.Contains(frm); if (exists) { var menuItem = GetMenuItem(menuName); if (menuItem != null) { menuItem.Checked = true; frm.BringToFront(); frm.Focus(); } else { windowsToolStripMenuItem.DropDownItems.Add(new ToolStripMenuItem() { Text = frm.Text, Name = menuName, Tag = frm, Checked = true }); } } else { var menuItem = GetMenuItem(menuName); if (menuItem != null) { windowsToolStripMenuItem.DropDownItems.Remove(menuItem); menuItem.Dispose(); } } } private ToolStripMenuItem GetMenuItem(string menuName) { var menuItems = windowsToolStripMenuItem.DropDownItems.Cast<ToolStripMenuItem>(); menuItems.ToList().ForEach(m => m.Checked = false); return menuItems.Where(m => m.Name == menuName).SingleOrDefault(); }
同時爲了實現點擊WINDOWS菜單項的子菜單可以快速切換子窗口,須要訂閱WINDOWS菜單項的DropDownItemClicked事件,固然也能夠爲新增的子菜單項訂閱Click事件,實現代碼以下:
windowsToolStripMenuItem.DropDownItemClicked += windowsToolStripMenuItem_DropDownItemClicked; void windowsToolStripMenuItem_DropDownItemClicked(object sender, ToolStripItemClickedEventArgs e) { var menuItem = GetMenuItem(e.ClickedItem.Name); menuItem.Checked = true; var childForm = menuItem.Tag as Form; childForm.BringToFront(); childForm.Focus(); } private void CheckWindowsMenuItem(string menuName) { var menuItem = GetMenuItem(menuName); if (menuItem != null) { menuItem.Checked = true; } }
這樣就基本實現了在WINDOWS菜單項下顯示已打開的子窗口列表,並點擊指定的菜單項可以切換當前活動的子窗口,但仍有一個不足的地方,那就是,當直接點擊子窗口來切換當前活動窗口時(說白了就是當點擊某個子窗口標題欄,該窗口就顯示在其它全部的子窗口最前面),WINDOWS菜單項下的子菜單勾選項沒有同步更新,一開始想到的是用Activated事件來進行處理,結果經測試發現有效,該Activated事件在點擊子窗口標題欄時並不會被觸發,因此只能換種方法,通過屢次測試,發現當窗口從後面切換到前面時(稱爲Z順序改變),子窗口就會發生重繪,從而觸發Paint方法,咱們能夠訂閱該事件,並進行處理,實現代碼以下:
private string currentChildFormName = null; //記錄當前活動子窗口名稱 childForm.Paint += (s, e) => { var frm=s as Form; if (!frm.Name.Equals(currentChildFormName) && this.splitContainer1.Panel2.Controls[0].Equals(frm)) //當容器中第一個控件就是當前的窗口,則代表該窗口處於全部窗口之上 { CheckWindowsMenuItem("menu_" + frm.Name); currentChildFormName = frm.Name; } };
最後貼出完整的實現代碼:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace WindowsFormsApplication1 { public partial class FormMain : Form { private int formCount = 0; private string currentChildFormName = null; public FormMain() { InitializeComponent(); this.IsMdiContainer = true; this.splitContainer1.Panel2.ControlAdded += Panel2_ControlChanged; this.splitContainer1.Panel2.ControlRemoved += Panel2_ControlChanged; windowsToolStripMenuItem.DropDownItemClicked += windowsToolStripMenuItem_DropDownItemClicked; } void windowsToolStripMenuItem_DropDownItemClicked(object sender, ToolStripItemClickedEventArgs e) { var menuItem = GetMenuItem(e.ClickedItem.Name); menuItem.Checked = true; var childForm = menuItem.Tag as Form; childForm.BringToFront(); childForm.Focus(); } private void FormMain_Load(object sender, EventArgs e) { ShowChildForm<FormChild>(); } private void ShowChildForm<TForm>() where TForm : Form, new() { TForm childForm = new TForm(); childForm.Name = "frm" + Guid.NewGuid().ToString("N"); childForm.Text = string.Format("Child Form -{0}", ++formCount); childForm.MdiParent = this; childForm.Parent = splitContainer1.Panel2; childForm.WindowState = FormWindowState.Maximized; childForm.Paint += (s, e) => { var frm=s as Form; if (!frm.Name.Equals(currentChildFormName) && this.splitContainer1.Panel2.Controls[0].Equals(frm)) //當容器中第一個控件就是當前的窗口,則代表該窗口處於全部窗口之上 { CheckWindowsMenuItem("menu_" + frm.Name); currentChildFormName = frm.Name; } }; childForm.Show(); } private void CheckWindowsMenuItem(string menuName) { var menuItem = GetMenuItem(menuName); if (menuItem != null) { menuItem.Checked = true; } } void Panel2_ControlChanged(object sender, ControlEventArgs e) { var frm = e.Control as Form; string menuName = "menu_" + frm.Name; bool exists = this.splitContainer1.Panel2.Controls.Contains(frm); if (exists) { var menuItem = GetMenuItem(menuName); if (menuItem != null) { menuItem.Checked = true; frm.BringToFront(); frm.Focus(); } else { windowsToolStripMenuItem.DropDownItems.Add(new ToolStripMenuItem() { Text = frm.Text, Name = menuName, Tag = frm, Checked = true }); } } else { var menuItem = GetMenuItem(menuName); if (menuItem != null) { windowsToolStripMenuItem.DropDownItems.Remove(menuItem); menuItem.Dispose(); } } } private ToolStripMenuItem GetMenuItem(string menuName) { var menuItems = windowsToolStripMenuItem.DropDownItems.Cast<ToolStripMenuItem>(); menuItems.ToList().ForEach(m => m.Checked = false); return menuItems.Where(m => m.Name == menuName).SingleOrDefault(); } private void newToolStripMenuItem_Click(object sender, EventArgs e) { ShowChildForm<FormChild>(); } } }
如下是系統自動生成的代碼:
namespace WindowsFormsApplication1 { partial class FormMain { /// <summary> /// 必需的設計器變量。 /// </summary> private System.ComponentModel.IContainer components = null; /// <summary> /// 清理全部正在使用的資源。 /// </summary> /// <param name="disposing">若是應釋放託管資源,爲 true;不然爲 false。</param> protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Windows 窗體設計器生成的代碼 /// <summary> /// 設計器支持所需的方法 - 不要 /// 使用代碼編輯器修改此方法的內容。 /// </summary> private void InitializeComponent() { this.menuStrip1 = new System.Windows.Forms.MenuStrip(); this.windowsToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.newToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.splitContainer1 = new System.Windows.Forms.SplitContainer(); this.menuStrip1.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.splitContainer1)).BeginInit(); this.splitContainer1.SuspendLayout(); this.SuspendLayout(); // // menuStrip1 // this.menuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { this.windowsToolStripMenuItem, this.newToolStripMenuItem}); this.menuStrip1.Location = new System.Drawing.Point(0, 0); this.menuStrip1.MdiWindowListItem = this.windowsToolStripMenuItem; this.menuStrip1.Name = "menuStrip1"; this.menuStrip1.Size = new System.Drawing.Size(1069, 25); this.menuStrip1.TabIndex = 1; this.menuStrip1.Text = "menuStrip1"; // // windowsToolStripMenuItem // this.windowsToolStripMenuItem.Name = "windowsToolStripMenuItem"; this.windowsToolStripMenuItem.Size = new System.Drawing.Size(73, 21); this.windowsToolStripMenuItem.Text = "Windows"; this.windowsToolStripMenuItem.Click += new System.EventHandler(this.windowsToolStripMenuItem_Click); // // newToolStripMenuItem // this.newToolStripMenuItem.Name = "newToolStripMenuItem"; this.newToolStripMenuItem.Size = new System.Drawing.Size(46, 21); this.newToolStripMenuItem.Text = "New"; this.newToolStripMenuItem.Click += new System.EventHandler(this.newToolStripMenuItem_Click); // // splitContainer1 // this.splitContainer1.BackColor = System.Drawing.SystemColors.ActiveCaption; this.splitContainer1.Dock = System.Windows.Forms.DockStyle.Fill; this.splitContainer1.Location = new System.Drawing.Point(0, 25); this.splitContainer1.Name = "splitContainer1"; // // splitContainer1.Panel2 // this.splitContainer1.Panel2.BackColor = System.Drawing.SystemColors.ScrollBar; this.splitContainer1.Size = new System.Drawing.Size(1069, 526); this.splitContainer1.SplitterDistance = 356; this.splitContainer1.TabIndex = 2; // // FormMain // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(1069, 551); this.Controls.Add(this.splitContainer1); this.Controls.Add(this.menuStrip1); this.MainMenuStrip = this.menuStrip1; this.Name = "FormMain"; this.Text = "FormMain"; this.Load += new System.EventHandler(this.FormMain_Load); this.menuStrip1.ResumeLayout(false); this.menuStrip1.PerformLayout(); ((System.ComponentModel.ISupportInitialize)(this.splitContainer1)).EndInit(); this.splitContainer1.ResumeLayout(false); this.ResumeLayout(false); this.PerformLayout(); } #endregion private System.Windows.Forms.MenuStrip menuStrip1; private System.Windows.Forms.ToolStripMenuItem windowsToolStripMenuItem; private System.Windows.Forms.SplitContainer splitContainer1; private System.Windows.Forms.ToolStripMenuItem newToolStripMenuItem; } }
如下是效果演示截圖:
以上內容給你們分享了在winform下實現左右佈局多窗口界面的方法,有什麼更好的實現方法能夠在下方評論,不足之處也歡迎指出,謝謝!下面將給你們介紹在winform下實現左右佈局多窗口界面的方法(二)---- 之續篇,感興趣的朋友繼續關注。
出處:http://www.jb51.net/article/80194.htm