设置DrawItem事件
private void tabControl1_DrawItem(object sender, DrawItemEventArgs e)//绘制tabcontrol控件的标签颜色和字体大小
{
SolidBrush back;
SolidBrush white;
SolidBrush black = new SolidBrush(Color.Black);//字体的颜色
if (e.Index==tabControl1.SelectedIndex)//选中标签的颜色
{
back = new SolidBrush(Color.Wheat);
white = new SolidBrush(Color.Blue);
}
else//其他标签的颜色
{
back = new SolidBrush(Color.SeaShell);
white = new SolidBrush(Color.Blue);
}
StringFormat sf = new StringFormat()
{
Alignment = StringAlignment.Center,
LineAlignment=StringAlignment.Center
};
//绑定选项卡
Rectangle rec1 = tabControl1.GetTabRect(e.Index);
//设置选项卡背景
e.Graphics.FillRectangle(back, rec1);
for (int i = 0; i < tabControl1.TabPages.Count; i++)
{
Rectangle rec = tabControl1.GetTabRect(i);
//设置标签文字字体和文字大小
e.Graphics.DrawString(tabControl1.TabPages[i].Text, new Font("宋体", 15, FontStyle.Bold), black, rec, sf);
}
//SolidBrush bisque = new SolidBrush(Color.White);//绘制标签1的颜色
//SolidBrush lightcyan = new SolidBrush(Color.LightCyan);//绘制标签2的颜色
//SolidBrush LightPink = new SolidBrush(Color.LightPink);
//SolidBrush black = new SolidBrush(Color.Black);//字体的颜色
//StringFormat stringFormat = new StringFormat();
//stringFormat.Alignment = StringAlignment.Center;//设置标签文字居中显示
//stringFormat.LineAlignment = StringAlignment.Center;
//Rectangle rec1 = tabControl1.GetTabRect(0);
//Rectangle rec2 = tabControl1.GetTabRect(1);
//Rectangle rec3 = tabControl1.GetTabRect(2);
//e.Graphics.FillRectangle(lightcyan, rec1);
//e.Graphics.FillRectangle(lightcyan, rec2);
//e.Graphics.FillRectangle(lightcyan, rec3);
//for (int i = 0; i < tabControl1.TabPages.Count; i++)
//{
// Rectangle rec = tabControl1.GetTabRect(i);
// //设置标签文字字体和文字大小
// e.Graphics.DrawString(tabControl1.TabPages[i].Text, new Font("宋体", 15, FontStyle.Bold), black, rec, stringFormat);
//}
}
标签:自定义,Color,标签,tabControl1,SolidBrush,new,TabControl,重绘,Rectangle
From: https://www.cnblogs.com/fjfhwfn/p/17717354.html