【问题标题】:Background color for Tabcontrol in c# windows applicationc# windows应用程序中Tabcontrol的背景颜色
【发布时间】:2009-10-13 05:24:20
【问题描述】:

我正在尝试为在 tabcontrol 中创建的选项卡添加字体颜色和背景颜色,并为没有选项卡的空白位置添加背景颜色。因为我的 tabcontrol 被拉伸到全屏表单的大小。假设在 tabcontrol 中只创建了一个 tab,tab 的背景颜色应该是蓝色,tabcontrol 的其余部分应该是深蓝色。默认颜色为灰色,非常难看。

我正在使用下面的代码,它适用于选项卡的字体颜色和背景颜色。但我没有得到其他地方的背景颜色。

我错过了什么吗?

     private void tabControl1_DrawItem_1(object sender, DrawItemEventArgs e)
    {
            //set color for Background
            Brush BG_BackBrush = new SolidBrush(Color.FromArgb(111, 111, 111)); //Set background color 
            //Rectangle BG_r = e.Bounds;
            Rectangle BG_r = new Rectangle(1682, 34, this.Width - 2, this.Height - 2);
            BG_r = new Rectangle(BG_r.X, BG_r.Y + 8, BG_r.Width, BG_r.Height - 3);
            e.Graphics.FillRectangle(BG_BackBrush, e.Bounds);


            //set color for Tabs
            Font TabFont;
            Brush BackBrush = new SolidBrush(Color.FromArgb(147, 188, 200)); //Set background color
            Brush ForeBrush = new SolidBrush(Color.FromArgb(11, 51, 106));//Set foreground color  


            if (e.Index == TaskBarRef.tabControl1.SelectedIndex)
            {
                TabFont = new Font(e.Font, FontStyle.Bold);
            }
            else
            {
                TabFont = new Font(e.Font, FontStyle.Regular);
            }
            Rectangle r = e.Bounds;
            r = new Rectangle(r.X, r.Y + 8, r.Width, r.Height - 3);

            string TabName = TaskBarRef.tabControl1.TabPages[e.Index].Text;
            StringFormat sf = new StringFormat();
            sf.Alignment = StringAlignment.Center;

            e.Graphics.FillRectangle(BackBrush, e.Bounds);
            e.Graphics.DrawString(TabName, TabFont, ForeBrush, r, sf);

            sf.Dispose();




            if (e.Index == TaskBarRef.tabControl1.SelectedIndex)
            {
                TabFont.Dispose();
                BackBrush.Dispose();
            }
            else
            {
                BackBrush.Dispose();
                ForeBrush.Dispose();
            }
    }

【问题讨论】:

  • 观察:您同时使用TabFont 和ForeBrush,但您将它们排他性处理。这看起来根本不对!

标签: c#


【解决方案1】:

我会查看this codeproject 示例。它显示了如何为选项卡以及整个选定控件着色。

在您的示例中,您可以将 BackBrush 更改为 Solid 画笔并将其添加到 sf.Dispose 行之前:

TaskBarRef.tabControl1.TabPages[e.Index].BackColor = BackBrush.Color;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-07-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-29
    • 1970-01-01
    • 2021-04-02
    • 1970-01-01
    相关资源
    最近更新 更多