C#实现的简单抽题菜单using System;
using System.Collections.Generic;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Xml.XPath;
using System.Windows.Forms;
namespace WindowsApplication1
{
public partial class ToolBox : Form
{
ArrayList nameArr = new ArrayList();
//记录每个列表里工具项的信息
Hashtable lstViewHash = new Hashtable();
public Hashtable panelHash = new Hashtable();
public Hashtable btnHash = new Hashtable();
Hashtable name_Index = new Hashtable();
public Panel ButtonPanel
{
get { return this.pnl_Button; }
}
string path = Application.StartupPath "\\..\\..\\ImageFile\\ToolImage\\ToolBox.xml";
public ToolBox()
{
InitializeComponent();
nameArr.Add("点线工具");
nameArr.Add("标绘图标");
nameArr.Add("文字编辑");
nameArr.Add("颜色调配");
}
private void ToolBox_Load(object sender, EventArgs e)
{
for (int i = 1; i < nameArr.Count 1; i )
{
//自定义的button按钮,封装了按钮组在不同状态下点击的行为
CustomButton cusBtn = new CustomButton((string)nameArr[i-1],this,i);
//cusBtn.Anchor = (AnchorStyles.Bottom|AnchorStyles .Left |AnchorStyles .Right);
//底层面板:避免dock重写form造成的定位错误
this.pnl_Button.Controls.Add(cusBtn);
//记录新增按钮
if(!this.btnHash .ContainsKey (cusBtn))
{
this.btnHash.Add(cusBtn, i);
this.name_Index.Add((string)nameArr[i - 1], i);
}
}
GetToolInformation(path, nameArr);
panelHash = CreatePanel(lstViewHash);
foreach (DictionaryEntry de in panelHash)
{
//将创建的面板加入到工具箱,通过按钮控制面板的显示与隐藏
Panel tooPan = (Panel)de.Value;
string name = (string)de.Key;
int index = (int)this.name_Index[name];
int number=this.name_Index .Count ;
int height = this.pnl_Button.ClientSize.Height - number * 23;
tooPan.Size = new Size(147, height);
int location_y = index * 23;
tooPan.Location = new Point(2, location_y);
tooPan.Anchor = (AnchorStyles.Top | AnchorStyles.Bottom|AnchorStyles .Left);
if (index < name_Index.Count)
{
tooPan.Visible = false;
}
this.pnl_Button .Controls.Add(tooPan);
}
}
#region 构造相应的控制面板
/// <summary>
/// 通过配置文件获取相应的工具项
/// </summary>
/// <param name="path"></param>
/// <param name="nameArr"></param>
private void GetToolInformation(string path,ArrayList nameArr)
{
Hashtable toolHash = null;
XPathDocument doc = new XPathDocument(path);
XPathNavigator nav = doc.CreateNavigator();
for (int i = 0; i < nameArr.Count; i )
{
string name = (string)nameArr[i];
XPathNodeIterator iter= nav.Select("/ToolBox/ToolBar[@name='" name "']");
iter .MoveNext ();
//XPathNodeIterator iter = nav.Select("/ToolBox/ToolBar[@name='" name "']");
toolHash = new Hashtable();
nav = iter.Current;
XPathNodeIterator iter_Down = nav.SelectChildren(XPathNodeType.Element);
while (iter_Down.MoveNext())
{
if (iter_Down.Current.Name == "Tool")
{
ToolData data = new ToolData();
XPathNavigator nav2 = iter_Down.Current;
XPathNodeIterator iter_Down2 = nav2.SelectChildren(XPathNodeType.Element);
while (iter_Down2.MoveNext())
{
switch (iter_Down2.Current.Name)
{
case "Name":
data.name = iter_Down2.Current.Value;
break;
case "Image":
data.image = Application.StartupPath "\\..\\.." iter_Down2.Current.Value;
break;
case "InfoClass":
data.infoClass = iter_Down2.Current.Value;
toolHash.Add(data.name, data);
break;
}
}
}
}
if (toolHash.Count != 0)
{
lstViewHash.Add(name, toolHash);
}
}
}
/// <summary>
/// 根据已知的记录创建列表
/// </summary>
/// <param name="toolInformation">列表工具信息</param>
/// <returns></returns>
private ListView CreateListView(Hashtable toolInformation)
{
//关键是各个控件工具区域的限定
ListView lst = new ListView();
ImageList imagelst = new ImageList();
imagelst.ImageSize = new Size(25, 25);
lst.View = View.Tile;
lst.TileSize = new Size(300, 45);
lst.Scrollable = false;
foreach (DictionaryEntry de in toolInformation)
{
ListViewItem item = new ListViewItem((string)de.Key);
imagelst.Images.Add(Image.FromFile((de.Value as ToolData).image));
item.ImageIndex = imagelst.Images.Count - 1;
lst.Items.Add(item);
}
lst.LargeImageList = imagelst;
return lst;
}
/// <summary>
/// 构造与按钮对应的面板,目前面板仅包含一个listview
/// </summary>
/// <param name="lstViewHash">工具列表</param>
/// <returns>包含工具列表的面板</returns>
private Hashtable CreatePanel(Hashtable lstViewHash)
{
Hashtable panHash = new Hashtable();
foreach (DictionaryEntry de in lstViewHash)
{
Panel panel = new Panel();
ListView lst = null;
Hashtable lstTool = (Hashtable)de.Value;
string name = (string)de.Key;
if(lstTool.Count != 0)
{
lst = CreateListView(lstTool);
panel.Controls.Add(lst);
lst.Dock = DockStyle.Fill;
panHash.Add(de.Key, panel);
}
}
return panHash;
}
#endregion
}
}
评论