欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

c#重写TabControl控件实现关闭按钮的方法

程序员文章站 2023-12-16 19:42:52
1.c#里面的tabcontrol控件没有关闭按钮,而且很难看。 2.有一些已经做好的第三方控件,但是收费。 3.由于我的故障树推理诊断项目在绘图的时候允许同时打开多个...

1.c#里面的tabcontrol控件没有关闭按钮,而且很难看。

2.有一些已经做好的第三方控件,但是收费。

3.由于我的故障树推理诊断项目在绘图的时候允许同时打开多个文档进行操作,就要实现类似于浏览器的多标签功能,而且要可以关闭。

4.所以自己写一个类继承tabcontrol类,然后重写一些里面的方法即可实现。

5.特色:有关闭按钮,标签有背景颜色,选中的标签和没选中的颜色不一样,实现鼠标中键和右键的功能

先看我的项目中的完整代码,有很多代码是我的项目需要,可根据你的项目需求删减,核心的代码后面详细解释:

复制代码 代码如下:

 /// <summary>
    /// 重写的tabcontrol控件 带关闭按钮
    /// </summary>
    public class mytabcontrol : tabcontrol
    {
        private int iconwidth = 16;
        private int iconheight = 16;
        private image icon = null;
        private brush biaocolor = brushes.silver; //选项卡的背景色
        private form_paint father;//父窗口,即绘图界面,为的是当选项卡全关后调用父窗口的dispose事件关闭父窗口
        private axmicrosoft.office.interop.visocx.axdrawingcontrol axdrawingcontrol1;
        public mytabcontrol(axmicrosoft.office.interop.visocx.axdrawingcontrol axdrawingcontrol)
            : base()
        {
            this.axdrawingcontrol1 = axdrawingcontrol;
            this.itemsize = new size(50, 25); //设置选项卡标签的大小,可改变高不可改变宽 
            //this.appearance = tabappearance.buttons; //选项卡的显示模式
            this.drawmode = tabdrawmode.ownerdrawfixed;
            icon = properties.resources.close.tobitmap();
            iconwidth = icon.width; iconheight = icon.height;
        }
        /// <summary>
        /// 设置父窗口
        /// </summary>
        /// <param name="fp">画图窗口</param>
        public void setfather(form_paint fp)
        {
            this.father = fp;
        }
        /// <summary>
        /// 重写的绘制事件
        /// </summary>
        /// <param name="e"></param>
        protected override void ondrawitem(drawitemeventargs e)//重写绘制事件。
        {
            graphics g = e.graphics;
            rectangle r = gettabrect(e.index);
            if (e.index == this.selectedindex)    //当前选中的tab页,设置不同的样式以示选中
            {
                brush selected_color = brushes.gold; //选中的项的背景色
                g.fillrectangle(selected_color, r); //改变选项卡标签的背景色
                string title = this.tabpages[e.index].text + "   ";
                g.drawstring(title, this.font, new solidbrush(color.black), new pointf(r.x + 3, r.y + 6));//pointf选项卡标题的位置
                r.offset(r.width - iconwidth - 3, 2);
                g.drawimage(icon, new point(r.x - 2, r.y + 2));//选项卡上的图标的位置 fnttab = new system.drawing.font(e.font, fontstyle.bold);
            }
            else//非选中的
            {
                g.fillrectangle(biaocolor, r); //改变选项卡标签的背景色
                string title = this.tabpages[e.index].text + "   ";
                g.drawstring(title, this.font, new solidbrush(color.black), new pointf(r.x + 3, r.y + 6));//pointf选项卡标题的位置
                r.offset(r.width - iconwidth - 3, 2);
                g.drawimage(icon, new point(r.x - 2, r.y + 2));//选项卡上的图标的位置
            }
        }

        protected override void onmouseclick(mouseeventargs e)
        {
            #region 左键判断是否在关闭区域
            if (e.button == mousebuttons.left)
            {
                point p = e.location;
                rectangle r = gettabrect(this.selectedindex);
                r.offset(r.width - iconwidth - 3, 2);
                r.width = iconwidth;
                r.height = iconheight;
                if (r.contains(p)) //点击特定区域时才发生
                {
                    string temp = this.selectedtab.text;
                    if (temp[temp.length - 5] == '*')//有变化才保存
                    {
                        //确认是否保存vsd文档到ft_doc_path
                        dialogresult response = messagebox.show("是否保存故障树" + this.selectedtab.name + "到图形文件", "请确认", messageboxbuttons.yesnocancel,
                                         messageboxicon.question, messageboxdefaultbutton.button2);
                        if (response == system.windows.forms.dialogresult.yes)//确认保存
                        {
                            axdrawingcontrol1.document.saveas(globalvariables.ft_doc_path + axdrawingcontrol1.document.title + ".vsd");//保存当前文档到文件夹
                            string datetime = datetime.now.tostring();//获取当前时间
                            helptool.savevsddb(axdrawingcontrol1.document.title, datetime);//保存vsd文档到数据库
                            helptool.setdatetimetoxml(axdrawingcontrol1.document.title, datetime);//如果信息已存在则将xml中的日期更新,如果不存在直接插入

                            this.selectedtab.text = temp.substring(0, temp.length - 5) + "   ";//保存后取消星号标志,还原为没变化的时候的样式
                        }
                        else if (response == system.windows.forms.dialogresult.cancel)//点击取消或者关闭
                        {
                            return;//直接退出,撤销这次关闭程序的事件。
                        }
                    }
                    if (this.tabcount == 1)//是最后一个选项卡,直接关闭父界面,即画图界面
                    {
                        father.disposefortabcontrol(true);
                    }
                    else//不是最后一个
                    {
                        this.tabpages.remove(this.selectedtab);
                    }
                }
            }
            #endregion
            #region 右键 选中
            else if (e.button == mousebuttons.right)    //  右键选中
            {
                  for (int i = 0; i < this.tabpages.count; i++)
                  {
                        tabpage tp = this.tabpages[i];
                        if (this.gettabrect(i).contains(new point(e.x, e.y)))
                        {
                              this.selectedtab = tp;
                              break;
                        }
                  }
            }
            #endregion
            #region 中键 选中 关闭
            else if (e.button == mousebuttons.middle)//鼠标中键关闭
            {
                for (int i = 0; i < this.tabpages.count; i++)
                {
                    tabpage tp = this.tabpages[i];
                    if (this.gettabrect(i).contains(new point(e.x, e.y)))//找到后,关闭
                    {
                        this.selectedtab = tp;
                        string temp = tp.text;
                        if (temp[temp.length - 5] == '*')//有变化才保存
                        {
                            //确认是否保存vsd文档到ft_doc_path
                            dialogresult response = messagebox.show("是否保存故障树" + this.selectedtab.name + "到图形文件", "请确认", messageboxbuttons.yesnocancel,
                                             messageboxicon.question, messageboxdefaultbutton.button2);
                            if (response == system.windows.forms.dialogresult.yes)//确认保存
                            {
                                axdrawingcontrol1.document.saveas(globalvariables.ft_doc_path + axdrawingcontrol1.document.title + ".vsd");//保存当前文档到文件夹
                                string datetime = datetime.now.tostring();//获取当前时间
                                helptool.savevsddb(axdrawingcontrol1.document.title, datetime);//保存vsd文档到数据库
                                helptool.setdatetimetoxml(axdrawingcontrol1.document.title, datetime);//如果信息已存在则将xml中的日期更新,如果不存在直接插入

                                this.selectedtab.text = temp.substring(0, temp.length - 5) + "   ";//保存后取消星号标志,还原为没变化的时候的样式
                            }
                            else if (response == system.windows.forms.dialogresult.cancel)//点击取消或者关闭
                            {
                                return;//直接退出,撤销这次关闭程序的事件。
                            }
                        }
                        if (this.tabcount == 1)//是最后一个选项卡,直接关闭父界面,即画图界面
                        {
                            father.disposefortabcontrol(true);
                        }
                        else//不是最后一个
                        {
                            this.tabpages.remove(this.selectedtab);
                        }

                        break;
                    }
                }
            }
            #endregion
        }
    }

