C#WinForm父級窗體內Panel容器中嵌入子窗體、程序主窗體設計例子
在項目開發中經常遇到父級窗體嵌入子窗體所以寫了一個例子程序,順便大概划分了下界面模塊和配色,不足之處還望指點
主窗體窗體采用前面一篇博客設計扁平化窗體
C#自定義Winform無邊框窗體
主要思路
1 this.IsMdiContainer=true;//設置父窗體是容器 2 Son mySon=new Son();//實例化子窗體 3 mySon.MdiParent=this;//設置窗體的父子關系 4 mySon.Parent=pnl1;//設置子窗體的容器為父窗體中的Panel 5 mySon.Show();//顯示子窗體,此句很重要,否則子窗體不會顯示
窗體設計上中下結構,最頂部是導航欄,其次是Top窗體部分、中間是Center內容部分、最底部是bottom導航面板

可以專門寫一個方法做顯示窗體
1 /// <summary>
2 /// 顯示窗體
3 /// </summary>
4 /// <param name="panel"></param>
5 /// <param name="frm"></param>
6 public void ShowForm(System.Windows.Forms.Panel panel, System.Windows.Forms.Form frm)
7 {
8 lock (this)
9 {
10 try
11 {
12 if (this.currentForm != null && this.currentForm == frm)
13 {
14 return;
15 }
16 else if (this.currentForm != null)
17 {
18 if (this.ActiveMdiChild != null)
19 {
20 this.ActiveMdiChild.Hide();
21 }
22 }
23 this.currentForm = frm;
24 frm.TopLevel = false;
25 frm.MdiParent = this;
26 panel.Controls.Clear();
27 panel.Controls.Add(frm);
28 frm.Show();
29 frm.Dock = System.Windows.Forms.DockStyle.Fill;
30 this.Refresh();
31 foreach (Control item in frm.Controls)
32 {
33 item.Focus();
34 break;
35 }
36 }
37 catch (System.Exception ex)
38 {
39 //
40 }
41 }
42 }
子窗體靜態字段
1 /// <summary> 2 /// 子窗體界面單例元素 3 /// </summary> 4 public static Form1 form1 = null; 5 public static Form2 form2 = null; 6 public static Form3 form3 = null; 7 public static Form4 form4 = null; 8 public static Form5 form5 = null;
構造初始化窗體 這里每一個窗體都是一個單例保證窗體的唯一性
1 //實例化子窗體界面 2 form1 = Form1.GetIntance; 3 form2 = Form2.GetIntance; 4 form3 = Form3.GetIntance; 5 form4 = Form4.GetIntance; 6 form5 = Form5.GetIntance;
窗體單例
1 private static MainForm formInstance;
2 public static MainForm GetIntance
3 {
4 get
5 {
6 if (formInstance != null)
7 {
8 return formInstance;
9 }
10 else
11 {
12 formInstance = new MainForm();
13 return formInstance;
14 }
15 }
16 }
初始化按鈕狀態
1 private bool initButton()
2 {
3 try
4 {
5 this.button1.BackColor = Color.FromArgb(53, 66, 83);
6 this.button2.BackColor = Color.FromArgb(53, 66, 83);
7 this.button3.BackColor = Color.FromArgb(53, 66, 83);
8 this.button4.BackColor = Color.FromArgb(53, 66, 83);
9 this.button5.BackColor = Color.FromArgb(53, 66, 83);
10 this.button6.BackColor = Color.FromArgb(53, 66, 83);
11 this.button7.BackColor = Color.FromArgb(53, 66, 83);
12 this.button8.BackColor = Color.FromArgb(53, 66, 83);
13 this.button9.BackColor = Color.FromArgb(53, 66, 83);
14 this.button10.BackColor = Color.FromArgb(53, 66, 83);
15 this.button11.BackColor = Color.FromArgb(53, 66, 83);
16 }
17 catch (Exception ex)
18 {
19 return false;
20 }
21 return true;
22 }
導航按鈕單擊切換事件
1 private void button1_Click(object sender, EventArgs e)
2 {
3 try
4 {
5 this.initButton();
6 this.button1.BackColor = Color.FromArgb(95, 129, 174);
7 Monitor.Enter(this.lockObj);
8 if (!formSwitchFlag)
9 {
10 formSwitchFlag = true;
11 this.ShowForm(pnlCenter,form1);
12 formSwitchFlag = false;
13 }
14 else
15 {
16 return;
17 }
18 }
19 catch (System.Exception ex)
20 {
21 //
22 }
23 finally
24 {
25 Monitor.Exit(this.lockObj);
26 }
27 }
最終展現效果圖


