1 先上效果圖
導(dǎo)航樹徒矗靠在MDI窗體的左邊,可拖動(dòng)改變大小集峦,打開的窗口在【窗口】菜單下有列表伏社。
VS2010實(shí)現(xiàn)這個(gè)其實(shí)很簡單,走了不少彎路塔淤,我直接上最簡單的方案
2 先創(chuàng)建一個(gè)MDIForm
窗體的 isMdiContainer = true
3 窗體上放一個(gè)MenuStrip菜單 name = menuMDI
一級(jí)菜單 輸入 【開始】name = startMenu摘昌、【窗口】 name = windowsMenuItem兩項(xiàng)
【開始】項(xiàng)下增加【新建】name = newMenuItem【-】、 【退出】name=exitMenuItem
【窗口】項(xiàng)下增加【導(dǎo)航樹】name = treeMenuItem
4 MDIForm窗體內(nèi)放置一個(gè)TreeView控件treeView1,
其中 Dock = left
靠近treeView1再放置一個(gè)Splitter控件splitter1,不用任何代碼就能拖動(dòng)改變treeview的寬度高蜂。
5 Treeview控件的使用百度或自己腦補(bǔ)
以上設(shè)置和布局基本實(shí)現(xiàn)導(dǎo)航樹 功能聪黎。
6 核心代碼
6.1新建測試窗體
private void newMenuItem_Click(object sender, EventArgs e)
{
Form f = new Form();
ll = ll + 11;
f.Name = ll.ToString();
f.Text = f.Name;
f.MdiParent = this;
f.Show();
}
6.2 退出程序
private void exitMenuItem_Click(object sender, EventArgs e)
{
Application.Exit();
}
6.3 導(dǎo)航樹開關(guān)
private void treeMenuItem_Click(object sender, EventArgs e)
{
if (treeView1.Visible)
{
treeView1.Visible = false;
treeMenuItem.Checked = false;
}
else {
treeView1.Visible = true;
treeMenuItem.Checked = true;
}
}
7 全部代碼
using System;
using System.Windows.Forms;
using System.Reflection;
using AppUI.SDI;
using System.Data;
using BLL;
namespace AppUI
{
public partial class MDIForm : Form
{
public MDIForm()
{
InitializeComponent();
}
//全部菜單項(xiàng)
DataTable dt = null;
private void MDIForm_Load(object sender, EventArgs e)
{
dt = BLLsysMemu.Select();
//創(chuàng)建treeview根節(jié)點(diǎn)
TreeNode tn0 = treeView1.Nodes.Add("我的導(dǎo)航");
tn0.ImageIndex = 0;
tn0.SelectedImageIndex = 0;
//TreeNode tn1 = new TreeNode("基礎(chǔ)數(shù)據(jù)");
//tn1.Tag = "1000";
//TreeNode tn2 = new TreeNode("業(yè)務(wù)數(shù)據(jù)");
//tn2.Tag = "2000";
//TreeNode tn3 = new TreeNode("系統(tǒng)管理");
//tn3.Tag = "9000";
//tn0.Nodes.Add(tn1);
//tn0.Nodes.Add(tn2);
//tn0.Nodes.Add(tn3);
TreeNode tn1 = null;
for (int r = 0; r < dt.Rows.Count; r++ )
{
string name, pid;
pid = dt.Rows[r]["menu_id"].ToString();
name = dt.Rows[r]["menu_name"].ToString();
if (pid.Substring(1) == "000") {
tn1 = new TreeNode(name);
tn1.ImageIndex = 1;
tn1.SelectedImageIndex = 2;
tn0.Nodes.Add(tn1);
}
else{
TreeNode tn2 = new TreeNode(name);
tn2.Tag = pid;
tn2.ImageIndex = 3;
tn2.SelectedImageIndex = 4;
tn1.Nodes.Add(tn2);
}
}
treeView1.ExpandAll();
}
private void treeView1_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e)
{
string formName = ""; //要打開的窗體:命名空間.類名
TreeNode tn = e.Node;
string tag = (string)tn.Tag;
if (string.IsNullOrEmpty(tag)) return;
//if (tag == "1000") formName = "JIC";
//else if (tag == "2000") formName = "YEW";
//else if (tag == "9000") formName = "XIT";
//else return;
Assembly assembly = Assembly.GetExecutingAssembly();
// 實(shí)例化窗體
//formName = "AppUI.SDI." + formName;
formName = dt.Select("menu_id = '" + tag + "'")[0]["s_form"].ToString();
Form form = assembly.CreateInstance(formName) as Form;
string ftext = dt.Select("menu_id = '" + tag + "'")[0]["menu_name"].ToString();
form.Text = tag + ftext;
ShowForm(form);
}
private void ShowForm(Form sdi) //在pane2里打開窗體
{
//this.splitContainer1.Panel2.Controls.Clear();
//sdi.FormBorderStyle = FormBorderStyle.FixedDialog;
sdi.FormBorderStyle = FormBorderStyle.None;
sdi.FormBorderStyle = FormBorderStyle.SizableToolWindow;
sdi.Dock = DockStyle.Fill;
sdi.TopLevel = false;
//sdi.Parent = this.splitContainer1.Panel2;
//this.splitContainer1.Panel2.Controls.Add(sdi);
sdi.BringToFront();
sdi.Show();
}
private void MDIForm_FormClosed(object sender, FormClosedEventArgs e)
{
Application.Exit();
}
long ll = 0;
private void newMenuItem_Click(object sender, EventArgs e)
{
Form f = new Form();
ll = ll + 11;
f.Name = ll.ToString();
f.Text = f.Name;
f.MdiParent = this;
f.Show();
}
private void treeMenuItem_Click(object sender, EventArgs e)
{
if (treeView1.Visible)
{
treeView1.Visible = false;
treeMenuItem.Checked = false;
}
else {
treeView1.Visible = true;
treeMenuItem.Checked = true;
}
}
private void exitMenuItem_Click(object sender, EventArgs e)
{
Application.Exit();
}
}
}