C#更改tabControl選項卡顏色的方法
更新時間:2016年03月03日 10:09:32 作者:Microblue
這篇文章主要介紹了C#更改tabControl選項卡顏色的方法,結合實例形式較為詳細的分析了C#更改tabControl選項卡顏色的的具體步驟與相關實現(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類的構造函數(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); }
更多關于C#相關內(nèi)容感興趣的讀者可查看本站專題:《C#數(shù)據(jù)結構與算法教程》、《C#常見控件用法教程》、《C#面向對象程序設計入門教程》及《C#程序設計之線程使用技巧總結》
希望本文所述對大家C#程序設計有所幫助。
相關文章
C# Char結構中IsLetterOrDigit(Char)的方法詳解
這篇文章給大家介紹了C#的Char 結構的IsLetterOrDigit(Char)的方法,并通過代碼示例給大家介紹的非常詳細,對大家的學習或工作有一定的幫助,需要的朋友可以參考下2024-02-02用C#獲取硬盤序列號,CPU序列號,網(wǎng)卡MAC地址的源碼
用C#獲取硬盤序列號,CPU序列號,網(wǎng)卡MAC地址的源碼...2007-03-03C#自定義鼠標拖拽Drag&Drop效果之基本原理及基本實現(xiàn)代碼
拖拽效果無論是在系統(tǒng)上、應用上、還是在網(wǎng)頁上,拖拽隨處可見,下面通過本文介紹下C#自定義鼠標拖拽Drag&Drop效果之基本原理及基本實現(xiàn)代碼,需要的朋友可以參考下2022-04-04