实现关闭按钮的关键代码是重写ondrawitem(drawitemeventargs e)方法:

复制代码 代码如下:

protected override void ondrawitem(drawitemeventargs e)//重写绘制事件。
        {
            graphics g = e.graphics;
            rectangle r = gettabrect(e.index);
            if (e.index == this.selectedindex)    //当前选中的tab页,设置不同的样式以示选中
            {
                brush selected_color = brushes.gold; //选中的项的背景色
                g.fillrectangle(selected_color, r); //改变选项卡标签的背景色
                string title = this.tabpages[e.index].text + "   ";
                g.drawstring(title, this.font, new solidbrush(color.black), new pointf(r.x + 3, r.y + 6));//pointf选项卡标题的位置
                r.offset(r.width - iconwidth - 3, 2);
                g.drawimage(icon, new point(r.x - 2, r.y + 2));//选项卡上的图标的位置 fnttab = new system.drawing.font(e.font, fontstyle.bold);
            }
            else//非选中的
            {
                g.fillrectangle(biaocolor, r); //改变选项卡标签的背景色
                string title = this.tabpages[e.index].text + "   ";
                g.drawstring(title, this.font, new solidbrush(color.black), new pointf(r.x + 3, r.y + 6));//pointf选项卡标题的位置
                r.offset(r.width - iconwidth - 3, 2);
                g.drawimage(icon, new point(r.x - 2, r.y + 2));//选项卡上的图标的位置
            }
        }

其中的if-else是用来判断当前选项卡是否是选中的,使选中的颜色和未选中的不一样,项目中不需要的可以去除。


具体实现关闭功能的原理是重写protected override void onmouseclick(mouseeventargs e)方法,左键单击会触发对应事件,判断单击的区域位置是否在关闭按钮的区域,实现关闭功能。另外有对中键和右键的处理,根据你的项目,修改对应按钮事件下的代码即可。

复制代码 代码如下:

