怎么修改tabcontrol当前选中的选项卡的标题字体颜色啊,紧急求助!!

2024-12-16 01:19:46
推荐回答(1个)
回答1:

先绑定事件
tabControl1.DrawMode = System.Windows.Forms.TabDrawMode.OwnerDrawFixed;
tabControl1.DrawItem += new DrawItemEventHandler(this.TabControl_DrawItem);

具体事件
///
/// 设置选中选项卡与未选中选项卡背景色
///
///
///
private void TabControl_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);
bshBack = new SolidBrush(Color.FromArgb(((int)(((byte)(23)))), ((int)(((byte)(151)))), ((int)(((byte)(248))))));
bshFore = Brushes.White;
}
else
{
fntTab = e.Font;
bshBack = new SolidBrush(Color.FromArgb(((int)(((byte)(212)))), ((int)(((byte)(208)))), ((int)(((byte)(200))))));
bshFore = new SolidBrush(Color.Black);
}
string tabName = 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);
}