江苏省委第六巡视组:WinForm实现可折叠的面板菜单

来源:百度文库 编辑:偶看新闻 时间:2024/04/29 05:33:02

WinForm实现可折叠的面板菜单

WinForm 2010-07-21 19:35:52 阅读112 评论0   字号: 订阅

        今天用WinForm做了一个可折叠的面板导航菜单,虽然没有VS工具箱的效果好,但是还是蛮不错的,主要用的知识点还是控件的停靠(Anchor),锚定(Dock)等。折叠面板都是动态构造的,非常便于修改或扩展。
效果图:

代码实现:
public partial class FMMain : Form, IFMMain
    {

        private List sysList = Globals.CurrentUser.Applications; //当前用户拥有的系统模块
        public FMMain()
        {
            InitializeComponent();
        }

        //设置控件的停靠属性,默认第一个面板展开
        private void SetSplitContainerDock(int index)
        { 
            foreach (Control control in this.splitContainer1.Panel1.Controls)
            {
                if (control is Button)
                {
                    Button btn = (Button)control;
                    if (btn.TabIndex > index)
                    {
                        if (btn.Dock != DockStyle.Bottom)
                        {
                            btn.Dock = DockStyle.Bottom;
                            btn.BringToFront();  //把该控件置于顶层
                        }
                    }
                    else
                    {
                        if (btn.Dock != DockStyle.Top)
                        {
                            btn.Dock = DockStyle.Top;
                            btn.BringToFront();  //把该控件置于顶层
                        }
                    }
                }
            }
        }

        //根据模块编号获取该模块下的数据
        private void GetModulesOfApplication(string appId)
        {
            DataSet ds;
            using (ChannelFactory channelFactory = new ChannelFactory("SysUsers"))
            {
                ISys_UserService proxy = channelFactory.CreateChannel();
                //应用程序编号应该作为参数传入
                ds = proxy.ModulesOfUserInApplication(Globals.CurrentUser.UserID, appId);
            }
            tvModules.Nodes.Clear();
            DataView dv = ds.Tables[0].DefaultView;
            dv.RowFilter = "ParentId='root'";
            dv.Sort = "Prority DESC";
            foreach (DataRowView row in dv)
            {
                var tnode = new TreeNode(row["ModuleName"].ToString());
                tnode.Tag = row;
                BindTree(dv, tnode);  //绑定树
                tvModules.Nodes.Add(tnode);
            }
        }

 
        //窗体加载时构造折叠面板
        private void FMMain_Load(object sender, EventArgs e)
        {
            int width = this.splitContainer1.Panel1.Width;
            int index = 0;
            int tabIndex = 0;
            string applicationId = "01";
            if (sysList.Count > 0)
            {
                Button[] btnCollections = new Button[sysList.Count];
                foreach (Sys_Applications app in sysList)
                {
                    Button btn = new Button
                    {
                        Tag = app.ApplicationId,
                        Text = app.ApplicationName,
                        Width = width-2,
                        Height = 23,
                        Dock = DockStyle.Top,
                        BackColor = SystemColors.Control,
                        UseVisualStyleBackColor = false,      //不使用XP主题样式
                        TextAlign = ContentAlignment.MiddleLeft,
                        TabIndex = index + 1,
                    };
                    btn.Click += (s, ex) =>           //绑定事件
                    {
                        Button clickedButton = (Button)s;
                        int clickedButtonTabIndex = clickedButton.TabIndex;
                        //设置按钮的停靠顺序

                        SetSplitContainerDock(clickedButtonTabIndex);
                        //加载系统菜单
                        GetModulesOfApplication(Globals.CurrentUser.UserID, clickedButton.Tag.ToString());
                        tvModules.BringToFront();
                    };
                    if (index == 0) { tabIndex = 1; applicationId = btn.Tag.ToString(); }
                    btnCollections[index++] = btn;
                }
                Array.Reverse(btnCollections); //反转数组, 改变控件的Z顺序 ,此句将直接影响下面添加的顺序
                this.splitContainer1.Panel1.Controls.AddRange(btnCollections);
                this.SetSplitContainerDock(tabIndex);
                this.GetModulesOfApplication(Globals.CurrentUser.UserID, applicationId); 
                this.splitContainer1.Panel1.Padding = new Padding { Left = 0, Right = 0 };  //设置折叠面板和外边缘的间隔像素
            }
      }

 }  评论这张 转发至微博