欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C#更改tabControl选项卡颜色的方法

程序员文章站 2022-11-24 08:35:27
本文实例讲述了c#更改tabcontrol选项卡颜色的方法。分享给大家供大家参考,具体如下: private void form1_load(object sen...

本文实例讲述了c#更改tabcontrol选项卡颜色的方法。分享给大家供大家参考,具体如下:

private void form1_load(object sender, eventargs e)
{
  this.tabcontrol1.drawmode = system.windows.forms.tabdrawmode.ownerdrawfixed;
  this.tabcontrol1.drawitem += new drawitemeventhandler(this.tabcontrol1_drawitem);
}
private void tabcontrol1_drawitem(object sender, system.windows.forms.drawitemeventargs e)
{
  stringformat sf = new stringformat();
  sf.linealignment = stringalignment.center;
  sf.alignment = stringalignment.center;
  if (e.index == tabcontrol1.selectedindex)
    e.graphics.fillrectangle(brushes.red, e.bounds.x, e.bounds.y, e.bounds.width, e.bounds.height);
  else
    e.graphics.fillrectangle(brushes.white, e.bounds.x, e.bounds.y, e.bounds.width, e.bounds.height);
  e.graphics.drawstring(((tabcontrol)sender).tabpages[e.index].text,
  system.windows.forms.systeminformation.menufont, new solidbrush(color.black), e.bounds, sf);
}

1.在form类的构造函数中添加下列语句:

this.tabcontrol1.drawmode = system.windows.forms.tabdrawmode.ownerdrawfixed;
this.tabcontrol1.drawitem += new drawitemeventhandler(this.tabcontrol1_drawitem);

2.实现下列函数:

private void tabcontrol1_drawitem(object sender, system.windows.forms.drawitemeventargs e)
{
   font fnttab;
   brush bshback;
   brush bshfore;
   if ( e.index == this.tabcontrol1.selectedindex)
   {
     fnttab = new font(e.font, fontstyle.bold);
     bshback = new system.drawing.drawing2d.lineargradientbrush(e.bounds, systemcolors.control, systemcolors.control, system.drawing.drawing2d.lineargradientmode.backwarddiagonal);
     bshfore = brushes.black;
   }
   else
   {
     fnttab = e.font;
     bshback = new solidbrush(color.blue );
     bshfore = new solidbrush(color.black);
   }
   string tabname  = this.tabcontrol1.tabpages[e.index].text;
   stringformat sfttab = new stringformat();
   e.graphics.fillrectangle(bshback, e.bounds);
   rectangle  rectab = e.bounds;
   rectab = new rectangle( rectab.x,  rectab.y + 4,  rectab.width,  rectab.height - 4);
   e.graphics.drawstring(tabname, fnttab, bshfore, rectab, sfttab);
}

更多关于c#相关内容感兴趣的读者可查看本站专题:《c#数据结构与算法教程》、《c#常见控件用法教程》、《c#面向对象程序设计入门教程》及《c#程序设计之线程使用技巧总结

希望本文所述对大家c#程序设计有所帮助。