浅析WINFORM工具条的重用实现

2020-12-13 04:01

阅读:393

标签:winform   style   blog   http   color   数据   

摘要: 一直以来,我都想看看别人家的工具栏重用(图1)到底是如何实现的,但在网上搜索了很久都没有找到过,即使找到一些程序,要么就是把这个工具栏写在具体的画面(图2),要么就是没有源代码的, 我在想,是否别人也有这个需求呢? 于是我决定把我自己的程序代码帖出来,一来可以分享给到需要的朋友,二来,希望有大侠经过,指点一二.

soscw.com,搜素材(图1)

 

soscw.com,搜素材

(图2).

对于图2, 当然好实现了, 只是对当前画面的功能实现. 相当于单独画面的一个按钮而已.

现在,我们主要讲一下图1的实现方法.

在首先,在讲工具栏之前,讲一下菜单栏的实现(图3),在设计的时候,我们的菜单都是没有权限的. 通过在数据库里设定权限做了控制.

soscw.com,搜素材

(图3)

首先在frmMain_Load的时候,加载下面的代码,这里主要是调用递归函数.

            MenuStrip ms = (MenuStrip)this.Controls["menuStrip1"];
            ArrayList arr = new ArrayList();
            dsright = C_BaseInfo.UserRight(frmLogin.C_UserInfo);
            GetMenuAllName(arr, null, 0, ms);//调用递归函数

