【问题标题】:How to change the background color of unused space tab in C# winforms?如何更改 C# winforms 中未使用空间选项卡的背景颜色?
【发布时间】:2012-08-06 05:01:05
【问题描述】:

  |Tab1|Tab2|Tab3| {    }
  |                     |
  |                     |
  |                     |
  |                     |
  |_____________________|

我可以更改Tabbackcolorforecolor.. 但我想更改那个{} 的颜色--> 空白空间是否可以这样做。 ..它显示默认的winforms颜色..帮助我在dis..

 private void Form1_Load(object sender, EventArgs e)
    {

    }


    private void tabControl1_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e)
    {
        Font fntTab;
        Brush bshBack;
        Brush bshFore;

        if ( e.Index == this.tabControl1.SelectedIndex)
        {
            fntTab = new Font(e.Font, FontStyle.Bold);
            bshBack = new System.Drawing.Drawing2D.LinearGradientBrush(e.Bounds, SystemColors.Control, SystemColors.Control, System.Drawing.Drawing2D.LinearGradientMode.BackwardDiagonal);
            bshFore = Brushes.Black;
            //bshBack = new System.Drawing.Drawing2D.LinearGradientBrush(e.Bounds, Color.LightSkyBlue , Color.LightGreen, System.Drawing.Drawing2D.LinearGradientMode.BackwardDiagonal);
            //bshFore = Brushes.Blue;
        }
        else
        {
            fntTab = e.Font;
            bshBack = new SolidBrush(Color.Red);
            bshFore = new SolidBrush(Color.Aqua);

            //bshBack = new SolidBrush(Color.White);
            //bshFore = new SolidBrush(Color.Black);
        }

        string tabName  = this.tabControl1.TabPages[e.Index].Text;
        StringFormat sftTab = new StringFormat();
        e.Graphics.FillRectangle(bshBack, e.Bounds);
        Rectangle  recTab = e.Bounds;
        recTab = new Rectangle( recTab.X,  recTab.Y + 4,  recTab.Width,  recTab.Height - 4);
        e.Graphics.DrawString(tabName, fntTab, bshFore, recTab, sftTab);

    }

【问题讨论】:

  • 我不认为标准的 .NET TabControl 允许您将选项卡右侧的这个“可用空间”设置为特定的背景颜色 - 它是透明的并且只显示表单的背景颜色。如果你真的需要这个,你必须找到另一个支持这个功能的 TabControl
  • 将外观属性更改为“正常”,它将更改为透明
  • 有更多充分的理由这样做而不是这样做。
  • @marc_s:OP 正在将他的选项卡控件 DrawMode 设置为 OwnerDrawFixed 以自定义选项卡控件的样式。但是,这样做不再将背景空白空间设置为透明,而是将其设置为 system.control

标签: c# winforms tabs tabcontrol


【解决方案1】:

您也可以像以前一样创建自定义选项卡控件

public class mytab : TabControl
{
    public mytab()
        : base()
    {
        this.DrawMode = TabDrawMode.OwnerDrawFixed;
        this.DrawItem += new DrawItemEventHandler(tabControl1_DrawItem);
    }

    private void tabControl1_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e)
    {
        Font fntTab;
        Brush bshBack;
        Brush bshFore;

        if (e.Index == this.SelectedIndex)
        {
            fntTab = new Font(e.Font, FontStyle.Bold);
            bshBack = new System.Drawing.Drawing2D.LinearGradientBrush(e.Bounds, SystemColors.Control, SystemColors.Control, System.Drawing.Drawing2D.LinearGradientMode.BackwardDiagonal);
            bshFore = Brushes.Black;
            //bshBack = new System.Drawing.Drawing2D.LinearGradientBrush(e.Bounds, Color.LightSkyBlue , Color.LightGreen, System.Drawing.Drawing2D.LinearGradientMode.BackwardDiagonal);
            //bshFore = Brushes.Blue;
        }
        else
        {
            fntTab = e.Font;
            bshBack = new SolidBrush(Color.Red);
            bshFore = new SolidBrush(Color.Aqua);

            //bshBack = new SolidBrush(Color.White);
            //bshFore = new SolidBrush(Color.Black);
        }

        string tabName = this.TabPages[e.Index].Text;
        StringFormat sftTab = new StringFormat();
        e.Graphics.FillRectangle(bshBack, e.Bounds);
        Rectangle recTab = e.Bounds;
        recTab = new Rectangle(recTab.X, recTab.Y + 4, recTab.Width, recTab.Height - 4);
        e.Graphics.DrawString(tabName, fntTab, bshFore, recTab, sftTab);


        Rectangle r = this.GetTabRect(this.TabPages.Count - 1);

        RectangleF tf =
            new RectangleF(r.X + r.Width,
            r.Y-5, this.Width - (r.X + r.Width)+5, r.Height+5);
        Brush b = Brushes.BlueViolet;

        e.Graphics.FillRectangle(b, tf);
    }

}

【讨论】:

    【解决方案2】:

    尝试将以下代码添加到您的 DrawItem 事件处理程序。不要忘记将 DrawMode 设置为“OwnerdrawFixed”。

    您可能需要稍微调整一下以覆盖一些未绘制的边距。

    私人无效tabControl1_DrawItem(对象发送者,System.Windows.Forms.DrawItemEventArgs e) { SolidBrush fillbrush=新的SolidBrush(Color.Red); //draw rectangle behind the tabs Rectangle lasttabrect = tabControl1.GetTabRect(tabControl1.TabPages.Count - 1); Rectangle background = new Rectangle(); background.Location = new Point(lasttabrect.Right, 0); //pad the rectangle to cover the 1 pixel line between the top of the tabpage and the start of the tabs background.Size = new Size(tabControl1.Right - background.Left, lasttabrect.Height+1); e.Graphics.FillRectangle(fillBrush, background); }

    '这个答案比之前的要好得多。但是 tabCtrl 没有定义。它必须是 tabControl1 控件。

    【讨论】:

    • 在 windows 10 的开始菜单下拖动时出现此错误。它将使用控制颜色重绘组件
    【解决方案3】:

    我认为为该空间赋予颜色的唯一方法是覆盖窗口的OnPaintBackground 方法,因此只需将其粘贴到您的表单(窗口)上

    您还必须将外观属性更改为“正常”

    private void Form1_Load(object sender, EventArgs e)
    {
    
    }
    
    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 );
    }
    

    对我来说效果很好

    【讨论】:

    • 您不必只编写代码,或者如果您使用 Visual Studio 编写 protected override 和空格,它将为您提供覆盖的方法,但“只需编写 | 粘贴代码”方法将被自动覆盖@user1484603
    • Seddik 我也试过了……但它对我不起作用……正如你所说,我重写了该方法并粘贴了代码……但是空白空间没有被绘制……跨度>
    • 尝试创建一个新项目,只添加这个 TabControl 和我的代码,看看它是否有效,在我的情况下,我有一个带有 TabControl 的窗口,它有效;即使我编写了您的代码,也添加了 OnPaintBackground 方法,它可以完美运行
    • 你用过tabControl1.DrawMode = TabDrawMode.OwnerDrawFixed@user1484603
    • Seddik 刚刚检查了新项目的工作..非常感谢... :)
    猜你喜欢
    • 2011-07-30
    • 2011-06-11
    • 2013-04-01
    • 1970-01-01
    • 2019-08-06
    • 1970-01-01
    • 2017-04-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多