自定义重绘TabControl的各个标签


设置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);
//}

    }
posted @   涵有为  阅读(153)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架
点击右上角即可分享
微信分享提示