折腾了几天,先把第一版成果记录下来,吼吼....
一、效果图
二、实现过程源码
1、先创建winform工程 创建好如下文件
2、在Config.cs中构静态数据


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace myDemoFrame.ConfigHelper
{
public static class Config
{
#region 树形导航栏静态数据
public static Dictionary<string, List<Dictionary<string, string>>> SetDicNavigationBar()
{
Dictionary<string, List<Dictionary<string, string>>> dic = new Dictionary<string, List<Dictionary<string, string>>>();
List<Dictionary<string, string>> lst = new List<Dictionary<string, string>>();
Dictionary<string, string> dL = new Dictionary<string, string>();
dL.Add("订单查询", "Form1");
dL.Add("订单管理", "Form2");
dL.Add("订单导出", "Form3");
lst.Add(dL);
dic.Add("模块1", lst);
lst = new List<Dictionary<string, string>>();
dL = new Dictionary<string, string>();
dL.Add("管理员新增", "Form4");
dL.Add("管理员查询", "Form5");
lst.Add(dL);
dic.Add("模块2", lst);
return dic;
}
#endregion
}
}
3、在LeftNavigationBar.cs中 拖入panel treeview 其中Dock设置full LeftNavigationBar 窗体的边框设置none 也可以不继承Form 继承UserControl


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Reflection;
namespace myDemoFrame.MyContorls
{
public partial class LeftNavigationBar : Form
{
/// <summary>
/// Form名字的集合
/// </summary>
public Dictionary<string, List<Dictionary<string, string>>> dic = new Dictionary<string, List<Dictionary<string, string>>>();
public Panel panel;
public LeftNavigationBar()
{
InitializeComponent();
}
private void CreateTree(Dictionary<string, List<Dictionary<string, string>>> dicLst)
{
foreach (var item in dicLst)
{
TreeNode root = new TreeNode();
root.Text = item.Key;
treeView1.Nodes.Add(root);
foreach (var child in item.Value)
{
foreach (var it in child)
{
root.Nodes.Add(it.Key);
}
}
}
}
private Dictionary<string, string> GetValue(Dictionary<string, List<Dictionary<string, string>>> dicLst)
{
Dictionary<string, string> dicK = new Dictionary<string, string>();
foreach (var item in dicLst)
{
foreach (var child in item.Value)
{
foreach (var it in child)
{
dicK.Add(it.Key, it.Value);
}
}
}
return dicK;
}
private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
if (e.Node.Nodes.Count == 0)
{
string strFormName = e.Node.Text;
Dictionary<string, string> dicL = GetValue(dic);
ShowForm(dicL[strFormName]);
}
}
}
private void ShowForm(string strFormName)
{
Form frm = GetForm(strFormName);
frm.BringToFront();
frm.Show();
}
private Form GetForm(string strFormName)
{
Control.ControlCollection ctrls = panel.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;
panel.Controls.Add(frm);
return frm;
}
}
private void LeftNavigationBar_Load(object sender, EventArgs e)
{
CreateTree(dic);
}
}
}
4、MainForm的设计
三个panel 一个menuStrip 其中panel的部署 Dock => Top \ Left \ Fill menuStrip注意几个属性的设置 ImageScaling为none ,TexImageRelation为ImageAboveText, Image插入美工做好的图即可


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace myDemoFrame
{
public partial class ManForm : Form
{
public ManForm()
{
InitializeComponent();
Init();
}
private void Init()
{
MyContorls.LeftNavigationBar mu = new MyContorls.LeftNavigationBar();
mu.dic = ConfigHelper.Config.SetDicNavigationBar();
mu.panel = this.panelMain;
mu.TopLevel = false;
mu.Dock = DockStyle.Left;
panelLeft.Controls.Add(mu);
mu.Show();
}
}
}
来源:oschina
链接:https://my.oschina.net/u/4417586/blog/4306410