苏飞论坛

标题: TabControl添加关闭按钮 [打印本页]

作者: yangying    时间: 2012-7-27 09:24
标题: TabControl添加关闭按钮
[C#] 纯文本查看 复制代码

        const int CLOSE_SIZE = 4;         

            //清空控件
            this.MainTabControl.TabPages.Clear();
            //绘制的方式OwnerDrawFixed表示由窗体绘制大小也一样
            this.MainTabControl.DrawMode = TabDrawMode.OwnerDrawFixed;
            this.MainTabControl.Padding = new System.Drawing.Point(CLOSE_SIZE, CLOSE_SIZE);
            this.MainTabControl.DrawItem += new DrawItemEventHandler(this.tabControl2_DrawItem);
            this.MainTabControl.MouseDown += new System.Windows.Forms.MouseEventHandler(this.tabControl2_MouseDown);



        

private void tabControl2_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e)
        {
            try
            {
                Rectangle myTabRect = this.MainTabControl.GetTabRect(e.Index);

                //先添加TabPage属性   
                e.Graphics.DrawString(this.MainTabControl.TabPages[e.Index].Text
                , this.Font, SystemBrushes.ControlText, myTabRect.X + 2, myTabRect.Y + 2);

                //再画一个矩形框
                using (Pen p = new Pen(Color.Red))
                {
                    myTabRect.Offset(myTabRect.Width - (CLOSE_SIZE + 3), 2);
                    myTabRect.Width = CLOSE_SIZE;
                    myTabRect.Height = CLOSE_SIZE;
                    e.Graphics.DrawRectangle(p, myTabRect);
                }

                //填充矩形框
                Color recColor = e.State == DrawItemState.Selected ? Color.Red : Color.Red;
                using (Brush b = new SolidBrush(recColor))
                {
                    e.Graphics.FillRectangle(b, myTabRect);
                }

                //画关闭符号
                using (Pen objpen = new Pen(Color.Red))
                {
                    //"\"线
                    Point p1 = new Point(myTabRect.X + 3, myTabRect.Y + 3);
                    Point p2 = new Point(myTabRect.X + myTabRect.Width - 3, myTabRect.Y + myTabRect.Height - 3);
                    e.Graphics.DrawLine(objpen , p1, p2);

                    //"/"线
                    Point p3 = new Point(myTabRect.X + 3, myTabRect.Y + myTabRect.Height - 3);
                    Point p4 = new Point(myTabRect.X + myTabRect.Width - 3, myTabRect.Y + 3);
                    e.Graphics.DrawLine(objpen , p3, p4);
                }

                e.Graphics.Dispose();
            }
            catch (Exception)
            {

            }
        }



        private void tabControl2_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                int x = e.X, y = e.Y;

                //计算关闭区域   
                Rectangle myTabRect = this.MainTabControl.GetTabRect(this.MainTabControl.SelectedIndex);

                myTabRect.Offset(myTabRect.Width - (CLOSE_SIZE + 3), 2);
                myTabRect.Width = CLOSE_SIZE;
                myTabRect.Height = CLOSE_SIZE;

                //如果鼠标在区域内就关闭选项卡   
                bool isClose = x > myTabRect.X && x < myTabRect.Right
                 && y > myTabRect.Y && y < myTabRect.Bottom;

                if (isClose == true)
                {
                    this.MainTabControl.TabPages.Remove(this.MainTabControl.SelectedTab);
                }
            }
        }

作者: 405389329    时间: 2012-11-18 17:42
tabControl2_DrawItem   LZ请问这个事件不响应是怎么回事
作者: 站长苏飞    时间: 2012-11-18 18:44
405389329 发表于 2012-11-18 17:42
tabControl2_DrawItem   LZ请问这个事件不响应是怎么回事

不能吧,这个事件是通过委托绑定的,除非是你没有处罚,代码贴出来看看
作者: 站长苏飞    时间: 2012-11-20 18:00
405389329 发表于 2012-11-20 17:49
using System;
using System.Collections.Generic;
using System.ComponentModel;

我这边正常啊,你说的不响应是怎么回事,事件没有执行,但是我这边是正常的啊,你怎么发现不执行的。说说方式




欢迎光临 苏飞论坛 (http://www.sufeinet.com/) Powered by Discuz! X3.4