C#WinForm父级窗体内Panel容器中嵌入子窗体、程序主窗体设计例子

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导航面板

可以专门写一个方法做显示窗体

/// <summary>
        /// 显示窗体
        /// </summary>
        /// <param name="panel"></param>
        /// <param name="frm"></param>
        public void ShowForm(System.Windows.Forms.Panel panel, System.Windows.Forms.Form frm)
        {
            lock (this)
            {
                try
                {
                    if (this.currentForm != null && this.currentForm == frm)
                    {
                        return;
                    }
                    else if (this.currentForm != null)
                    {
                        if (this.ActiveMdiChild != null)
                        {
                            this.ActiveMdiChild.Hide();
                        }
                    }
                    this.currentForm = frm;
                    frm.TopLevel = false;
                    frm.MdiParent = this;
                    panel.Controls.Clear();
                    panel.Controls.Add(frm);
                    frm.Show();
                    frm.Dock = System.Windows.Forms.DockStyle.Fill;
                    this.Refresh();
                    foreach (Control item in frm.Controls)
                    {
                        item.Focus();
                        break;
                    }
                }
                catch (System.Exception ex)
                {
                    //
                }
            }
        }

子窗体静态字段

/// <summary>
        /// 子窗体界面单例元素
        /// </summary>
        public static Form1 form1 = null;
        public static Form2 form2 = null;
        public static Form3 form3 = null;
        public static Form4 form4 = null;
        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;

窗体单例

private static MainForm formInstance;
        public static MainForm GetIntance
        {
            get
            {
                if (formInstance != null)
                {
                    return formInstance;
                }
                else
                {
                    formInstance = new MainForm();
                    return formInstance;
                }
            }
        }

初始化按钮状态

private bool initButton()
        {
            try
            {
                this.button1.BackColor = Color.FromArgb(53, 66, 83);
                this.button2.BackColor = Color.FromArgb(53, 66, 83);
                this.button3.BackColor = Color.FromArgb(53, 66, 83);
                this.button4.BackColor = Color.FromArgb(53, 66, 83);
                this.button5.BackColor = Color.FromArgb(53, 66, 83);
                this.button6.BackColor = Color.FromArgb(53, 66, 83);
                this.button7.BackColor = Color.FromArgb(53, 66, 83);
                this.button8.BackColor = Color.FromArgb(53, 66, 83);
                this.button9.BackColor = Color.FromArgb(53, 66, 83);
                this.button10.BackColor = Color.FromArgb(53, 66, 83);
                this.button11.BackColor = Color.FromArgb(53, 66, 83);
            }
            catch (Exception ex)
            {
                return false;
            }
            return true;
        }

导航按钮单击切换事件

private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                this.initButton();
                this.button1.BackColor = Color.FromArgb(95, 129, 174);
                Monitor.Enter(this.lockObj);
                if (!formSwitchFlag)
                {
                    formSwitchFlag = true;
                    this.ShowForm(pnlCenter,form1);
                    formSwitchFlag = false;
                }
                else
                {
                    return;
                }
            }
            catch (System.Exception ex)
            {
                //
            }
            finally
            {
                Monitor.Exit(this.lockObj);
            }
        }

最终展现效果图

程序源代码工程文件下载

小伙伴们直接去这里下载:

https://files.cnblogs.com/files/JiYF/GUIDesign.rar 

 

  • 11
    点赞
  • 64
    收藏
    觉得还不错? 一键收藏
  • 54
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 54
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值