C#更改tabControl選項卡顏色的方法
更新時間:2016年03月03日 10:09:32 作者:Microblue
這篇文章主要介紹了C#更改tabControl選項卡顏色的方法,結(jié)合實例形式較為詳細(xì)的分析了C#更改tabControl選項卡顏色的的具體步驟與相關(guān)實現(xiàn)技巧,需要的朋友可以參考下
本文實例講述了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類的構(gòu)造函數(shù)中添加下列語句:
this.tabControl1.DrawMode = System.Windows.Forms.TabDrawMode.OwnerDrawFixed; this.tabControl1.DrawItem += new DrawItemEventHandler(this.tabControl1_DrawItem);
2.實現(xiàn)下列函數(shù):
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);
}
更多關(guān)于C#相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《C#數(shù)據(jù)結(jié)構(gòu)與算法教程》、《C#常見控件用法教程》、《C#面向?qū)ο蟪绦蛟O(shè)計入門教程》及《C#程序設(shè)計之線程使用技巧總結(jié)》
希望本文所述對大家C#程序設(shè)計有所幫助。
相關(guān)文章
c# 怎樣簡潔高效的實現(xiàn)多個 Enum 自由轉(zhuǎn)換
這篇文章主要介紹了c# 怎樣簡潔高效的實現(xiàn)多個 Enum 自由轉(zhuǎn)換,幫助大家更好的進(jìn)行c# 開發(fā),感興趣的朋友可以了解下2020-10-10
c# List find()方法返回值的問題說明(返回結(jié)果為對象的指針)
本篇文章主要介紹了c#中List find()方法返回值的問題說明(返回結(jié)果為對象的指針) 需要的朋友可以過來參考下,希望對大家有所幫助2014-01-01

