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

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 = ne

你可能感兴趣的:(设计模式,ui,c#)