C#重绘TabControl

C#重绘TabControl的Tabpage标签,添加图片及关闭按钮

C#重绘TabControl
Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->using System;   
using System.Collections.Generic;   
using System.ComponentModel;   
using System.Data;   
using System.Drawing;   
using System.Linq;   
using System.Text;   
using System.Windows.Forms;   
  
namespace TabControlTest   
{   
    public partial class Form1 : Form   
    {   
        public Form1()   
        {   
            InitializeComponent();   
        }   
  
        const int CLOSE_SIZE = 15;   
        Bitmap image = new Bitmap("ONE.ico");   
  
  
        private void MainTabControl_DrawItem(object sender, 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.White))   
                {   
                    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.White : Color.White;   
                using (Brush b = new SolidBrush(recColor))   
                {   
                    e.Graphics.FillRectangle(b, myTabRect);   
                }   
  
                //画关闭符号   
                using (Pen objpen = new Pen(Color.Black))   
                {   
                    //"\"线   
                    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);   
                    ////=============================================   
                    Bitmap bt = new Bitmap(image);   
                    Point p5 = new Point(myTabRect.X - 50, 4);   
                    e.Graphics.DrawImage(bt, p5);   
                    //e.Graphics.DrawString(this.MainTabControl.TabPages[e.Index].Text, this.Font, objpen.Brush, p5);   
                }   
  
  
                //绘制小图标                 
                //==============================================================================   
                //Bitmap bt = new Bitmap("E:\\1\\2.jpg");   
                //Point p5 = new Point(4, 4);   
                ////e.Graphics.DrawImage(bt, e.Bounds);   
                //e.Graphics.DrawImage(bt, p5);   
                //Pen pt = new Pen(Color.Red);   
                ////e.Graphics.DrawString(this.MainTabControl.TabPages[e.Index].Text, this.Font, pt.Brush, e.Bounds);   
                //e.Graphics.DrawString(this.MainTabControl.TabPages[e.Index].Text, this.Font, pt.Brush, p5);   
  
                e.Graphics.Dispose();   
            }   
            catch  
            {   
    
            }   
    
        }   
  
        private void MainTabControl_MouseDown(object sender, 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);   
                 }   
             }   
    
  
        }   
  
         //初始化页面   
         private void Form1_Load(object sender, EventArgs e)   
         {   
             //清空控件   
             //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.MainTabControl_DrawItem);   
             this.MainTabControl.MouseDown += new System.Windows.Forms.MouseEventHandler(this.MainTabControl_MouseDown);   
         }   
  
         //添加新的tabPage并修改所有tabPage标签上面的图片   
         private void button1_Click(object sender, EventArgs e)   
         {   
             TabPage tabtage = new TabPage();   
             MainTabControl.TabPages.Add(tabtage);   
             MainTabControl.SelectedTab = tabtage;   
             image = new Bitmap("ONE.ico");   
  
  
         }   
         //关闭选中的tabPage   
         private void button2_Click(object sender, EventArgs e)   
         {   
             MainTabControl.TabPages.Remove(MainTabControl.SelectedTab);   
         }   
    }   
}
View Code

C#重绘TabControl的Tabpage标签,添加图片及关闭按钮2

C#重绘TabControl
C#重绘TabControl的Tabpage标签,添加图片及关闭按钮

