Change color of unused space of TabControl

时光总嘲笑我的痴心妄想 提交于 2019-12-07 20:12:51

问题


I want to change the color of unused space in the right of TabPage headers.

I tried to override the OnPaintBackground method of the window and it is working, this is the code I used:

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

But because I added a close button to my TabPages and I use

`tabControl1.DrawMode = TabDrawMode.OwnerDrawFixed` 

and I changed the tabControl1_DrawItem

the code above doesn't work for me.


回答1:


The transparency at the right of the tabs is provided by the visual style renderer. However, the native Windows control will disable it when you set the DrawMode property. No way to change that behavior.

The best approach is to just get rid of it completely, it is quite worthless in general. And take over the painting completely. Something you can do by deriving your own class from TabControl and setting the ControlStyles.UserPaint style flag. Add a new class to your project and paste the code shown below. Compile. Drop the new control from the top of the toolbox onto your form. You'll probably want to customize the DrawTab() method to get the appearance you want, you'll have lots of opportunities to make it look any way you want. Also note that you can override OnPaintBackground() to make the tabwell look the way you want it, you don't have to hack the form's painting.

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


来源:https://stackoverflow.com/questions/27469886/change-color-of-unused-space-of-tabcontrol

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!