我有一个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 );
}
来自here。