You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何移除左侧TabControl选中标签的浮雕边框并设置下划线样式

移除TabControl选中标签的浮雕边框

我正在开发的窗体中使用了左对齐的TabControl,需求是选中标签仅通过标题下划线来标识,不改变颜色或其他样式。如下图所示,选中标签带有一个用于营造凸起效果的浮雕边框,我无法找到移除该边框的方法,恳请指教。

带边框的标签

以下是我用于设置文本对齐和颜色控制的代码。我尝试过增大每个标签的矩形区域,但绘制的矩形似乎在边框下方,无法覆盖它。

private void FFTSettingsTabControl_DrawItem(object sender, DrawItemEventArgs e)
{
    //Get the working area of the TabControl main control
    Rectangle rec = FFTSettingsTabControl.ClientRectangle;
    
    //Create a StringFormat object to set the layout of the label text
    StringFormat StrFormat = new StringFormat();
    StrFormat.LineAlignment = StringAlignment.Center;// Set the text to be centered vertically
    StrFormat.Alignment = StringAlignment.Near;// Set the text to be centered horizontally

    // The background fill color of the label, it can also be a picture (e.Graphics.DrawImage)
    SolidBrush backColor = new SolidBrush(Color.FromArgb(255, (byte)20, (byte)20, (byte)20));
    Pen p = new Pen(backColor);
    SolidBrush fontColor;// Label font color
                         //Draw the background of the main control
    e.Graphics.FillRectangle(backColor, rec);
    e.Graphics.DrawRectangle(p, rec);

    //Draw label style
    Font fntTab = e.Font;
    Font fntSel = new Font(e.Font, FontStyle.Underline);

    Brush bshBack = new SolidBrush(Color.FromArgb(255, (byte)20, (byte)20, (byte)20));

    for (int i = 0; i < FFTSettingsTabControl.TabPages.Count; i++)
    {
        bool bSelected = (FFTSettingsTabControl.SelectedIndex == i);
        Rectangle recBounds = FFTSettingsTabControl.GetTabRect(i);
        RectangleF tabTextArea = (RectangleF)FFTSettingsTabControl.GetTabRect(i);
        if (bSelected)
        {
            e.Graphics.FillRectangle(bshBack, recBounds);
            fontColor = new SolidBrush(Color.White);
            e.Graphics.DrawString(FFTSettingsTabControl.TabPages[i].Text, fntSel, fontColor, tabTextArea, StrFormat);
        }
        else
        {
            fontColor = new SolidBrush(Color.White);
            e.Graphics.DrawString(FFTSettingsTabControl.TabPages[i].Text, fntTab, fontColor, tabTextArea, StrFormat);
        }
    }
}

解决方法

你的代码存在两个关键问题:

  1. DrawItem事件使用错误:该事件会为每个标签单独触发,不需要循环遍历所有TabPage,应针对e.Index对应的标签绘制。
  2. 默认边框未被完全覆盖:系统默认的浮雕边框绘制在自定义内容之上,需要确保用背景色完全覆盖标签区域。

修改后的代码如下:

private void FFTSettingsTabControl_DrawItem(object sender, DrawItemEventArgs e)
{
    TabControl tabControl = sender as TabControl;
    if (tabControl == null) return;

    // 获取当前要绘制的标签页
    TabPage tabPage = tabControl.TabPages[e.Index];
    bool isSelected = (e.Index == tabControl.SelectedIndex);

    // 设置文本对齐格式
    StringFormat strFormat = new StringFormat()
    {
        LineAlignment = StringAlignment.Center,
        Alignment = StringAlignment.Near
    };

    // 背景色与控件一致
    Color bgColor = Color.FromArgb(255, 20, 20, 20);
    using (SolidBrush bgBrush = new SolidBrush(bgColor))
    {
        // 先填充整个标签区域,覆盖默认边框
        Rectangle tabRect = tabControl.GetTabRect(e.Index);
        // 稍微扩大矩形范围,确保完全覆盖边框(可根据实际情况调整)
        tabRect.Inflate(1, 1);
        e.Graphics.FillRectangle(bgBrush, tabRect);

        // 绘制控件整体背景(仅在第一个标签绘制时执行一次,避免重复绘制)
        if (e.Index == 0)
        {
            Rectangle clientRect = tabControl.ClientRectangle;
            e.Graphics.FillRectangle(bgBrush, clientRect);
            using (Pen pen = new Pen(bgColor))
            {
                e.Graphics.DrawRectangle(pen, clientRect);
            }
        }
    }

    // 设置字体和颜色
    using (Font tabFont = isSelected ? new Font(tabControl.Font, FontStyle.Underline) : tabControl.Font)
    using (SolidBrush fontBrush = new SolidBrush(Color.White))
    {
        RectangleF textArea = tabControl.GetTabRect(e.Index);
        e.Graphics.DrawString(tabPage.Text, tabFont, fontBrush, textArea, strFormat);
    }

    // 防止系统绘制默认边框
    e.DrawFocusRectangle();
}

同时请确认你的TabControl已设置DrawMode = TabDrawMode.OwnerDrawFixed,否则自定义绘制不会生效。


内容的提问来源于stack exchange,提问作者dwilliams

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.16 19:15:58