protected override void onmouseclick(mouseeventargs e)
        {
            #region 左键判断是否在关闭区域
            if (e.button == mousebuttons.left)
            {
                point p = e.location;
                rectangle r = gettabrect(this.selectedindex);
                r.offset(r.width - iconwidth - 3, 2);
                r.width = iconwidth;
                r.height = iconheight;
                if (r.contains(p)) //点击特定区域时才发生
                {
                    string temp = this.selectedtab.text;
                    if (temp[temp.length - 5] == '*')//有变化才保存
                    {
                        //确认是否保存vsd文档到ft_doc_path
                        dialogresult response = messagebox.show("是否保存故障树" + this.selectedtab.name + "到图形文件", "请确认", messageboxbuttons.yesnocancel,
                                         messageboxicon.question, messageboxdefaultbutton.button2);
                        if (response == system.windows.forms.dialogresult.yes)//确认保存
                        {
                            axdrawingcontrol1.document.saveas(globalvariables.ft_doc_path + axdrawingcontrol1.document.title + ".vsd");//保存当前文档到文件夹
                            string datetime = datetime.now.tostring();//获取当前时间
                            helptool.savevsddb(axdrawingcontrol1.document.title, datetime);//保存vsd文档到数据库
                            helptool.setdatetimetoxml(axdrawingcontrol1.document.title, datetime);//如果信息已存在则将xml中的日期更新,如果不存在直接插入

                            this.selectedtab.text = temp.substring(0, temp.length - 5) + "   ";//保存后取消星号标志,还原为没变化的时候的样式
                        }
                        else if (response == system.windows.forms.dialogresult.cancel)//点击取消或者关闭
                        {
                            return;//直接退出,撤销这次关闭程序的事件。
                        }
                    }
                    if (this.tabcount == 1)//是最后一个选项卡,直接关闭父界面,即画图界面
                    {
                        father.disposefortabcontrol(true);
                    }
                    else//不是最后一个
                    {
                        this.tabpages.remove(this.selectedtab);
                    }
                }
            }
            #endregion
            #region 右键 选中
            else if (e.button == mousebuttons.right)    //  右键选中
            {
                  for (int i = 0; i < this.tabpages.count; i++)
                  {
                        tabpage tp = this.tabpages[i];
                        if (this.gettabrect(i).contains(new point(e.x, e.y)))
                        {
                              this.selectedtab = tp;
                              break;
                        }
                  }
            }
            #endregion
            #region 中键 选中 关闭
            else if (e.button == mousebuttons.middle)//鼠标中键关闭
            {
                for (int i = 0; i < this.tabpages.count; i++)
                {
                    tabpage tp = this.tabpages[i];
                    if (this.gettabrect(i).contains(new point(e.x, e.y)))//找到后,关闭
                    {
                        this.selectedtab = tp;
                        string temp = tp.text;
                        if (temp[temp.length - 5] == '*')//有变化才保存
                        {
                            //确认是否保存vsd文档到ft_doc_path
                            dialogresult response = messagebox.show("是否保存故障树" + this.selectedtab.name + "到图形文件", "请确认", messageboxbuttons.yesnocancel,
                                             messageboxicon.question, messageboxdefaultbutton.button2);
                            if (response == system.windows.forms.dialogresult.yes)//确认保存
                            {
                                axdrawingcontrol1.document.saveas(globalvariables.ft_doc_path + axdrawingcontrol1.document.title + ".vsd");//保存当前文档到文件夹
                                string datetime = datetime.now.tostring();//获取当前时间
                                helptool.savevsddb(axdrawingcontrol1.document.title, datetime);//保存vsd文档到数据库
                                helptool.setdatetimetoxml(axdrawingcontrol1.document.title, datetime);//如果信息已存在则将xml中的日期更新,如果不存在直接插入

                                this.selectedtab.text = temp.substring(0, temp.length - 5) + "   ";//保存后取消星号标志,还原为没变化的时候的样式
                            }
                            else if (response == system.windows.forms.dialogresult.cancel)//点击取消或者关闭
                            {
                                return;//直接退出,撤销这次关闭程序的事件。
                            }
                        }
                        if (this.tabcount == 1)//是最后一个选项卡,直接关闭父界面,即画图界面
                        {
                            father.disposefortabcontrol(true);
                        }
                        else//不是最后一个
                        {
                            this.tabpages.remove(this.selectedtab);
                        }

                        break;
                    }
                }
            }
            #endregion
        }

写完之后如何使用呢???

在你的窗体上拖一个tabcontrol,然后打开对应窗体代码文件的.designer.cs文件里找到private void initializecomponent()方法,然后找到里面对应的tabcontrol的定义语句即 this.tabcontrol =。。。。改成this.tabcontrol = new mytabcontrol();如果想传参,就在前面重写mytabcontrol时加入带参的构造函数(我的就带有参数)。

值得一提的是.designer.cs文件里找到private void initializecomponent()方法都是程序根据你的可视化界面设计自动生成的,所以每次你在可视化的设计环境下重新编辑了,这里就会重新生成,所以你得手动再次改一下this.tabcontrol = new mytabcontrol();


我的程序效果如下

c#重写TabControl控件实现关闭按钮的方法

 

上一篇:

下一篇: