C#WinForm父级窗体内Panel容器中嵌入子窗体、程序主窗体设计例子
在项目开发中经常遇到父级窗体嵌入子窗体所以写了一个例子程序,顺便大概划分了下界面模块和配色,不足之处还望指点
主窗体窗体采用前面一篇博客设计扁平化窗体
主要思路
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 }
最终展现效果图
程序源代码工程文件下载
from: https://www.cnblogs.com/JiYF/p/9031699.html