手机
当前位置:查字典教程网 >编程开发 >C#教程 >C#更改tabControl选项卡颜色的方法
C#更改tabControl选项卡颜色的方法
摘要:本文实例讲述了C#更改tabControl选项卡颜色的方法。分享给大家供大家参考,具体如下:privatevoidForm1_Load(ob...

本文实例讲述了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#更改tabControl选项卡颜色的方法】相关文章:

C# javascript 读写Cookie的方法

c#遍历System.drawing.Color下面的所有颜色以及名称以查看

C#操作注册表的方法

C#使用非托管代码直接修改字符串的方法

C# datatable 不能通过已删除的行访问该行的信息处理方法

C#下解析HTML的两种方法介绍

C# 抓取网页内容的方法

C#生成sitemap站点地图的方法

C# 获取打印机当前状态的方法

C#中读写INI文件的方法例子

精品推荐
分类导航