更改 TabControl 未使用空间的颜色 [英] Change color of unused space of TabControl

查看:21
本文介绍了更改 TabControl 未使用空间的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

但是因为我在 TabPages 中添加了一个关闭按钮并且我使用了

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

`tabControl1.DrawMode = TabDrawMode.OwnerDrawFixed` 

我改变了tabControl1_DrawItem

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

the code above doesn't work for me.

推荐答案

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

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.

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

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

这篇关于更改 TabControl 未使用空间的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