2009-12-15  来自:网上整理  字体大小:【大 中 小】
摘要:本文介绍C#重绘TabControl的Tabpage标签,添加图片及关闭按钮,并提供完整的实现代码。

 以下就是C#重绘TabControl控件的源码:

 1 using System;
  2 using System.Collections.Generic;
  3 using System.ComponentModel;
  4 using System.Data;
  5 using System.Drawing;
  6 using System.Linq;
  7 using System.Text;
  8 using System.Windows.Forms;
  9 
 10 
 11 namespace TabControlTest
 12 {
 13     public partial class Form1 : Form
 14     {
 15         public Form1()
 16         {
 17             InitializeComponent();
 18         }
 19 
 20         const int CLOSE_SIZE = 15;
 21  //tabPage标签图片
 22         Bitmap image = new Bitmap("E:\\1\\2.jpg");
 23 //绘制“X”号即关闭按钮
 24         private void MainTabControl_DrawItem(object sender, DrawItemEventArgs e)
 25         {
 26 
 27             try
 28             {
 29                 Rectangle myTabRect = this.MainTabControl.GetTabRect(e.Index);
 30 
 31                 //先添加TabPage属性   
 32                 e.Graphics.DrawString(this.MainTabControl.TabPages[e.Index].Text
 33                 , this.Font, SystemBrushes.ControlText, myTabRect.X + 2, myTabRect.Y + 2);
 34 
 35                 //再画一个矩形框
 36                 using (Pen p = new Pen(Color.White))
 37                 {
 38                     myTabRect.Offset(myTabRect.Width - (CLOSE_SIZE + 3), 2);
 39                     myTabRect.Width = CLOSE_SIZE;
 40                     myTabRect.Height = CLOSE_SIZE;
 41                     e.Graphics.DrawRectangle(p, myTabRect);
 42                  
 43                 }
 44 
 45                 //填充矩形框
 46                 Color recColor = e.State == DrawItemState.Selected ? Color.White : Color.White;
 47                 using (Brush b = new SolidBrush(recColor))
 48                 {
 49                     e.Graphics.FillRectangle(b, myTabRect);
 50                 }
 51 
 52                 //画关闭符号
 53                 using (Pen objpen = new Pen(Color.Black))
 54                 {
 55                     //"\"线
 56                     Point p1 = new Point(myTabRect.X + 3, myTabRect.Y + 3);
 57                     Point p2 = new Point(myTabRect.X + myTabRect.Width - 3, myTabRect.Y + myTabRect.Height - 3);
 58                     e.Graphics.DrawLine(objpen, p1, p2);
 59 
 60                     //"/"线
 61                     Point p3 = new Point(myTabRect.X + 3, myTabRect.Y + myTabRect.Height - 3);
 62                     Point p4 = new Point(myTabRect.X + myTabRect.Width - 3, myTabRect.Y + 3);
 63                     e.Graphics.DrawLine(objpen, p3, p4);
 64                       ////=============================================
 65                     Bitmap bt = new Bitmap(image);
 66                     Point p5 = new Point(myTabRect.X-50, 4);
 67                     e.Graphics.DrawImage(bt, p5);
 68                     //e.Graphics.DrawString(this.MainTabControl.TabPages[e.Index].Text, this.Font, objpen.Brush, p5);
 69                 }
 70 
 71 
 72                 //绘制小图标              
 73                 //==============================================================================
 74                 //Bitmap bt = new Bitmap("E:\\1\\2.jpg");
 75                 //Point p5 = new Point(4, 4);
 76                 ////e.Graphics.DrawImage(bt, e.Bounds);
 77                 //e.Graphics.DrawImage(bt, p5);
 78                 //Pen pt = new Pen(Color.Red);
 79                 ////e.Graphics.DrawString(this.MainTabControl.TabPages[e.Index].Text, this.Font, pt.Brush, e.Bounds);
 80                 //e.Graphics.DrawString(this.MainTabControl.TabPages[e.Index].Text, this.Font, pt.Brush, p5);
 81 
 82                 e.Graphics.Dispose();
 83             }
 84             catch (Exception)
 85             {
 86 
 87             }
 88 
 89 
 90         }
 91         //=======================================================================
 92      
 93 //关闭按钮功能
 94         private void MainTabControl_MouseDown(object sender, MouseEventArgs e)
 95         {
 96             if (e.Button == MouseButtons.Left)
 97             {
 98                 int x = e.X, y = e.Y;
 99 
100                 //计算关闭区域   
101                 Rectangle myTabRect = this.MainTabControl.GetTabRect(this.MainTabControl.SelectedIndex);
102 
103                 myTabRect.Offset(myTabRect.Width - (CLOSE_SIZE + 3), 2);
104                 myTabRect.Width = CLOSE_SIZE;
105                 myTabRect.Height = CLOSE_SIZE;
106 
107                 //如果鼠标在区域内就关闭选项卡   
108                 bool isClose = x > myTabRect.X && x < myTabRect.Right
109                  && y > myTabRect.Y && y < myTabRect.Bottom;
110 
111                 if (isClose == true)
112                 {
113                     this.MainTabControl.TabPages.Remove(this.MainTabControl.SelectedTab);
114                 }
115             }
116 
117 
118         }
119 //初始化页面
120         private void Form1_Load(object sender, EventArgs e)
121         {
122             //清空控件
123             //this.MainTabControl.TabPages.Clear();
124             //绘制的方式OwnerDrawFixed表示由窗体绘制大小也一样
125             this.MainTabControl.DrawMode = TabDrawMode.OwnerDrawFixed;
126             this.MainTabControl.Padding = new System.Drawing.Point(CLOSE_SIZE, CLOSE_SIZE);
127             this.MainTabControl.DrawItem += new DrawItemEventHandler(this.MainTabControl_DrawItem);
128             this.MainTabControl.MouseDown += new System.Windows.Forms.MouseEventHandler(this.MainTabControl_MouseDown);
129         }
130 
131  //添加新的tabPage并修改所有tabPage标签上面的图片
132         private void button1_Click(object sender, EventArgs e)
133         {
134             TabPage tabtage=new TabPage ();
135             MainTabControl.TabPages.Add(tabtage);
136             MainTabControl.SelectedTab = tabtage;
137             image = new Bitmap("E:\\1\\3.jpg");
138 
139 
140         }
141 //关闭选中的tabPage
142         private void button2_Click(object sender, EventArgs e)
143         {
144             MainTabControl.TabPages.Remove(MainTabControl.SelectedTab);
145         }
146 
147      
148 
149     }
150 }

