当您使用OwnerDrawFixed 时,这意味着您 将提供绘图代码。如果您没有连接并使用DrawItem 事件,则不会绘制任何内容。这在设计时看起来和你的差不多,因为事件没有触发。对于设计时绘制,您必须对控件进行子类化并使用OnDrawItem。
// colors to use
private Color[] TColors = {Color.Salmon, Color.White, Color.LightBlue};
private void tabControl1_DrawItem(object sender, DrawItemEventArgs e)
{
// get ref to this page
TabPage tp = ((TabControl)sender).TabPages[e.Index];
using (Brush br = new SolidBrush(TColors[e.Index]))
{
Rectangle rect = e.Bounds;
e.Graphics.FillRectangle(br, e.Bounds);
rect.Offset(1, 1);
TextRenderer.DrawText(e.Graphics, tp.Text,
tp.Font, rect, tp.ForeColor);
// draw the border
rect = e.Bounds;
rect.Offset(0, 1);
rect.Inflate(0, -1);
// ControlDark looks right for the border
using (Pen p = new Pen(SystemColors.ControlDark))
{
e.Graphics.DrawRectangle(p, rect);
}
if (e.State == DrawItemState.Selected) e.DrawFocusRectangle();
}
}
基本结果:
标签拇指在我看来有点局促,没有默认的那么高。所以,我添加了一个TFontSize 来绘制与字体大小不同的文本。
将TabControl.Font 设置为 10(这似乎很多),以便 Windows 绘制稍大的拇指/标题。如果还是在默认的 8.25 处绘制文字,还有更多的空间:
private float TFontSize = 8.25F; // font drawing size
...
using (Font f = new Font(tp.Font.FontFamily,TFontSize))
{
// shift for a gutter/padding
rect.Offset(1, 1);
TextRenderer.DrawText(e.Graphics, tp.Text,
f, rect, tp.ForeColor);
}
您会以这种方式失去的一件事是 VisualStyles 效果,但无论如何它们似乎与彩色标签发生冲突。