[英]TabControl Custom Tab Bar Background Color
我有一个 TabControl,我需要在其中自定义选项卡颜色。 为此,我将 DrawMode 设置为 OwnerDrawFixed,并覆盖了 DrawItem 事件。 但是,当我更改 DrawMode 时,选项卡栏的透明度似乎被替换为灰色。 我需要再次使其透明,或者将颜色显式更改为主页背景颜色,但我不知道如何执行此操作。
我可以在 OwnerDrawFixed 和 Normal 之间来回切换,并在设计模式下看到透明度的变化,但即使在正常情况下运行,我也会得到灰色的标签栏背景。
我的覆盖代码如下。
private void SettingsTabControl_DrawItem( object sender, DrawItemEventArgs e )
{
TabPage tab = SettingsTabControl.TabPages[e.Index];
Rectangle header = SettingsTabControl.GetTabRect( e.Index );
using (SolidBrush darkBrush = new SolidBrush( Color.FromArgb( 0, 68, 124 ) ))
using (SolidBrush lightBrush = new SolidBrush( Color.FromArgb( 179, 191, 218 ) ))
{
StringFormat sf = new StringFormat();
sf.Alignment = StringAlignment.Center;
sf.LineAlignment = StringAlignment.Center;
if (e.State == DrawItemState.Selected)
{
Font font = new Font( SettingsTabControl.Font.Name, 10, FontStyle.Bold );
e.Graphics.FillRectangle( lightBrush, e.Bounds );
e.Graphics.DrawString( tab.Text, font, darkBrush, header, sf );
}
else
{
e.Graphics.FillRectangle( darkBrush, e.Bounds );
e.Graphics.DrawString( tab.Text, e.Font, lightBrush, header, sf );
}
}
}
我也希望删除选项卡框周围的灰色边框,但除了在它的顶部绘画之外,似乎没有什么好方法可以做到这一点。 有没有更好的办法?
我将以下代码添加到我的 SettingsTabControl_DrawItem 方法中,它解决了这个特定问题。 我仍然有边框颜色问题,但我想我可以忍受。
//draw rectangle behind the tabs
Rectangle lastTabRect = SettingsTabControl.GetTabRect( SettingsTabControl.TabPages.Count - 1 );
Rectangle background = new Rectangle();
background.Location = new Point( lastTabRect.Right, 0 );
//pad the rectangle to cover the 1 pixel line between the top of the tabpage and the start of the tabs
background.Size = new Size( SettingsTabControl.Right - background.Left, lastTabRect.Height + 1 );
using (SolidBrush b = new SolidBrush( Colors.Get( Item.BorderBackground ) ))
{
e.Graphics.FillRectangle( b, background );
}
从这里开始。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.