最近在寫HR系統用到的第三方的Devexpress套件由於要使用權限機制控制不一樣用戶進入系統顯示菜單因此要配合字典數據動態生成。在WEB中這種問題灰常的輕鬆在winform裏就稍微有點不一樣爲了用DEV實現卡式菜單有組的概念具體如圖紅色部分數據庫
提示:這裏用了菜單頭、菜單組、菜單項來逐級生成 ,首先是菜單頭便利建立而後判斷是否有菜單組若是有菜單組就先生成菜單組而後把菜單項加入菜單組最後爲按鈕註冊事件,這裏的事情用一個公用方法搞定便可express
/// <summary> /// 動態加載菜單 /// </summary> private void GetMenuBind() { //根據登陸用戶角色菜單動態建立 //循環建立卡菜單 DataTable baseModuleDT = BLL.BaseModule.GetDataTable("LevelNo=1", ""); for (int i = 0; i < baseModuleDT.Rows.Count; i++) { //建立卡 RibbonPage ribbonPage = new DevExpress.XtraBars.Ribbon.RibbonPage();//菜單卡對象定義 ribbonPage.Text = baseModuleDT.Rows[i]["FullName"].ToString(); this.ribbon.Pages.Add(ribbonPage); //獲取組菜單 DataTable baseModuleDTG = BLL.BaseModule.GetDataTable("ParentId=" + baseModuleDT.Rows[i]["Id"].ToString() + " and LevelNo=2", ""); if (baseModuleDTG.Rows.Count <= 0) { //沒有組 RibbonPageGroup ribbonPageGroup = new DevExpress.XtraBars.Ribbon.RibbonPageGroup();//菜單卡分組定義 ribbonPageGroup.Text = baseModuleDT.Rows[i]["FullName"].ToString();//卡片組名 ribbonPage.Groups.Add(ribbonPageGroup); DataTable baseModuleDTB = BLL.BaseModule.GetDataTable("ParentId=" + baseModuleDT.Rows[i]["Id"].ToString() + " and LevelNo=3", ""); for (int b = 0; b < baseModuleDTB.Rows.Count; b++) { Image image = Image.FromFile(filePath + baseModuleDTB.Rows[b]["MenuIco"].ToString());//獲取文件 //功能按鈕 BarButtonItem barButtonItem = new DevExpress.XtraBars.BarButtonItem(); barButtonItem.Caption = baseModuleDTB.Rows[b]["FullName"].ToString(); barButtonItem.LargeGlyph = image;// global::MemberManager.Properties.Resources.group_key; barButtonItem.Name = baseModuleDTB.Rows[b]["WinFrom"].ToString(); barButtonItem.Tag = baseModuleDTB.Rows[b]["WinFrom"].ToString().Substring(0, baseModuleDTB.Rows[b]["WinFrom"].ToString().IndexOf('.'));//截取文件名點之前的 ribbonPageGroup.ItemLinks.Add(barButtonItem); barButtonItem.ItemClick += new DevExpress.XtraBars.ItemClickEventHandler(this.barButtonItem_ItemClick);//註冊事件 } } else { for (int g = 0; g < baseModuleDTG.Rows.Count; g++) { //建立組 RibbonPageGroup ribbonPageGroup = new DevExpress.XtraBars.Ribbon.RibbonPageGroup();//菜單卡分組定義 ribbonPageGroup.Text = baseModuleDTG.Rows[g]["FullName"].ToString();//卡片組名 ribbonPage.Groups.Add(ribbonPageGroup); DataTable baseModuleDTB = BLL.BaseModule.GetDataTable("ParentId=" + baseModuleDTG.Rows[g]["Id"].ToString() + " and LevelNo=3", ""); for (int b = 0; b < baseModuleDTB.Rows.Count; b++) { Image image = Image.FromFile(filePath + baseModuleDTB.Rows[b]["MenuIco"].ToString());//獲取文件 //功能按鈕 BarButtonItem barButtonItem = new DevExpress.XtraBars.BarButtonItem(); barButtonItem.Caption = baseModuleDTB.Rows[b]["FullName"].ToString(); barButtonItem.LargeGlyph = image;// global::MemberManager.Properties.Resources.group_key; barButtonItem.Name = baseModuleDTB.Rows[b]["WinFrom"].ToString(); //barButtonItem.Tag = baseModuleDTB.Rows[b]["WinFrom"].ToString().Substring(0, baseModuleDTB.Rows[b]["WinFrom"].ToString().IndexOf('.'));//截取文件名點之前的 ribbonPageGroup.ItemLinks.Add(barButtonItem); barButtonItem.ItemClick += new DevExpress.XtraBars.ItemClickEventHandler(this.barButtonItem_ItemClick);//註冊事件 } } } } }
動態調用事件以下:this
/// <summary> /// 動態調出窗體事件 注:動態調用窗體名需和數據庫中名稱徹底一致 /// </summary> private void barButtonItem_ItemClick(object sender, ItemClickEventArgs e) { if (!ShowChildrenForm(e.Item.Tag.ToString())) { if (this.MdiChildren.Length < childFormMaxNumber) { DictItemUtil.AddLog("成功打開" + e.Item.Caption.ToString()); Assembly asm = Assembly.Load("MemberManager");//程序集名 object frmObj = asm.CreateInstance("MemberManager." + e.Item.Tag.ToString());//程序集+form的類名。 Form childForm = (Form)frmObj; childForm.Tag = e.Item.Tag.ToString(); //tag屬性要從新寫一次,不然在第二次的時候取不到。緣由還不清楚。有知道的望告知。 childForm.MdiParent = this; childForm.Show(); } else MessageUtil.ShowWarning(message); } }