Vs2005的win form动态生成菜单 (转)
2007-05-08 11:08
399 查看
这几天想用vs2005做点东西。要用到windows form 的MainMenu控件。于是想做成动态生成并且动态梆定事件,在网上找了一下没有发现什么好的方法。于是自己来研究一下。以前是做web form的东西,对windows form还真有点陌生的感觉。还好以前用Delphi也做过类似人功能有一点印象。就是用递归去创建菜单。好了不多说了,下面看代码。
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
private void frmMain_Load(object sender, EventArgs e)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
CreateMenu();
![](http://images.csdn.net/syntaxhighlighti<br/>4000<br/>ng/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
/**////
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
/// 动态创建菜单
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
///
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
private void CreateMenu()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//定义一个主菜单
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
MenuStrip mainMenu = new MenuStrip();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
DataSet ds = new DataSet();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//从XML中读取数据。数据结构后面详细讲一下。
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
ds.ReadXml(@"....Menu.xml");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
DataView dv = ds.Tables[0].DefaultView;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//通过DataView来过滤数据首先得到最顶层的菜单
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
dv.RowFilter = "ParentItemID=0";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
for(int i=0; i<dv.Count;i++)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//创建一个菜单项
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
ToolStripMenuItem topMenu = new ToolStripMenuItem();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//给菜单赋Text值。也就是在界面上看到的值。
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
topMenu.Text = dv[i]["Text"].ToString();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//如果是有下级菜单则通过CreateSubMenu方法来创建下级菜单
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if (Convert.ToInt16(dv[i]["IsModule"]) == 1)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//以ref的方式将顶层菜单传递参数,因为他可以在赋值后再回传。--也许还有更好的方法^_^.
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
CreateSubMenu(ref topMenu, Convert.ToInt32(dv[i]["ItemID"]),ds.Tables[0]);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//显示应用程序中已打开的 MDI 子窗体列表的菜单项
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
mainMenu.MdiWindowListItem = topMenu;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//将递归附加好的菜单加到菜单根项上。
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
mainMenu.Items.Add(topMenu);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
mainMenu.Dock = DockStyle.Top;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//将窗体的MainMenuStrip梆定为mainMenu.
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.MainMenuStrip = mainMenu;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//这句很重要。如果不写这句菜单将不会出现在主窗体中。
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.Controls.Add(mainMenu);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
/**//// 创建子菜单
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
///
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
/// 父菜单项
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
/// 父菜单的ID
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
/// 所有菜单数据集
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
private void CreateSubMenu(ref ToolStripMenuItem topMenu,int ItemID, DataTable dt)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
DataView dv = new DataView(dt);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//过滤出当前父菜单下在所有子菜单数据(仅为下一层的)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
dv.RowFilter = "ParentItemID=" + ItemID.ToString();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
for (int i = 0; i < dv.Count; i++)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//创建子菜单项
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
ToolStripMenuItem subMenu = new ToolStripMenuItem();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
subMenu.Text = dv[i]["Text"].ToString();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//如果还有子菜单则继续递归加载。
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if (Convert.ToInt16(dv[i]["IsModule"]) == 1)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//递归调用
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
CreateSubMenu(ref subMenu, Convert.ToInt32(dv[i]["ItemID"]), dt);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
else
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//扩展属性可以加任何想要的值。这里用formName属性来加载窗体。
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
subMenu.Tag = dv[i]["FormName"].ToString();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//给没有子菜单的菜单项加事件。
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
subMenu.Click += new EventHandler(subMenu_Click);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if (dv[i]["ImageName"].ToString().Length > 0)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//设置菜单项前面的图票为16X16的图片文件。
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Image img = Image.FromFile(@"....Image"+dv[i]["ImageName"].ToString());
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
subMenu.Image = img;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
subMenu.Image.Tag = dv[i]["ImageName"].ToString();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//将菜单加到顶层菜单下。
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
topMenu.DropDownItems.Add(subMenu);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
/**////
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
/// 菜单单击事件
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
///
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
///
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
///
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
void subMenu_Click(object sender, EventArgs e)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//tag属性在这里有用到。
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
string formName = ((ToolStripMenuItem)sender).Tag.ToString();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
CreateFormInstance(formName);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
/**////
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
/// 创建form实例。
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
///
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
/// form的类名
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
private void CreateFormInstance(string formName)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
bool flag = false;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//遍历主窗口上的所有子菜单
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
for(int i=0;i<this.MdiChildren.Length; i++)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//如果所点的窗口被打开则重新激活
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if (this.MdiChildren[i].Tag.ToString().ToLower() == formName.ToLower())
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.MdiChildren[i].Activate();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.MdiChildren[i].Show();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.MdiChildren[i].WindowState = FormWindowState.Normal;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
flag = true;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
break;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if (!flag)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//如果不存在则用反射创建form窗体实例。
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Assembly asm = Assembly.Load("Fastyou.BookShop.Win");//程序集名
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
object frmObj = asm.CreateInstance("Fastyou.BookShop.Win." + formName);//程序集+form的类名。
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Form frms = (Form)frmObj;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//tag属性要重新写一次,否则在第二次的时候取不到。原因还不清楚。有知道的望告知。
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
frms.Tag = formName.ToString();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
frms.MdiParent = this;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
frms.Show();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
下面来说明一下XML文件。
Text:为显示在窗体上的文字,
ItemID:为菜单的ID,
ParentItemID:为此菜单的父菜单ID如果是顶级菜单则为0
FormName:为单击菜单所要显示的窗体类名如果是还有下级菜单的则不要写。
IsModule:是否还有子菜单1有,0没有
Level:菜单所在的层数。目前还没用到。
ImageName:菜单前的图标为16x16的图片文件。
在这里还可以根据自己的需要加其它的属性,也可以写成以数据库的方式。
这是我目前用到的功能。我会继续发表我做这个小软件的心得体会。希望对大家有所帮助。
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
private void frmMain_Load(object sender, EventArgs e)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
CreateMenu();
![](http://images.csdn.net/syntaxhighlighti<br/>4000<br/>ng/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
/**////
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
/// 动态创建菜单
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
///
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
private void CreateMenu()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//定义一个主菜单
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
MenuStrip mainMenu = new MenuStrip();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
DataSet ds = new DataSet();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//从XML中读取数据。数据结构后面详细讲一下。
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
ds.ReadXml(@"....Menu.xml");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
DataView dv = ds.Tables[0].DefaultView;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//通过DataView来过滤数据首先得到最顶层的菜单
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
dv.RowFilter = "ParentItemID=0";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
for(int i=0; i<dv.Count;i++)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//创建一个菜单项
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
ToolStripMenuItem topMenu = new ToolStripMenuItem();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//给菜单赋Text值。也就是在界面上看到的值。
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
topMenu.Text = dv[i]["Text"].ToString();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//如果是有下级菜单则通过CreateSubMenu方法来创建下级菜单
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if (Convert.ToInt16(dv[i]["IsModule"]) == 1)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//以ref的方式将顶层菜单传递参数,因为他可以在赋值后再回传。--也许还有更好的方法^_^.
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
CreateSubMenu(ref topMenu, Convert.ToInt32(dv[i]["ItemID"]),ds.Tables[0]);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//显示应用程序中已打开的 MDI 子窗体列表的菜单项
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
mainMenu.MdiWindowListItem = topMenu;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//将递归附加好的菜单加到菜单根项上。
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
mainMenu.Items.Add(topMenu);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
mainMenu.Dock = DockStyle.Top;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//将窗体的MainMenuStrip梆定为mainMenu.
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.MainMenuStrip = mainMenu;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//这句很重要。如果不写这句菜单将不会出现在主窗体中。
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.Controls.Add(mainMenu);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
/**//// 创建子菜单
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
///
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
/// 父菜单项
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
/// 父菜单的ID
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
/// 所有菜单数据集
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
private void CreateSubMenu(ref ToolStripMenuItem topMenu,int ItemID, DataTable dt)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
DataView dv = new DataView(dt);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//过滤出当前父菜单下在所有子菜单数据(仅为下一层的)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
dv.RowFilter = "ParentItemID=" + ItemID.ToString();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
for (int i = 0; i < dv.Count; i++)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//创建子菜单项
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
ToolStripMenuItem subMenu = new ToolStripMenuItem();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
subMenu.Text = dv[i]["Text"].ToString();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//如果还有子菜单则继续递归加载。
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if (Convert.ToInt16(dv[i]["IsModule"]) == 1)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//递归调用
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
CreateSubMenu(ref subMenu, Convert.ToInt32(dv[i]["ItemID"]), dt);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
else
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//扩展属性可以加任何想要的值。这里用formName属性来加载窗体。
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
subMenu.Tag = dv[i]["FormName"].ToString();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//给没有子菜单的菜单项加事件。
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
subMenu.Click += new EventHandler(subMenu_Click);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if (dv[i]["ImageName"].ToString().Length > 0)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//设置菜单项前面的图票为16X16的图片文件。
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Image img = Image.FromFile(@"....Image"+dv[i]["ImageName"].ToString());
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
subMenu.Image = img;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
subMenu.Image.Tag = dv[i]["ImageName"].ToString();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//将菜单加到顶层菜单下。
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
topMenu.DropDownItems.Add(subMenu);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
/**////
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
/// 菜单单击事件
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
///
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
///
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
///
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
void subMenu_Click(object sender, EventArgs e)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//tag属性在这里有用到。
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
string formName = ((ToolStripMenuItem)sender).Tag.ToString();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
CreateFormInstance(formName);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
/**////
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
/// 创建form实例。
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
///
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
/// form的类名
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
private void CreateFormInstance(string formName)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
bool flag = false;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//遍历主窗口上的所有子菜单
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
for(int i=0;i<this.MdiChildren.Length; i++)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//如果所点的窗口被打开则重新激活
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if (this.MdiChildren[i].Tag.ToString().ToLower() == formName.ToLower())
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.MdiChildren[i].Activate();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.MdiChildren[i].Show();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.MdiChildren[i].WindowState = FormWindowState.Normal;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
flag = true;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
break;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if (!flag)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//如果不存在则用反射创建form窗体实例。
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Assembly asm = Assembly.Load("Fastyou.BookShop.Win");//程序集名
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
object frmObj = asm.CreateInstance("Fastyou.BookShop.Win." + formName);//程序集+form的类名。
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Form frms = (Form)frmObj;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//tag属性要重新写一次,否则在第二次的时候取不到。原因还不清楚。有知道的望告知。
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
frms.Tag = formName.ToString();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
frms.MdiParent = this;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
frms.Show();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
下面来说明一下XML文件。
Text:为显示在窗体上的文字,
ItemID:为菜单的ID,
ParentItemID:为此菜单的父菜单ID如果是顶级菜单则为0
FormName:为单击菜单所要显示的窗体类名如果是还有下级菜单的则不要写。
IsModule:是否还有子菜单1有,0没有
Level:菜单所在的层数。目前还没用到。
ImageName:菜单前的图标为16x16的图片文件。
在这里还可以根据自己的需要加其它的属性,也可以写成以数据库的方式。
这是我目前用到的功能。我会继续发表我做这个小软件的心得体会。希望对大家有所帮助。
相关文章推荐
- Vs2005的win form动态生成菜单
- Vs2005的win form动态生成菜单
- Vs2005的win form动态生成菜单
- Vs2005的win form动态生成菜单
- VS2005利用TreeView控件和递归函数从数据库动态生成树状菜单
- VS2005中动态生成树状菜单
- VS2005利用TreeView控件和递归函数动态生成树状菜单
- jquery easyui Accordion 动态生成菜单
- Ext.menu.Menu动态生成的菜单
- VS2005 GridView动态绑定数据源(交叉表) 动态绑定列 动态生成模板列 自定义表头
- Flash+XML生成动态导航菜单代码
- 一个动态生成菜单的JS.保存以备后用.
- JSF动态生成菜单
- C#动态生成子菜单(以IE收藏夹菜单为例)
- c#使用Dataset读取XML文件动态生成菜单的方法
- 动态生成右键菜单子菜单
- 【转载】【权限控制】角色访问动态生成用户权限菜单树(转载: http://www.comprg.com.cn/post_show.asp?id=7873 )
- [留着备用]ASP.NET动态菜单生成通用方法
- 移动项目开发笔记(动态生成xml文件生成导航菜单(续))
- Android 实现动态生成菜单