先看看效果:
核心代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Reflection; namespace WinFormDemo
{
public partial class FrmMain : Form
{
public FrmMain()
{
InitializeComponent();
} private void tvMenu_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e)
{
if (null != e.Node.Parent)
{
if ( == e.Node.Nodes.Count)
{
ShowForm(e.Node.Text);
}
}
} private void ShowForm(string strFormName)
{
Form frm = GetForm(strFormName);
frm.BringToFront();
frm.Show();
} private Form GetForm(string strFormName)
{
Control.ControlCollection ctrls = pnlCentral.Controls;
if (ctrls.ContainsKey(strFormName))
{
return ctrls[strFormName] as Form;
}
else
{
Assembly assembly = Assembly.GetExecutingAssembly();
Type type = assembly.GetTypes().Where(item => item.Name.Equals(strFormName)).FirstOrDefault(); Form frm = assembly.CreateInstance(type.FullName) as Form;
frm.Name = strFormName;
frm.TopLevel = false;
frm.Dock = DockStyle.Fill;
frm.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
pnlCentral.Controls.Add(frm); return frm;
}
}
}
}
以上就是FrmMain主界面核心代码。以上代码没有进行优化,根据个人经验自己优化下。