首先需要将 绘制模式修改成 自定义模式
只有DrawMode等于OwnerDrawFixed时,才会在绘制选项卡时发生DrawItem事件
tabControl1.DrawMode = TabDrawMode.OwnerDrawFixed;
注册事件
const int CLOSE_SIZE = 12;// 定义范围大小
private void tabControl1_DrawItem(object sender, DrawItemEventArgs e)//DrawItem事件
{
try
{
标签背景填充颜色
SolidBrush BackBrush = new SolidBrush(Color.FromArgb(5, 131, 205));
//标签文字填充颜色
SolidBrush FrontBrush = new SolidBrush(Color.Black);
//获取当前Tab选项卡的绘图区域
Rectangle myTabRect = this.tabControl1.GetTabRect(e.Index);
//绘制标签头背景色
using (Brush brBack = new SolidBrush(Color.White))
{
if (e.Index == this.tabControl1.SelectedIndex)
{
e.Graphics.FillRectangle(brBack, myTabRect); //设置当前选中的tabgage的背景色
}
}
StringFormat StringF = new StringFormat();
//设置文字对齐方式
StringF.Alignment = StringAlignment.Near;
StringF.LineAlignment = StringAlignment.Center;
for (int i = 0; i < tabControl1.TabPages.Count; i++)
{
//获取标签头工作区域
Rectangle Rec = tabControl1.GetTabRect(i);
if (tabControl1.SelectedIndex == i)//选中的item更改颜色
{
//绘制标签头文字
e.Graphics.DrawString(tabControl1.TabPages[i].Text, new Font("微软雅黑", 11), FrontBrush, Rec, StringF);
}
else
{
//绘制标签头背景颜色
//e.Graphics.FillRectangle(BackBrush, Rec);
e.Graphics.DrawString(tabControl1.TabPages[i].Text, new Font("微软雅黑", 11), FrontBrush, Rec, StringF);
}
}
//先添加TabPage属性
//e.Graphics.DrawString(this.tabControl1.TabPages[e.Index].Text, this.Font, SystemBrushes.ControlText, myTabRect.X + 2, myTabRect.Y + 2);
//再画一个矩形框
using (Pen p = new Pen(Color.Transparent))
{
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.Transparent : Color.Transparent;
using (Brush b = new SolidBrush(recColor))
{
e.Graphics.FillRectangle(b, myTabRect);
}
//画Tab选项卡右上方关闭按钮
using (Pen objpen = new Pen(Color.Red, 1.8f))
{
//自己画X
//"\"线
Point p1 = new Point(myTabRect.X + 10, myTabRect.Y + 10);
Point p2 = new Point(myTabRect.X + myTabRect.Width - 10, myTabRect.Y + myTabRect.Height - 10);
e.Graphics.DrawLine(objpen, p1, p2);
//"/"线
Point p3 = new Point(myTabRect.X + 10, myTabRect.Y + myTabRect.Height - 10);
Point p4 = new Point(myTabRect.X + myTabRect.Width - 10, myTabRect.Y + 10);
e.Graphics.DrawLine(objpen, p3, p4);
使用图片
//Bitmap bt = new Bitmap(image);
//Point p5 = new Point(myTabRect.X, 4);//获取绘图区域的开始坐标位置
//e.Graphics.DrawImage(bt, p5);
}
e.Graphics.Dispose();
}
catch (Exception)
{
}
}
//鼠标点击事件
private void tabControl1_MouseDown(object sender, MouseEventArgs e)
{
if (this.tabControl1.TabCount == 0)
{
return;
}
if (!String.IsNullOrEmpty(this.tabControl1.SelectedTab.Text))
{
if (e.Button == MouseButtons.Left)
{
int x = e.X, y = e.Y;
//计算关闭区域
Rectangle myTabRect = this.tabControl1.GetTabRect(this.tabControl1.SelectedIndex); ;
myTabRect.Offset(myTabRect.Width - 0x12, 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);
}
}
}
}
绑定事件
this.tabControl1.DrawItem += tabControl1_DrawItem;
this.tabControl1.MouseDown += tabControl1_MouseDown;
另外操作关于 tabpage标签大小的操作
SizeMode属性设为Fixed 然后设置ItemSize的大小就可以了
new size(80,20)
其中设置问题
参考地址:https://www.cnblogs.com/zhang-rui/p/13209207.html