C#更改tabControl选项卡颜色的方法
本文实例讲述了C#更改tabControl选项卡颜色的方法。分享给大家供大家参考,具体如下:
privatevoidForm1_Load(objectsender,EventArgse) { this.tabControl1.DrawMode=System.Windows.Forms.TabDrawMode.OwnerDrawFixed; this.tabControl1.DrawItem+=newDrawItemEventHandler(this.tabControl1_DrawItem); } privatevoidtabControl1_DrawItem(objectsender,System.Windows.Forms.DrawItemEventArgse) { StringFormatsf=newStringFormat(); 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,newSolidBrush(Color.Black),e.Bounds,sf); }
1.在Form类的构造函数中添加下列语句:
this.tabControl1.DrawMode=System.Windows.Forms.TabDrawMode.OwnerDrawFixed; this.tabControl1.DrawItem+=newDrawItemEventHandler(this.tabControl1_DrawItem);
2.实现下列函数:
privatevoidtabControl1_DrawItem(objectsender,System.Windows.Forms.DrawItemEventArgse) { FontfntTab; BrushbshBack; BrushbshFore; if(e.Index==this.tabControl1.SelectedIndex) { fntTab=newFont(e.Font,FontStyle.Bold); bshBack=newSystem.Drawing.Drawing2D.LinearGradientBrush(e.Bounds,SystemColors.Control,SystemColors.Control,System.Drawing.Drawing2D.LinearGradientMode.BackwardDiagonal); bshFore=Brushes.Black; } else { fntTab=e.Font; bshBack=newSolidBrush(Color.Blue); bshFore=newSolidBrush(Color.Black); } stringtabName=this.tabControl1.TabPages[e.Index].Text; StringFormatsftTab=newStringFormat(); e.Graphics.FillRectangle(bshBack,e.Bounds); RectanglerecTab=e.Bounds; recTab=newRectangle(recTab.X,recTab.Y+4,recTab.Width,recTab.Height-4); e.Graphics.DrawString(tabName,fntTab,bshFore,recTab,sftTab); }
更多关于C#相关内容感兴趣的读者可查看本站专题:《C#数据结构与算法教程》、《C#常见控件用法教程》、《C#面向对象程序设计入门教程》及《C#程序设计之线程使用技巧总结》
希望本文所述对大家C#程序设计有所帮助。