以下是main窗体部分源码
======================================
//在tabPage中添加关闭按钮
const int CLOSE_SIZE = 15;public Main()
{
InitializeComponent();
MyMain = this;
//this.tabControl1.TabPages.Clear();
this.tabControl1.DrawMode = TabDrawMode.OwnerDrawFixed;
this.tabControl1.Padding = new System.Drawing.Point(CLOSE_SIZE, CLOSE_SIZE);
this.tabControl1.DrawItem += new DrawItemEventHandler(this.tabControl1_DrawItem);
this.tabControl1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.tabControl1_MouseDown);
}
//重绘关闭按钮
private void tabControl1_DrawItem(object sender, DrawItemEventArgs e)
{
try
{
//Rectangle rec = tabControl1.ClientRectangle;
//SolidBrush br = new SolidBrush(Color.White);
//e.Graphics.FillRectangle(br, rec);
// 重绘tabPage
Rectangle myTabRect = this.tabControl1.GetTabRect(e.Index);
//添加TabPage属性
//新建一个StringFormat对象,用于对标签文字的布局设置
StringFormat StrFormat = new StringFormat();
StrFormat.LineAlignment = StringAlignment.Center;// 设置文字垂直方向居中
StrFormat.Alignment = StringAlignment.Center;// 设置文字水平方向居中
// 标签背景填充颜色,也可以是图片
SolidBrush bru = new SolidBrush(Color.WhiteSmoke);
SolidBrush bruFont = new SolidBrush(Color.Black);// 标签字体颜色
Font font = new System.Drawing.Font("宋体", 12F);//设置标签字体样式
//绘制标签头背景颜色
e.Graphics.DrawString(this.tabControl1.TabPages[e.Index].Text, this.Font, bruFont, myTabRect.X + 2, myTabRect.Y + 2);
//再画一个矩形框
using (Pen p = new Pen(Color.Black))
{
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.DarkRed : Color.DarkGray;
using (Brush b = new SolidBrush(recColor))
{
e.Graphics.FillRectangle(b, myTabRect);
}
tabControl1.Appearance = TabAppearance.Normal;
//画关闭符号
using (Pen p = new Pen(Color.White))
{
//画"/"线
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(p, 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(p, p3, p4);
}
e.Graphics.Dispose();
}catch(Exception ex)
{
}
}
private void tabControl1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left && this.tabControl1.SelectedIndex > 0)
{
int x = e.X, y = e.Y;
//计算关闭区域
Rectangle myTabRect = this.tabControl1.GetTabRect(this.tabControl1.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.tabControl1.TabPages.Remove(this.tabControl1.SelectedTab);
}
}
return ;
}