標籤:color 底部 .net clear 下載 bottom containe fill 效果
C#WinForm父級表單內Panel容器中嵌入子表單、程式主表單設計例子
在項目開發中經常遇到父級表單嵌入子表單所以寫了一個例子程式,順便大概劃分了下介面模組和配色,不足之處還望指點
主表單表單採用前面一篇部落格設計扁平化表單
在項目開發中經常遇到父級表單嵌入子表單所以寫了一個例子程式,順便大概劃分了下介面模組和配色,不足之處還望指點
主表單表單採用前面一篇部落格設計扁平化表單
C#自訂Winform無邊框表單
主要思路
1 this.IsMdiContainer=true;//設定父表單是容器2 Son mySon=new Son();//執行個體化子表單3 mySon.MdiParent=this;//設定表單的父子關係4 mySon.Parent=pnl1;//設定子表單的容器為父表單中的Panel5 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 try11 {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 else11 {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 else15 {16 return;17 }18 }19 catch (System.Exception ex)20 {21 //22 }23 finally24 {25 Monitor.Exit(this.lockObj);26 }27 }
最終展現動態效果
程式原始碼工程檔案下載
C#WinForm表單內Panel容器中嵌入子表單、程式主表單設計例子