C#WinForm父级窗体内Panel容器中嵌入子窗体、程序主窗体设计例子
在项目开发中经常遇到父级窗体嵌入子窗体所以写了一个例子程序,顺便大概划分了下界面模块和配色,不足之处还望指点
主窗体窗体采用前面一篇博客设计扁平化窗体
主要思路
this.IsMdiContainer=true;//设置父窗体是容器
Son mySon=new Son();//实例化子窗体
mySon.MdiParent=this;//设置窗体的父子关系
mySon.Parent=pnl1;//设置子窗体的容器为父窗体中的Panel
mySon.Show();//显示子窗体,此句很重要,否则子窗体不会显示
窗体设计上中下结构,最顶部是导航栏,其次是Top窗体部分、中间是Center内容部分、最底部是bottom导航面板
可以专门写一个方法做显示窗体
///
/// 显示窗体
///
///
///
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