更改选项卡控件未使用空间的颜色

本文关键字:空间 颜色 未使用 控件 选项 | 更新日期: 2023-09-27 18:34:19

我想更改 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

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

更改选项卡控件未使用空间的颜色

选项卡右侧的透明度由视觉样式呈现器提供。 但是,本机 Windows 控件将在设置 DrawMode 属性时禁用它。 没有办法改变这种行为。

最好的方法是完全摆脱它,一般来说它是毫无价值的。 并完全接管这幅画。 您可以通过从 TabControl 派生自己的类并设置 ControlStyles.UserPaint 样式标志来执行的操作。 向项目添加新类并粘贴如下所示的代码。 编译。 将新控件从工具箱顶部拖放到窗体上。 您可能希望自定义 DrawTab() 方法以获得所需的外观,您将有很多机会使其看起来像您想要的任何方式。 另请注意,您可以覆盖 OnPaintBackground() 以使选项卡井看起来像您想要的那样,您不必破解表单的绘画。

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);
            }
        }
    }
}