c#动态生成菜单
1 /// <summary> 2 /// 主窗体 3 /// </summary> 4 public partial class Frm_Main : BaseForm 5 { 6 /// <summary> 7 /// 构造函数 8 /// </summary> 9 /// <param name="mzDoctorUserInfo"></param> 10 /// 11 public Frm_Main() 12 { 13 InitializeComponent(); 14 15 SetFormMax(this); 16 17 InitFormStatus(this);
19 this.menuStrMain.Renderer = new MyMenuRender(); 20 } 21 22 /// <summary> 23 /// 窗体加载函数 24 /// </summary> 25 /// <param name="sender"></param> 26 /// <param name="e"></param> 27 private void Frm_Main_Load(object sender, EventArgs e) 28 { 29 GetMenuString(); 30 31 this.Icon = Icon.FromHandle(new Bitmap(imageList1.Images[0]).GetHicon()); 34 } 35 36 /// <summary> 37 /// 获取窗体的菜单 38 /// </summary> 39 private void GetMenuString() 40 { 41 string errmsg = ""; 42 System.Data.DataSet ds = new DataSet(); 43 int tt = WebService.GetInstance(CommonInfo.LoginInfo.userParameters).Execute("DataServices", "115001001", "1", "<Code>111</Code>", out errmsg, out ds); 44 if (tt == 1) 45 { 46 InitMenu(ds.Tables[0], menuStrMain, MenuClicked); 47 } 48 } 49 50 /// <summary> 51 /// 菜单单击 52 /// </summary> 53 /// <param name="sender"></param> 54 /// <param name="e"></param> 55 public void MenuClicked(object sender, EventArgs e) 56 { 57 string parameters = string.Empty; 58 string winName = string.Empty; 59 string id = string.Empty; 60 try 61 { 62 id = ((ToolStripMenuItem)sender).Tag.ToString(); 63 64 DataRow[] drs = MenuData.Select("MenuID = '" + id + "'"); 65 66 if (drs.Length == 1) 67 { 68 winName = drs[0]["Stmp1"].ToString(); 69 parameters = drs[0]["Stmp2"].ToString().Trim(); 70 } 71 72 73 Type formType = Type.GetType(winName); 74 75 if (formType != null) 76 { 77 if (typeof(Form).IsAssignableFrom(formType)) 78 { 79 CloseOtherAllFrom(); 80 81 Form f = null; 82 83 if (parameters == null || parameters == string.Empty) 84 { 85 f = (Form)Activator.CreateInstance(formType); 86 } 87 else 88 { 89 f = (Form)Activator.CreateInstance(formType, new object[1] { parameters }); 90 } 91 f.ShowDialog(); 92 } 93 } 94 } 95 catch (Exception ee) 96 { 97 MessageBox.Show(ee.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); 98 } 99 } 100 101 /// <summary> 102 /// 关闭窗体 103 /// </summary> 104 public void CloseWindows() 105 { 106 this.Close(); 107 } 108 }
1 #region 菜单 2 3 /// <summary> 4 /// 关闭其他所有窗口 5 /// </summary> 6 protected void CloseOtherAllFrom() 7 { 8 foreach (Form frm in this.MdiChildren) 9 { 10 frm.Close(); 11 } 12 } 13 14 /// <summary> 15 /// 初始化菜单 16 /// </summary> 17 protected void InitMenu(DataTable dt, MenuStrip menuStrip, EventHandler callback) 18 { 19 this.MenuData = dt; 20 21 //循环生成父菜单 22 for (int i = 0; i < dt.Rows.Count; i++) 23 { 24 string id = MenuData.Rows[i]["MenuID"].ToString().Trim(); 25 string text = MenuData.Rows[i]["MenuName"].ToString().Trim(); 26 27 ToolStripMenuItem mi = new ToolStripMenuItem(text); 28 mi.Tag = id; 29 30 if (id.Length == 6) 31 { 32 if (!ExistDropDownItems(MenuData, id)) 33 { 34 mi.Click += callback; 35 } 36 37 menuStrip.Items.Add(mi); 38 39 //递归算法生成所有级别的下级子菜单 40 AddMenuItem(MenuData, MenuData.Rows[i]["MenuID"].ToString(), mi.DropDownItems, callback); 41 } 42 } 43 } 44 45 /// <summary> 46 /// 创建无级树菜单 47 /// </summary> 48 /// <param name="dt">获取菜单的数据源</param> 49 /// <param name="parentID">菜单的父ID</param> 50 /// <param name="parItem">创建菜单的Item</param> 51 private void AddMenuItem(DataTable dt, string parentID, ToolStripItemCollection parItem, EventHandler callback) 52 { 53 DataRow[] drs = dt.Select("MenuID like '" + parentID + "%' and MenuID <> '" + parentID + "'"); 54 if (drs.Length > 0) 55 { 56 foreach (DataRow dr in drs) 57 { 58 ToolStripMenuItem mi = new ToolStripMenuItem(); 59 mi.Text = dr["MenuName"].ToString(); 60 mi.Tag = dr["MenuID"].ToString(); 61 if (!ExistDropDownItems(dt, mi.Tag.ToString())) 62 { 63 mi.Click += new EventHandler(callback); 64 } 65 parItem.Add(mi); 66 AddMenuItem(dt, drs[0].ToString(), mi.DropDownItems, callback); 67 } 68 } 69 else 70 { 71 return; 72 } 73 } 74 75 /// <summary> 76 /// 指定节点是否存在下级节点 77 /// </summary> 78 /// <param name="dt">数据集</param> 79 /// <param name="parentID">节点编号</param> 80 /// <returns>存在true 不存在 false</returns> 81 private bool ExistDropDownItems(DataTable dt, string parentID) 82 { 83 DataRow[] drs = dt.Select("MenuID like '" + parentID + "%' and MenuID <> '" + parentID + "'"); 84 85 if (drs.Length > 0) 86 { 87 return true; 88 } 89 return false; 90 } 91 92 #endregion
人生,总是有一些空城旧事,年华未央;总是有些季节,一季花凉,满地忧伤。许多事,看开了,便会峰回路转;许多梦,看淡了,便会云开日出。学会思索,学会珍藏,微笑领悟,默默坚强。