C#更改tabControl选项卡颜色的方法
2016-03-03 10:09
801 查看
本文实例讲述了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#程序设计有所帮助。
您可能感兴趣的文章:
相关文章推荐
- c#调用COM组件
- C#实现把指定数据写入串口
- C#动态创建button的方法
- C#中抽象方法与虚拟方法的区别
- c#中虚函数的相关使用方法
- C#实现给图片加水印的方法
- C#使用加边法计算行列式的值
- C#实现多线程的同步方法实例分析
- C#中尾递归的使用、优化及编译器优化
- C#中的delegate委托类型基本学习教程
- C#实现子窗体与父窗体通信方法实例总结
- C#通用邮件发送类分享
- 举例讲解C#中自动实现的属性
- C#中this的用法集锦
- C#数据结构之顺序表(SeqList)实例详解
- C#.NET获取拨号连接的宽带连接方法
- C#异步绑定数据实现方法
- C#实现AddRange为数组添加多个元素的方法
- C#中Equality和Identity浅析
- C#比较二个数组并找出相同或不同元素的方法