更改 TabControl 未使用空间的颜色

2024-02-05

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

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


选项卡右侧的透明度由视觉样式渲染器提供。但是,当您设置 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);
            }
        }
    }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

更改 TabControl 未使用空间的颜色 的相关文章

  • C中函数指针的递归声明

    我想声明一个返回指向相同类型函数的指针的函数 我想用它来实现如下状态机 typedef event handler t event handler t event t compilation error event handler t st
  • C# 测试活动的互联网连接。 Ping google.com

    C 2008 我正在使用此代码来测试互联网连接 因为我的应用程序必须登录到网络服务器 但是 如果用户互联网连接失败或电缆被拔出 我必须通知用户 Ping www google com to check if the user has a i
  • C++ 和序列化:有什么方法可以进行某种内省吗?

    我读过一些例子维基百科 http en wikipedia org wiki Type introspection C 2B 2B但我正在寻找一些现实生活中的例子 如何使用内省 为什么 它有助于编写干净的代码 以及代码本身 例如 有没有办法
  • 当我使用 SetWindowsHookEx WH_KEYBOARD_LL 交换按键时,为什么我的程序会陷入过多键盘输入事件的循环?

    I am trying to write a program for Windows system that swaps the A and B keys i e when I press the A key B gets typed an
  • NHibernate IQueryable 集合作为 root 的属性

    我有一个根对象 它有一个集合属性 例如 I have a Shelf object that has Books Now public class Shelf public ICollection
  • 在目标 VS 安装时,VSIX 扩展内部使用的 WPF-Log4Net 未输出日志

    当 Log4net 在 VSIX 扩展中使用并安装在另一个目标 VS 上时 它不会记录日志 我有一个 WPF 解决方案 我下载了 log4net dll 添加了 log4net config 并将 复制到输出目录 值设置为 始终复制 log
  • 为什么将 char 传递给函数会改变它在 c 中的值?

    我目前正在关注本作业簿 http www cs bham ac uk exr lectures opsys 10 11 lectures os dev pdf关于构建操作系统 我的目的是写一个64位内核 我已经在文本模式下加载 内核 代码并
  • boost::asio::io_service 是否保留处理程序的顺序?

    Does boost asio io service http www boost org doc libs release doc html boost asio reference io service html保证处理程序的调用顺序与
  • 以编程方式运行 T4 文本模板

    有没有一种方法可以通过代码以编程方式运行 T4 文本模板 我正在制作一种自定义域特定语言 我希望相关的文本模板在用户每次保存时运行 目前 这就是我在 DSL 模型中所做的事情 protected override void OnDocume
  • 从套接字读取 C HTTP

    我想知道如何判断是否已从套接字接收到所有数据 这是一个简单的网络代理 现在我正在处理请求部分 所以发送的内容应该以 r n r n 结尾 我不知道请求会持续多久 我在这里读过一些帖子 说我应该检查读取函数是否返回 0 但其他人说0只在客户端
  • ASP.NET中如何访问除wwwroot以外的位置

    我可以使用访问服务器的物理位置Server MapPath 这给了我内部的物理路径wwwroot文件夹 我想将一些数据保存到同一服务器的另一个驱动器中D 驾驶 我想我无法获取以下位置的物理位置D 驾驶使用Server MapPath因为它位
  • 修改正在运行的可执行文件的资源内容

    All 我将应用程序设置存储在资源中 当我的程序首次加载时 我使用 WinAPI 读取指定的资源 然后我解析检索到的字节数据 这对我来说完美无缺 现在假设用户更改了我的应用程序中的设置 他 她检查复选框控件 我想将更新的设置保存到我的资源中
  • Identity Server 4:添加访问令牌的声明

    我正在使用 Identity Server 4 和隐式流 并且想要向访问令牌添加一些声明 新的声明或属性是 tenantId 和 langId 我已将 langId 添加为我的范围之一 如下所示 然后通过身份服务器请求 但我也获得了tena
  • 为什么这个单独的定义会导致错误?

    挑战 我有这段代码无法编译 你能找出问题所在吗 有一次让我很头疼 header namespace values extern std string address extern int port cpp file std string v
  • 为什么 httpRuntime targetFramework="4.5" 禁止抓取 .ASPXAUTH cookie?

    当我的 web config 具有以下 httpRuntime 时 我的控制器无法获取 cookie ASPXAUTH 它似乎能够获取任何其他 cookie 无论带或不带句点前缀 如果我删除下面的行 它就可以正常工作
  • C 中的链表数组:初始化和插入?

    我需要创建一个链表数组 如图所示 这就是我到目前为止所做的 typedef struct Node int data struct Node next Node int main void Node link 5 for int q 0 q
  • 为什么必须通过 this 指针访问模板基类成员?

    如果下面的类不是模板 我可以简单地拥有x in the derived班级 但是 通过下面的代码 我have to use this gt x Why template
  • #define 内存地址声明

    这个 define 语句有什么作用 它用于定义内存地址 但我不明白 uint32 t 部分 define GPxDAT uint32 t 0x6FC0 通常用于访问映射到地址空间的硬件寄存器 或者一些特定的内存地址 硬件寄存器应定义为vol
  • 如何创建和使用类箭头运算符? [复制]

    这个问题在这里已经有答案了 因此 在到处研究之后 我似乎找不到如何创建类箭头运算符 即 class Someclass operator gt 我只需要知道如何使用它并正确使用它 它的输入是什么 它返回什么 我如何正确地声明 原型化它 运算
  • 替换全局热键

    我有一个位于托盘中的应用程序 我想定义多个热键来触发我的程序中的事件 我从 AaronLS 在这个问题中的出色回答中找到了灵感 使用C 设置全局热键 https stackoverflow com a 27309185 3064934 如果

随机推荐