有一些每个用户都需要的菜单就直接置true了,如更改密码,报表工具等.(具体的报表权限还有地方控制了的.)

        private void GetMenuAllName(ArrayList arr, ToolStripMenuItem tsmi, int level, MenuStrip ms)
        {
            if (level == 0)//一级菜单
            {
                for (int i = 0; i  0)
                {
                    for (int i = 0; i  0)
                                {
                                    for (int j = 0; j 

首先看一下, 工具栏按钮的定义.

 // 
            // toolStripMenuItem32
            // 
            this.toolStripMenuItem32.Image = ((System.Drawing.Image)(resources.GetObject("toolStripMenuItem32.Image")));
            this.toolStripMenuItem32.Name = "toolStripMenuItem32";
            this.toolStripMenuItem32.ShortcutKeys = System.Windows.Forms.Keys.F9;
            this.toolStripMenuItem32.Size = new System.Drawing.Size(211, 22);
            this.toolStripMenuItem32.Text = "查找";
            this.toolStripMenuItem32.Click += new System.EventHandler(this.toolStripMenuItem32_Click);
            // 
            // toolStripMenuItem33
            // 
            this.toolStripMenuItem33.Name = "toolStripMenuItem33";
            this.toolStripMenuItem33.ShortcutKeys = System.Windows.Forms.Keys.F5;
            this.toolStripMenuItem33.Size = new System.Drawing.Size(211, 22);
            this.toolStripMenuItem33.Text = "首笔";
            this.toolStripMenuItem33.Click += new System.EventHandler(this.toolStripMenuItem33_Click);
            // 
            // toolStripMenuItem34
            // 
            this.toolStripMenuItem34.Name = "toolStripMenuItem34";
            this.toolStripMenuItem34.ShortcutKeys = System.Windows.Forms.Keys.F6;
            this.toolStripMenuItem34.Size = new System.Drawing.Size(211, 22);
            this.toolStripMenuItem34.Text = "上一笔";
            this.toolStripMenuItem34.Click += new System.EventHandler(this.toolStripMenuItem34_Click);
            // 
            // toolStripMenuItem35
            // 
            this.toolStripMenuItem35.Name = "toolStripMenuItem35";
            this.toolStripMenuItem35.ShortcutKeys = System.Windows.Forms.Keys.F7;
            this.toolStripMenuItem35.Size = new System.Drawing.Size(211, 22);
            this.toolStripMenuItem35.Text = "下一笔";
            this.toolStripMenuItem35.Click += new System.EventHandler(this.toolStripMenuItem35_Click);
            // 
            // toolStripMenuItem36
            // 
            this.toolStripMenuItem36.Name = "toolStripMenuItem36";
            this.toolStripMenuItem36.ShortcutKeys = System.Windows.Forms.Keys.F8;
            this.toolStripMenuItem36.Size = new System.Drawing.Size(211, 22);
            this.toolStripMenuItem36.Text = "末笔";
            this.toolStripMenuItem36.Click += new System.EventHandler(this.toolStripMenuItem36_Click);
            // 
            // toolStripMenuItem37
            // 
            this.toolStripMenuItem37.Name = "toolStripMenuItem37";
            this.toolStripMenuItem37.ShortcutKeys = System.Windows.Forms.Keys.F11;
            this.toolStripMenuItem37.Size = new System.Drawing.Size(211, 22);
            this.toolStripMenuItem37.Text = "预备查询";
            this.toolStripMenuItem37.Click += new System.EventHandler(this.toolStripMenuItem37_Click);
            // 
            // toolStripMenuItem38
            // 
            this.toolStripMenuItem38.Name = "toolStripMenuItem38";
            this.toolStripMenuItem38.ShortcutKeys = System.Windows.Forms.Keys.F12;
            this.toolStripMenuItem38.Size = new System.Drawing.Size(211, 22);
            this.toolStripMenuItem38.Text = "新增记录";
            this.toolStripMenuItem38.Click += new System.EventHandler(this.toolStripMenuItem38_Click);
            // 
            // toolStripMenuItem39
            // 
            this.toolStripMenuItem39.Image = ((System.Drawing.Image)(resources.GetObject("toolStripMenuItem39.Image")));
            this.toolStripMenuItem39.Name = "toolStripMenuItem39";
            this.toolStripMenuItem39.ShortcutKeys = System.Windows.Forms.Keys.F2;
            this.toolStripMenuItem39.Size = new System.Drawing.Size(211, 22);
            this.toolStripMenuItem39.Text = "保存";
            this.toolStripMenuItem39.Click += new System.EventHandler(this.toolStripMenuItem39_Click);
            // 
            // toolStripMenuItem40
            // 
            this.toolStripMenuItem40.Image = ((System.Drawing.Image)(resources.GetObject("toolStripMenuItem40.Image")));
            this.toolStripMenuItem40.Name = "toolStripMenuItem40";
            this.toolStripMenuItem40.ShortcutKeys = ((System.Windows.Forms.Keys)((System.Windows.Forms.Keys.Control | System.Windows.Forms.Keys.O)));
            this.toolStripMenuItem40.Size = new System.Drawing.Size(211, 22);
            this.toolStripMenuItem40.Text = "复制";
            this.toolStripMenuItem40.Click += new System.EventHandler(this.toolStripMenuItem40_Click);
            // 
            // toolStripMenuItem41
            // 
            this.toolStripMenuItem41.Image = ((System.Drawing.Image)(resources.GetObject("toolStripMenuItem41.Image")));
            this.toolStripMenuItem41.Name = "toolStripMenuItem41";
            this.toolStripMenuItem41.ShortcutKeys = System.Windows.Forms.Keys.F4;
            this.toolStripMenuItem41.Size = new System.Drawing.Size(211, 22);
            this.toolStripMenuItem41.Text = "删除";
            this.toolStripMenuItem41.Click += new System.EventHandler(this.toolStripMenuItem41_Click);
            // 
            // toolStripMenuItem42
            // 
            this.toolStripMenuItem42.Image = ((System.Drawing.Image)(resources.GetObject("toolStripMenuItem42.Image")));
            this.toolStripMenuItem42.Name = "toolStripMenuItem42";
            this.toolStripMenuItem42.ShortcutKeys = ((System.Windows.Forms.Keys)((System.Windows.Forms.Keys.Control | System.Windows.Forms.Keys.P)));
            this.toolStripMenuItem42.Size = new System.Drawing.Size(211, 22);
            this.toolStripMenuItem42.Text = "打印";
            this.toolStripMenuItem42.Click += new System.EventHandler(this.toolStripMenuItem42_Click);
            // 
            // toolStripMenuItem43
            // 
            this.toolStripMenuItem43.Image = ((System.Drawing.Image)(resources.GetObject("toolStripMenuItem43.Image")));
            this.toolStripMenuItem43.Name = "toolStripMenuItem43";
            this.toolStripMenuItem43.ShortcutKeys = ((System.Windows.Forms.Keys)((System.Windows.Forms.Keys.Alt | System.Windows.Forms.Keys.Delete)));
            this.toolStripMenuItem43.Size = new System.Drawing.Size(211, 22);
            this.toolStripMenuItem43.Text = "关闭所有子窗口";
            this.toolStripMenuItem43.Click += new System.EventHandler(this.toolStripMenuItem43_Click);

具体到某一个画面时,它们的新增,删除,修改的权限时又做了控制.

 for (int i = 0; i 0].Rows.Count; i++)
            {
                if (pParentWin.dsright.Tables[0].Rows[i]["FormName"].ToString() == this.Name)
                {

                    CanIns = Convert.ToBoolean(pParentWin.dsright.Tables[0].Rows[i]["CanIns"]);
                    CanUpd = Convert.ToBoolean(pParentWin.dsright.Tables[0].Rows[i]["CanUpd"]);
                    CanDel = Convert.ToBoolean(pParentWin.dsright.Tables[0].Rows[i]["CanDel"]);
                }
            }
            InitializeTools(1);  // 这里是初始化

// 初始化分为多种情况.

  private void InitializeTools(int Type)
        {
            if (Type == 0) //主窗体加载时,主菜单完全不可用
            {
                pParentWin.toolStrip1.Enabled = false;
                ToolsStatue = 0;

            }
            else if (Type == 1) //加载子窗体后或点预备查询后,查找 预备查询 新增 打印可用其余都不可用
            {
                pParentWin.toolStrip1.Enabled = true;
                pParentWin.toolStripAddNew.Enabled = true;
                pParentWin.toolStripPreSearch.Enabled = true;
                pParentWin.toolStripSearch.Enabled = true;
                pParentWin.toolStripPrint.Enabled = true;

                pParentWin.toolStripFirst.Enabled = false;
                pParentWin.toolStripPre.Enabled = false;
                pParentWin.toolStripNext.Enabled = false;
                pParentWin.toolStripLast.Enabled = false;
                pParentWin.toolStripSave.Enabled = false;
                pParentWin.toolStripCopy.Enabled = false;
                pParentWin.toolStripDelete.Enabled = false;
                if (CanIns == false)
                {
                    pParentWin.toolStripAddNew.Enabled = false; //新增按钮失效
                }

                if (CanUpd == false)
                {
                    pParentWin.toolStripSave.Enabled = false;  //保存按钮失效
                }
                if (CanDel == false)
                {
                    pParentWin.toolStripDelete.Enabled = false;  //删除按钮失效
                }

                ToolsStatue = 1;


            }
            else if (Type == 2)//查询有结果后及保存后,所有工具条可用,如查询无结果,工具条状态为1
            {
                pParentWin.toolStrip1.Enabled = true;
                pParentWin.toolStripAddNew.Enabled = true;
                pParentWin.toolStripPreSearch.Enabled = true;
                pParentWin.toolStripSearch.Enabled = true;
                pParentWin.toolStripPrint.Enabled = true;

                pParentWin.toolStripFirst.Enabled = true;
                pParentWin.toolStripPre.Enabled = true;
                pParentWin.toolStripNext.Enabled = true;
                pParentWin.toolStripLast.Enabled = true;
                pParentWin.toolStripSave.Enabled = true;
                pParentWin.toolStripCopy.Enabled = true;
                pParentWin.toolStripDelete.Enabled = true;
                if (CanIns == false)
                {
                    pParentWin.toolStripAddNew.Enabled = false; //新增按钮失效
                }

                if (CanUpd == false)
                {
                    pParentWin.toolStripSave.Enabled = false;  //保存按钮失效
                }
                if (CanDel == false)
                {
                    pParentWin.toolStripDelete.Enabled = false;  //删除按钮失效
                }
                ToolsStatue = 2;
            }
            else if (Type == 3)//点新增后,除新增不可用。其余工具条全部可用
            {
                pParentWin.toolStrip1.Enabled = true;
                pParentWin.toolStripAddNew.Enabled = false;
                pParentWin.toolStripPreSearch.Enabled = true;
                pParentWin.toolStripSearch.Enabled = true;
                pParentWin.toolStripPrint.Enabled = true;

                pParentWin.toolStripFirst.Enabled = true;
                pParentWin.toolStripPre.Enabled = true;
                pParentWin.toolStripNext.Enabled = true;
                pParentWin.toolStripLast.Enabled = true;
                pParentWin.toolStripSave.Enabled = true;
                pParentWin.toolStripCopy.Enabled = true;
                pParentWin.toolStripDelete.Enabled = true;
                if (CanIns == false)
                {
                    pParentWin.toolStripAddNew.Enabled = false; //新增按钮失效
                }

                if (CanUpd == false)
                {
                    pParentWin.toolStripSave.Enabled = false;  //保存按钮失效
                }
                if (CanDel == false)
                {
                    pParentWin.toolStripDelete.Enabled = false;  //删除按钮失效
                }
                ToolsStatue = 3;
            }
        }

这里基本上就差不多了. 但如果打开多个画面之间,按钮是需要回到当时的状态的.

        private void frm_Activated(object sender, EventArgs e)
        {
            //自定义父窗口的工具栏
            InitializeToolsEvent(1);
            InitializeTools(ToolsStatue);
          }

        private void frm_Deactivate(object sender, EventArgs e)
        {
            //自定义父窗口的工具栏
            InitializeToolsEvent(0);

        }

InitializeTools(ToolsStatue) 就是当时的状态了. 而InitializeToolsEvent就是注册和释放工具栏按钮了.

 private void InitializeToolsEvent(int Type)
        {
            if (Type == 1) //得到焦点
            {
                pParentWin.toolStripSearch.Click += new EventHandler(this.Search);
                pParentWin.toolStripPreSearch.Click += new EventHandler(this.PreSearch);
                pParentWin.toolStripAddNew.Click += new EventHandler(this.AddNew);
                pParentWin.toolStripSave.Click += new EventHandler(this.Save);
                pParentWin.toolStripFirst.Click += new EventHandler(this.First);
                pParentWin.toolStripPre.Click += new EventHandler(this.Pre);
                pParentWin.toolStripNext.Click += new EventHandler(this.Next);
                pParentWin.toolStripLast.Click += new EventHandler(this.Last);
                pParentWin.toolStripDelete.Click += new EventHandler(this.Delete);
                pParentWin.toolStripCopy.Click += new EventHandler(this.Copy);
                pParentWin.toolStripPrint.Click += new EventHandler(this.Print);
            }
            else
            {
                pParentWin.toolStripSearch.Click -= new EventHandler(this.Search);
                pParentWin.toolStripPreSearch.Click -= new EventHandler(this.PreSearch);
                pParentWin.toolStripAddNew.Click -= new EventHandler(this.AddNew);
                pParentWin.toolStripSave.Click -= new EventHandler(this.Save);
                pParentWin.toolStripFirst.Click -= new EventHandler(this.First);
                pParentWin.toolStripPre.Click -= new EventHandler(this.Pre);
                pParentWin.toolStripNext.Click -= new EventHandler(this.Next);
                pParentWin.toolStripLast.Click -= new EventHandler(this.Last);
                pParentWin.toolStripDelete.Click -= new EventHandler(this.Delete);
                pParentWin.toolStripCopy.Click -= new EventHandler(this.Copy);
                pParentWin.toolStripPrint.Click -= new EventHandler(this.Print);
            }
        }

然后这里再对每一个功能进行重写就可以了.

想法很简单, 基本代码也有了. 希望对大家有用,如有不明白的地方,请留言, 有更好的实现方法也请指点一二.  本人不胜感激.

 

(原创作品)转载请注明出处...

  

 

 

 

 

 

 

浅析WINFORM工具条的重用实现,搜素材,soscw.com

浅析WINFORM工具条的重用实现

标签:winform   style   blog   http   color   数据   

原文地址:http://www.cnblogs.com/Geton/p/3824698.html


评论


亲,登录后才可以留言!