更改TabControl的未使用空间的颜色

时间:2014-12-14 13:38:22

标签: c# winforms tabcontrol

我想更改TabPage标题右侧未使用空间的颜色。

我试图覆盖窗口的OnPaintBackground方法并且它正常工作,这是我使用的代码:

protected override void OnPaintBackground(PaintEventArgs e)
{
    base.OnPaintBackground(e);
    Rectangle lasttabrect = tabControl1.GetTabRect(tabControl1.TabPages.Count - 1);
    RectangleF emptyspacerect = new RectangleF(
            lasttabrect.X + lasttabrect.Width + tabControl1.Left,
            tabControl1.Top + lasttabrect.Y,
            tabControl1.Width - (lasttabrect.X + lasttabrect.Width),
            lasttabrect.Height);

    Brush b = Brushes.BlueViolet; // the color you want
    e.Graphics.FillRectangle(b, emptyspacerect);
}

但是因为我在我的TabPages上添加了一个关闭按钮,我使用

`tabControl1.DrawMode = TabDrawMode.OwnerDrawFixed` 

我更改了tabControl1_DrawItem

上面的代码对我不起作用。

1 个答案:

答案 0 :(得分:6)

选项卡右侧的透明度由视觉样式渲染器提供。但是,当您设置DrawMode属性时,本机Windows控件将禁用它。无法改变这种行为。

最好的方法是完全摆脱它,一般来说它毫无价值。并完全接管这幅画。你可以通过从TabControl派生自己的类并设置ControlStyles.UserPaint样式标志来做的事情。在项目中添加一个新类并粘贴下面显示的代码。编译。将新控件从工具箱顶部拖放到表单上。你可能想要自定义DrawTab()方法来获得你想要的外观,你将有很多机会让它看起来你想要的任何方式。另请注意,您可以重写OnPaintBackground()以使tabwell看起来像您想要的那样,您不必破解表单的绘画。

using System;
using System.Drawing;
using System.Windows.Forms;

class MyTabControl : TabControl {
    public MyTabControl() {
        // Take over the painting completely, we want transparency and double-buffering
        this.SetStyle(ControlStyles.UserPaint | ControlStyles.SupportsTransparentBackColor, true);
        this.DoubleBuffered = this.ResizeRedraw = true;
    }

    public override Color BackColor {
        // Override TabControl.BackColor, we need transparency
        get { return Color.Transparent; }
        set { base.BackColor = Color.Transparent; }
    }

    protected virtual void DrawTabRectangle(Graphics g, int index, Rectangle r) {
        if (index == 0) r = new Rectangle(r.Left - 2, r.Top, r.Width + 2, r.Height);
        if (index != this.SelectedIndex) r = new Rectangle(r.Left, r.Top + 2, r.Width, r.Height - 2);
        Color tabColor;
        if (index == this.SelectedIndex) tabColor = Color.FromKnownColor(KnownColor.Window);
        else tabColor = Color.FromArgb(0xf0, 0xf0, 0xf0);
        using (var br = new SolidBrush(tabColor)) {
            g.FillRectangle(br, r);
        }
    }

    protected virtual void DrawTab(Graphics g, int index, Rectangle r) {
        r.Inflate(-1, -1);
        TextRenderer.DrawText(g, this.TabPages[index].Text, this.Font,
            r, Color.FromKnownColor(KnownColor.WindowText), 
            TextFormatFlags.Left | TextFormatFlags.VerticalCenter);
    }

    protected override void OnPaint(PaintEventArgs e) {
        if (TabCount <= 0) return;
        // Draw tabpage area
        Rectangle r = ClientRectangle;
        var top = this.GetTabRect(0).Bottom;
        using (var br = new SolidBrush(Color.FromKnownColor(KnownColor.Window))) {
            e.Graphics.FillRectangle(br, new Rectangle(r.Left, top, r.Width, r.Height - top));
        }
        // Draw tabs
        for (int index = 0; index < TabCount; index++) {
            r = GetTabRect(index);
            DrawTabRectangle(e.Graphics, index, r);
            DrawTab(e.Graphics, index, r);
            if (index == this.SelectedIndex) {
                r.Inflate(-1, -1);
                ControlPaint.DrawFocusRectangle(e.Graphics, r);
            }
        }
    }
}