C#重绘TabControl的Tabpage标签,添加图片及关闭按钮就像你介绍到这里了,希望对你有所帮助。
View Code

C#重绘TabControl控件的源码

C#重绘TabControl
1using System;
  2 using System.Collections.Generic;
  3 using System.ComponentModel;
  4 using System.Data;
  5 using System.Drawing;
  6 using System.Linq;
  7 using System.Text;
  8 using System.Windows.Forms;
  9
  10
  11 namespace TabControlTest
  12 {
  13     public partial class Form1 : Form
  14     {
  15         public Form1()
  16         {
  17             InitializeComponent();
  18         }
  19
  20         const int CLOSE_SIZE = 15;
  21 //tabPage标签图片
  22         Bitmap image = new Bitmap("E:\\1\\2.jpg");
  23 //绘制“X”号即关闭按钮
  24         private void MainTabControl_DrawItem(object sender, DrawItemEventArgs e)
  25         {
  26
  27             try
  28             {
  29                 Rectangle myTabRect = this.MainTabControl.GetTabRect(e.Index);
  30
  31                 //先添加 TabPage属性
  32                 e.Graphics.DrawString(this.MainTabControl.TabPages[e.Index].Text
  33                 , this.Font, SystemBrushes.ControlText, myTabRect.X + 2, myTabRect.Y + 2);
  34
  35                 //再画一个矩形框
  36                 using (Pen p = new Pen(Color.White))
  37                 {
  38                     myTabRect.Offset(myTabRect.Width - (CLOSE_SIZE + 3), 2);
  39                     myTabRect.Width = CLOSE_SIZE;
  40                     myTabRect.Height = CLOSE_SIZE;
  41                     e.Graphics.DrawRectangle(p, myTabRect);
  42
  43                 }
  44
  45                 //填充矩形框
  46                 Color recColor = e.State == DrawItemState.Selected ? Color.White : Color.White;
  47                 using (Brush b = new SolidBrush(recColor))
  48                 {
  49                     e.Graphics.FillRectangle(b, myTabRect);
  50                 }
  51
  52                 //画关闭符号
  53                 using (Pen objpen = new Pen(Color.Black))
  54                 {
  55                     //"\"线
  56                     Point p1 = new Point(myTabRect.X + 3, myTabRect.Y + 3);
  57                     Point p2 = new Point(myTabRect.X + myTabRect.Width - 3, myTabRect.Y + myTabRect.Height - 3);
  58                     e.Graphics.DrawLine(objpen, p1, p2);
  59
  60                     //"/"线
  61                     Point p3 = new Point(myTabRect.X + 3, myTabRect.Y + myTabRect.Height - 3);
  62                     Point p4 = new Point(myTabRect.X + myTabRect.Width - 3, myTabRect.Y + 3);
  63                     e.Graphics.DrawLine(objpen, p3, p4);
  64                       ////=============================================
  65                     Bitmap bt = new Bitmap(image);
  66                     Point p5 = new Point(myTabRect.X-50, 4);
  67                     e.Graphics.DrawImage(bt, p5);
  68                     //e.Graphics.DrawString(this.MainTabControl.TabPages[e.Index].Text, this.Font, objpen.Brush, p5);
  69                 }
  70
  71
  72                 //绘制小图标
  73                 //==============================================================================
  74                 //Bitmap bt = new Bitmap("E:\\1\\2.jpg");
  75                 //Point p5 = new Point(4, 4);
  76                 ////e.Graphics.DrawImage(bt, e.Bounds);
  77                 //e.Graphics.DrawImage(bt, p5);
  78                 //Pen pt = new Pen(Color.Red);
  79                 ////e.Graphics.DrawString(this.MainTabControl.TabPages[e.Index].Text, this.Font, pt.Brush, e.Bounds);
  80                 //e.Graphics.DrawString(this.MainTabControl.TabPages[e.Index].Text, this.Font, pt.Brush, p5);
  81
  82                 e.Graphics.Dispose();
  83             }
  84             catch (Exception)
  85             {
  86
  87             }
  88
  89
  90         }
  91         //=======================================================================
  92
  93 //关闭按钮功能
  94         private void MainTabControl_MouseDown(object sender, MouseEventArgs e)
  95         {
  96             if (e.Button == MouseButtons.Left)
  97             {
  98                 int x = e.X, y = e.Y;
  99
  100                 //计算关闭区域
  101                 Rectangle myTabRect = this.MainTabControl.GetTabRect(this.MainTabControl.SelectedIndex);
  102
  103                 myTabRect.Offset(myTabRect.Width - (CLOSE_SIZE + 3), 2);
  104                 myTabRect.Width = CLOSE_SIZE;
  105                 myTabRect.Height = CLOSE_SIZE;
  106
  107                 //如果鼠标在区域内就关闭选项卡
  108                 bool isClose = x > myTabRect.X && x < myTabRect.Right
  109                  && y > myTabRect.Y && y < myTabRect.Bottom;
  110
  111                 if (isClose == true)
  112                 {
  113                     this.MainTabControl.TabPages.Remove(this.MainTabControl.SelectedTab);
  114                 }
  115             }
  116
  117
  118         }
  119 //初始化页面
  120         private void Form1_Load(object sender, EventArgs e)
  121         {
  122             //清空控件
  123             //this.MainTabControl.TabPages.Clear();
  124             //绘制的方式 OwnerDrawFixed表示由窗体绘制大小也一样
  125             this.MainTabControl.DrawMode = TabDrawMode.OwnerDrawFixed;
  126             this.MainTabControl.Padding = new System.Drawing.Point(CLOSE_SIZE, CLOSE_SIZE);
  127             this.MainTabControl.DrawItem += new DrawItemEventHandler(this.MainTabControl_DrawItem);
  128             this.MainTabControl.MouseDown += new System.Windows.Forms.MouseEventHandler(this.MainTabControl_MouseDown);
  129         }
  130
  131 //添加新的tabPage并修改所有 tabPage标签上面的图片
  132         private void button1_Click(object sender, EventArgs e)
  133         {
  134             TabPage tabtage=new TabPage ();
  135             MainTabControl.TabPages.Add(tabtage);
  136             MainTabControl.SelectedTab = tabtage;
  137             image = new Bitmap("E:\\1\\3.jpg");
  138
  139
  140         }
  141 //关闭选中的tabPage
  142         private void button2_Click(object sender, EventArgs e)
  143         {
  144             MainTabControl.TabPages.Remove(MainTabControl.SelectedTab);
  145         }
  146
  147
  148
  149     }
  150 }
View Code

 

C#重绘TabControl

上一篇:C#:XML操作类


下一篇:DELPHI开发LINUX插件架构的程序