C#WinForm窗体内Panel容器中嵌入子窗体、程序主窗体设计例子
C#WinForm父级窗体内Panel容器中嵌入子窗体、程序主窗体设计例子
在项目开发中经常遇到父级窗体嵌入子窗体所以写了一个例子程序,顺便大概划分了下界面模块和配色,不足之处还望指点
主窗体窗体采用前面一篇博客设计扁平化窗体
C#自定义Winform无边框窗体
主要思路
this.IsMdiContainer=true;//设置父窗体是容器
Son mySon=new Son();//实例化子窗体
mySon.MdiParent=this;//设置窗体的父子关系
mySon.Parent=pnl1;//设置子窗体的容器为父窗体中的Panel
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;
构造初始化窗体 这里每一个窗体都是一个单例保证窗体的唯一性
//实例化子窗体界面
form1 = Form1.GetIntance;
form2 = Form2.GetIntance;
form3 = Form3.GetIntance;
form4 = Form4.GetIntance;
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(, , );
this.button2.BackColor = Color.FromArgb(, , );
this.button3.BackColor = Color.FromArgb(, , );
this.button4.BackColor = Color.FromArgb(, , );
this.button5.BackColor = Color.FromArgb(, , );
this.button6.BackColor = Color.FromArgb(, , );
this.button7.BackColor = Color.FromArgb(, , );
this.button8.BackColor = Color.FromArgb(, , );
this.button9.BackColor = Color.FromArgb(, , );
this.button10.BackColor = Color.FromArgb(, , );
this.button11.BackColor = Color.FromArgb(, , );
}
catch (Exception ex)
{
return false;
}
return true;
}
导航按钮单击切换事件
private void button1_Click(object sender, EventArgs e)
{
try
{
this.initButton();
this.button1.BackColor = Color.FromArgb(, , );
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
复制到浏览器即可下载
或者博客留言,给出邮箱!基本一天内可以回复,就酱紫,爱你们呦,么么哒!O(∩_∩)O
C#WinForm窗体内Panel容器中嵌入子窗体、程序主窗体设计例子的更多相关文章
- Qt界面中嵌入其他exe程序的界面,使用Qt5
下面用一个小例子来演示如何在Qt的界面中嵌入其他exe程序的界面,最终效果如下图所示.本文参考了 http://blog.csdn.net/jiaoyaziyang/article/details/4 ...
- 接下来将介绍C#如何设置子窗体在主窗体中居中显示,本文提供详细的操作步骤,需要的朋友可以参考下
接下来将介绍C#如何设置子窗体在主窗体中居中显示,本文提供详细的操作步骤,需要的朋友可以参考下 其实表面上看是很简单的 开始吧,现在有两个窗体Form1主窗体,Form2子窗体 而且我相信大部分人都会 ...
- wpf 子窗体调用主窗体中函数的实现
子窗体(Win_Set): MainWindow m; m = (MainWindow)this.Owner; m.showText(&qu ...
- c#在主窗体panel 容器内嵌入另一个窗体(子窗体)的实现
主窗体: 子窗体: 把子窗体嵌入到主窗体的panel 右侧中: 代码: { public MainForm() { InitializeComponent(); } private void Clo ...
- WPF中嵌入普通Win32程序的方法
公司现在在研发基于.Net中WPF技术的产品,由于要兼容旧有产品,比如一些旧有的Win32程序.第三方的Win32程序等等,还要实现自动登录这些外部Win32程序,因此必须能够将这些程序整合到我们的系 ...
- Docker容器中安装新的程序
在容器里面安装一个简单的程序(ping). 之前下载的是ubuntu的镜像,则可以使用ubuntu的apt-get命令来安装ping程序:apt-get install -y ping. $docke ...
- Unity容器中AOP应用示例程序
转发请注明出处:https://www.cnblogs.com/zhiyong-ITNote/p/9127001.html 实在没有找到Unity容器的AOP应用程序示例的说明,在微软官网找到了教程( ...
- 2017-11-29 由runnable说起Android中的子线程和主线程
1.首先纠正一个观点,就是runnable运行在子线程中是错误的观念.runnable只是创建了一个执行任务的对象,但是它本身并不会创建一个新的子线程,Runable只是给你接口让你实现工作线程的工作 ...
- C#设置窗体中的窗体随主窗体大小变化而变化
form2 f=new form2(); f.Size=this.Size; f.Location=this.Location; f.showdialog(); 作者:耑新新,发布于 博客园 转载请 ...
随机推荐
- 【搜索】WAR大佬的SET @upcexam6201
时间限制: 1 Sec 内存限制: 128 MB 题目描述 WAR大佬认为一个包含重复元素的集合认为是优美的,当且仅当集合中的元素的和等于他们的积. 求包含n个元素的优美的集合的个数. WAR大佬当然 ...
- 基于SwiperJs的H5/移动端下拉刷新上拉加载更多
最早时,公司的H5项目中曾用过点击一个"加载更多"的DOM元素来实现分页的功能,后来又用过网上有人写的一个上拉加载更多的插件,那个插件是页面将要滚动到底部时就自动请求数据并插入到页 ...
- html的文字样式、下行线、删除线、上标、下标等实现方式
先看效果如下: 代码如下: <del>del标签删除线</del><br/> <strike>strike标签删除线</strike>< ...
- tensorflow由于未初始化变量所导致的错误
版权声明:本文为博主原创文章,如需转载请注明出处,谢谢. https://blog.csdn.net/qq_38542085/article/details/78742295 初始代码 import ...
- C# Task 是什么?返回值如何实现? Wait如何实现
关于Task的API太多了,网上的实例也很多,现在我们来说说Task究竟是个什么东西[task一般用于多线程,它一定与线程有关],还有它的返回值有事怎么搞的. 首先我们以一个最简单的API开始,Tas ...
- js 图片base64转file文件的两种方式
js 图片base64转file文件的两种方式 https://blog.csdn.net/yin13037173186/article/details/83302628 //将base64转换为bl ...
- GPG(GnuPG)入门
GPG(GnuPG)入门 下载: https://gnupg.org/download/index.html 或者 http://www.hellopp.cn/page/5b9a1405c3f1f7d ...
- wsl install lamp
sudo apt-get update sudo apt-get install lamp-server^ /etc/init.d/apache2 start /etc/init.d/mysql st ...
- [古怪问题] Marshal.GetActiveObject 在管理员模式下无法正常运行
问题: var obj = Marshal.GetActiveObject("PowerPoint.Application") 该代码在管理员模式下运行无法正常获取正在运行的 PP ...
- spring 中单例 bean 初始化之后和销毁之前执行指定动作 postconstruct 和 preDestroy
1 生命周期方法, 在指定bean 创建完成后执行初始化动作或销毁之前做一些善后动作.有 3 种方法 1)实现接口 InitializingBean 然后实现 afterPropertiesSet 方 ...