C# WinFrom SplitContainer和UserControl组成简单的类似于web系统的框架页

示例效果:
FrameForm分为上左右三区
上区显示TopUC的顶部Logo及欢迎语
左区显示LeftMenuUC的左侧功能按钮菜单
右区显示点击左侧功能按钮后对应的用户控件窗体内容
(事实上功能按钮应该由传统的WinForm菜单来代替 WinForm程序就应该有自己的传统的样式 而不是和Web类似
不过为了类似于web系统的框架页面
所以尝试了本示例)

C# WinFrom SplitContainer和UserControl组成简单的类似于web系统的框架页

示例涉及的窗体及用户控件:
FrameForm 框架窗体 包含两个SplitContainer
SplitContainer1对窗体上下分隔
SplitContainer2对SplitContainer1的Panel2进行左右分隔
TopUC 顶部用户控件
LeftMenuUC 左侧菜单用户控件 其中包含两个功能按钮button1 button2
ContentUC1 内容用户控件1 为button1所调用 将显示的框架的右区
ContentUC2 内容用户控件2 为button1所调用 将显示的框架的右区

FrameForm相关代码:

FrameForm相关代码
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->partialclassFrameForm
{
///<summary>
///必需的设计器变量。
///</summary>
privateSystem.ComponentModel.IContainercomponents=null;

///<summary>
///清理所有正在使用的资源。
///</summary>
///<paramname="disposing">如果应释放托管资源,为true;否则为false。</param>
protectedoverridevoidDispose(booldisposing)
{
if(disposing&&(components!=null))
{
components.Dispose();
}
base.Dispose(disposing);
}

#regionWindows窗体设计器生成的代码

///<summary>
///设计器支持所需的方法-不要
///使用代码编辑器修改此方法的内容。
///</summary>
privatevoidInitializeComponent()
{
this.splitContainer1=newSystem.Windows.Forms.SplitContainer();
this.splitContainer2=newSystem.Windows.Forms.SplitContainer();
this.splitContainer1.Panel2.SuspendLayout();
this.splitContainer1.SuspendLayout();
this.splitContainer2.SuspendLayout();
this.SuspendLayout();
//
//splitContainer1
//
this.splitContainer1.BorderStyle=System.Windows.Forms.BorderStyle.FixedSingle;
this.splitContainer1.Dock=System.Windows.Forms.DockStyle.Fill;
this.splitContainer1.IsSplitterFixed=true;
this.splitContainer1.Location=newSystem.Drawing.Point(0,0);
this.splitContainer1.Name="splitContainer1";
this.splitContainer1.Orientation=System.Windows.Forms.Orientation.Horizontal;
//
//splitContainer1.Panel1
//
this.splitContainer1.Panel1.BackgroundImageLayout=System.Windows.Forms.ImageLayout.Center;
//
//splitContainer1.Panel2
//
this.splitContainer1.Panel2.Controls.Add(this.splitContainer2);
this.splitContainer1.Size=newSystem.Drawing.Size(947,640);
this.splitContainer1.SplitterDistance=103;
this.splitContainer1.TabIndex=0;
//
//splitContainer2
//
this.splitContainer2.BorderStyle=System.Windows.Forms.BorderStyle.FixedSingle;
this.splitContainer2.Dock=System.Windows.Forms.DockStyle.Fill;
this.splitContainer2.Location=newSystem.Drawing.Point(0,0);
this.splitContainer2.Name="splitContainer2";
//
//splitContainer2.Panel2
//
this.splitContainer2.Panel2.BackColor=System.Drawing.SystemColors.ButtonShadow;
this.splitContainer2.Size=newSystem.Drawing.Size(947,533);
this.splitContainer2.SplitterDistance=193;
this.splitContainer2.TabIndex=0;
//
//FrameForm
//
this.AutoScaleDimensions=newSystem.Drawing.SizeF(6F,12F);
this.AutoScaleMode=System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize=newSystem.Drawing.Size(947,640);
this.Controls.Add(this.splitContainer1);
this.Name="FrameForm";
this.Text="FrameForm";
this.Load+=newSystem.EventHandler(this.FrameForm_Load);
this.splitContainer1.Panel2.ResumeLayout(false);
this.splitContainer1.ResumeLayout(false);
this.splitContainer2.ResumeLayout(false);
this.ResumeLayout(false);

}

#endregion

privateSystem.Windows.Forms.SplitContainersplitContainer1;
privateSystem.Windows.Forms.SplitContainersplitContainer2;
}

publicpartialclassFrameForm:Form
{
publicFrameForm()
{
InitializeComponent();
}

privatevoidFrameForm_Load(objectsender,EventArgse)
{
//加载TopForm
TopUCucTop=newTopUC();
this.splitContainer1.Panel1.Controls.Add(ucTop);
ucTop.Show();
//加载LeftMenuForm
LeftMenuUCucLeftMenu=newLeftMenuUC();
this.splitContainer2.Panel1.Controls.Add(ucLeftMenu);
ucLeftMenu.Show();

////加载TopForm
//TopFormfrmTop=newTopForm();
//frmTop.FormBorderStyle=FormBorderStyle.None;
//frmTop.TopLevel=false;
//this.splitContainer1.Panel1.Controls.Add(frmTop);
//frmTop.Show();
////加载LeftMenuForm
//LeftMenuFormfrmLeftMenu=newLeftMenuForm();
//frmLeftMenu.FormBorderStyle=FormBorderStyle.None;
//frmLeftMenu.TopLevel=false;
//this.splitContainer2.Panel1.Controls.Add(frmLeftMenu);
//frmLeftMenu.Show();
}
}

LeftMenuUC 相关代码:

LeftMenuUC 相关代码
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->publicpartialclassLeftMenuUC:UserControl
{
publicLeftMenuUC()
{
InitializeComponent();
}

privatevoidbutton1_Click(objectsender,EventArgse)
{
ContentUC1ucContent1
=newContentUC1();
SplitContainersplit
=(SplitContainer)Parent.Parent;

split.Panel2.Controls.Clear();
split.Panel2.Controls.Add(ucContent1);

//split.Panel2.Controls.Add(ucContent1);
//split.Panel2.Controls.SetChildIndex(ucContent1,0);

ucContent1.Show();
}

privatevoidbutton2_Click(objectsender,EventArgse)
{
ContentUC2ucContent2
=newContentUC2();
SplitContainersplit
=(SplitContainer)Parent.Parent;

split.Panel2.Controls.Clear();
split.Panel2.Controls.Add(ucContent2);

//split.Panel2.Controls.Add(ucContent2);
//split.Panel2.Controls.SetChildIndex(ucContent2,0);

ucContent2.Show();
}
}

你可能感兴趣的:(contain)