C#更改tabControl选项卡颜色的方法
作者:Microblue 时间:2022-09-07 19:30:14
本文实例讲述了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选项卡
0
投稿
猜你喜欢
Springboot添加支付接口
2023-06-21 00:10:20
Spring+SpringMVC+JDBC实现登录的示例(附源码)
2021-11-16 08:29:56
C语言 MD5的源码实例详解
2022-12-22 05:37:16
Android基于注解的6.0权限动态请求框架详解
2023-08-13 13:31:32
浅谈Android中视图动画的属性与使用
2023-04-15 22:41:12
Android Studio kotlin生成编辑类注释代码
2023-06-16 12:03:20
Mybatis配置之typeAlias标签的用法
2023-11-27 20:18:20
Android应用开发中模拟按下HOME键的效果(实现代码)
2023-03-09 00:13:14
C#多线程学习之(四)使用线程池进行多线程的自动管理
2021-07-17 10:04:43
C#、ASP.NET通用扩展工具类之LogicSugar
2023-11-18 09:56:07
c#在程序中定义和使用自定义事件方法总结
2022-07-12 01:45:30
使用Flutter开发的抖音国际版实例代码详解
2023-11-12 09:38:44
eclipse 中的javac命令与java命令
2023-08-19 14:16:57
Spring整合Mybatis思路梳理总结
2022-04-26 03:56:12
android轻量级无侵入式管理数据库自动升级组件
2023-12-09 15:36:01
详解Java编程中protected修饰符与static修饰符的作用
2022-05-11 03:53:17
Java中的Random()函数及两种构造方法
2023-05-12 03:00:46
android 大图片拖拽并缩放实现原理
2022-11-10 05:59:55
spring整合redis缓存并以注解(@Cacheable、@CachePut、@CacheEvict)形式使用
2022-04-25 01:46:14
C#添加、获取、删除PDF附件实例代码
2023-06-23 11:41:46