折騰了幾天,先把第一版成果記錄下來,吼吼.... 一、效果圖 二、實現過程源碼 1、先創建winform工程 創建好如下文件 2、在Config.cs中構靜態數據 using System; using System.Collections.Generic; using System.Linq; u ...
折騰了幾天,先把第一版成果記錄下來,吼吼....
一、效果圖
二、實現過程源碼
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 } }View Code
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); } } }View Code
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(); } } }View Code