示例效果:
FrameForm分为上左右三区
上区显示TopUC的顶部Logo及欢迎语
左区显示LeftMenuUC的左侧功能按钮菜单
右区显示点击左侧功能按钮后对应的用户控件窗体内容
(事实上功能按钮应该由传统的WinForm菜单来代替 WinForm程序就应该有自己的传统的样式 而不是和Web类似
不过为了类似于web系统的框架页面
所以尝试了本示例)
示例涉及的窗体及用户控件:
FrameForm 框架窗体 包含两个SplitContainer
SplitContainer1对窗体上下分隔
SplitContainer2对SplitContainer1的Panel2进行左右分隔
TopUC 顶部用户控件
LeftMenuUC 左侧菜单用户控件 其中包含两个功能按钮button1 button2
ContentUC1 内容用户控件1 为button1所调用 将显示的框架的右区
ContentUC2 内容用户控件2 为button1所调用 将显示的框架的右区
FrameForm相关代码:


partial class FrameForm
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.splitContainer1 = new System.Windows.Forms.SplitContainer();
this.splitContainer2 = new System.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 = new System.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 = new System.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 = new System.Drawing.Point(0, 0);
this.splitContainer2.Name = "splitContainer2";
//
// splitContainer2.Panel2
//
this.splitContainer2.Panel2.BackColor = System.Drawing.SystemColors.ButtonShadow;
this.splitContainer2.Size = new System.Drawing.Size(947, 533);
this.splitContainer2.SplitterDistance = 193;
this.splitContainer2.TabIndex = 0;
//
// FrameForm
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(947, 640);
this.Controls.Add(this.splitContainer1);
this.Name = "FrameForm";
this.Text = "FrameForm";
this.Load += new System.EventHandler(this.FrameForm_Load);
this.splitContainer1.Panel2.ResumeLayout(false);
this.splitContainer1.ResumeLayout(false);
this.splitContainer2.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.SplitContainer splitContainer1;
private System.Windows.Forms.SplitContainer splitContainer2;
}
public partial class FrameForm : Form
{
public FrameForm()
{
InitializeComponent();
}
private void FrameForm_Load(object sender, EventArgs e)
{
//加载TopForm
TopUC ucTop = new TopUC();
this.splitContainer1.Panel1.Controls.Add(ucTop);
ucTop.Show();
//加载LeftMenuForm
LeftMenuUC ucLeftMenu = new LeftMenuUC();
this.splitContainer2.Panel1.Controls.Add(ucLeftMenu);
ucLeftMenu.Show();
////加载TopForm
//TopForm frmTop = new TopForm();
//frmTop.FormBorderStyle = FormBorderStyle.None;
//frmTop.TopLevel = false;
//this.splitContainer1.Panel1.Controls.Add(frmTop);
//frmTop.Show();
////加载LeftMenuForm
//LeftMenuForm frmLeftMenu = new LeftMenuForm();
//frmLeftMenu.FormBorderStyle = FormBorderStyle.None;
//frmLeftMenu.TopLevel = false;
//this.splitContainer2.Panel1.Controls.Add(frmLeftMenu);
//frmLeftMenu.Show();
}
}
LeftMenuUC 相关代码:


public partial class LeftMenuUC : UserControl
{
public LeftMenuUC()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
ContentUC1 ucContent1 = new ContentUC1();
SplitContainer split = (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();
}
private void button2_Click(object sender, EventArgs e)
{
ContentUC2 ucContent2 = new ContentUC2();
SplitContainer split = (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();
}
}