软件的整体主页布局是用到了WeifenLuo.WinFormsUI.Docking;的可拖拉式布局,和我们常用的VS 工具效果一样。这样的好处是菜单页和内容页的窗口在编码的时候可以分离,互不影响。

菜单是抽屉式布局,废话不多说先上代码

  /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FrmLeft_Load(object sender, EventArgs e)
        {
            Init();
        }
        private void Init()
        {
            this.panel1.Controls.Clear();
            this.panel1.Controls.Add(this.listView1);
            this.listView1.Clear();
            //查询菜单的父节点,先把父节点打上去
            m_Parent = bll.GetSysMenusParent(FrmLogin.LoginUserID);
            int i = m_Parent.Count;
            foreach (var item in m_Parent)
            {
                Button b = new Button();
                b.Text = item.Menu_Name;
                b.Dock = DockStyle.Top;
                b.Size = new System.Drawing.Size(244, 30);
                b.Tag = item.Menu_Id;
                b.TabIndex = i;
                b.Click += new System.EventHandler(this.ButtonClick);
                this.panel1.Controls.Add(b);
                i--;
            }
        }
        void ButtonClick(object sender, System.EventArgs e)
        {          
            Button clickedButton = (Button)sender;          
            int clickedButtonTabIndex = clickedButton.TabIndex;        
            foreach (Control ctl in this.panel1.Controls)
            {
                if (ctl is Button)
                {
                    Button btn = (Button)ctl;
                    if (btn.TabIndex > clickedButtonTabIndex)
                    {
                        if (btn.Dock != DockStyle.Bottom)
                        {
                            btn.Dock = DockStyle.Bottom;

                            btn.BringToFront();
                        }
                    }
                    else
                    {
                        if (btn.Dock != DockStyle.Top)
                        {
                            btn.Dock = DockStyle.Top;
                            btn.BringToFront();
                        }
                    }
                }
            }
            foreach (var item in m_Parent)
            {
                 if (clickedButton.Tag.ToString() == item.Menu_Id)
                 {
                     this.listView1.Clear();
       
                    //通过父节点去查找子节点绘制
                     List<Base_SysMenu> Childs = bll.GetSysMenuChilds(clickedButton.Tag.ToString(),FrmLogin.LoginUserID);
                     listView1.LargeImageList = new FrmListImages().imageList1;
                     this.listView1.BeginUpdate();

                     foreach (var itemChilds in Childs)
                     {
                         listView1.Items.Add(new ListViewItem 
                         { 
                             Text = itemChilds.Menu_Name,
                             Tag = itemChilds.Menu_Tag,
                             Name=itemChilds.Menu_Id,
                             ImageIndex = itemChilds.Menu_Img == null ? 0 : itemChilds.Menu_Img.Value 
                         });

                     }

                     this.listView1.EndUpdate();
                     break;
                 }
            }
            listView1.BringToFront();  
           
        }

以上代码执行的效果就是这样的,需要我们点击具体的菜单,才能显示子菜单。

 

 

 

点击子菜单利用了反射,会把具体的内容页绘制到右侧。

实现代码


        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                if (this.listView1.SelectedIndices.Count > 0)
                {
                    //反射动态实例化窗口
                    object FrmWindow = this.listView1.SelectedItems[0].Tag;
                    object MenuID = this.listView1.SelectedItems[0].Name;
                    if (CheckFormIsOpen(FrmWindow.ToString()))
                    {
                        //DockContent doc = (DockContent)Assembly.Load("YX").CreateInstance("YX." + FrmWindow);
                        //doc.Show(_frmMain.dockPanel1, DockState.Document);

                        Assembly assembly = Assembly.GetExecutingAssembly(); // 获取当前程序集                   
                        object[] parameters = new object[1];
                        parameters[0] = MenuID;
                        DockContent obj = (DockContent)assembly.CreateInstance("YX." + FrmWindow, true, System.Reflection.BindingFlags.Default, null, parameters, null, null);// 创建类的实例 
                        obj.Show(_frmMain.dockPanel1, DockState.Document);
                    }
                }
            }
            catch (Exception ex)
            {

                MessageBox.Show("还没有配置");
            }

        }

 

Logo

快速构建 Web 应用程序

更多推荐