如何更改Tab控件的背景颜色(在标签后面)?

时间:2019-04-14 10:10:20

标签: c# tabcontrol

我正在一个学校项目上,该项目是关于一个火车站的,该站点使用TabControl填充整个表格。到目前为止,我已经想到了诸如更改当前选项卡颜色或摆脱控件的Fixed3D方面之类的事情,但是我仍然不知道如何更改选项卡后面区域的颜色,该颜色始终是标准的灰色。 / p>

到目前为止,我试图通过将DrawMode属性设置为OwnerDrawFixed并使用代码来更改控件的外观。

private void tabControl1_DrawItem(object sender, DrawItemEventArgs e)
        {
            try
            {
                //This line of code will help you to change the apperance like 
                size,name,style.
                Font f;
                //For background color
                Brush backBrush;
                //For forground color
                Brush foreBrush;
                //This construct will hell you to deside which tab page have 
                current focus
                //to change the style.
                if (e.Index == this.tabControl1.SelectedIndex)
                {
                    //This line of code will help you to change the apperance 
               like size,name,style.
                    f = new Font(e.Font, FontStyle.Bold | FontStyle.Bold);
                    f = new Font(e.Font, FontStyle.Bold);
                    backBrush = new System.Drawing.SolidBrush(Color.DimGray);
                    foreBrush = Brushes.White;
                    Graphics g = e.Graphics;
                    Pen p = new Pen(Color.DimGray, 7);
                    g.DrawRectangle(p, this.tabPage1.Bounds);
                }
                else
                {
                    f = e.Font;
                    backBrush = new SolidBrush(e.BackColor);
                    foreBrush = new SolidBrush(e.ForeColor);
                }
                //To set the alignment of the caption.
                string tabName = this.tabControl1.TabPages[e.Index].Text;
                StringFormat sf = new StringFormat();
                sf.Alignment = StringAlignment.Center;
                //Thsi will help you to fill the interior portion of
                //selected tabpage.
                e.Graphics.FillRectangle(backBrush, e.Bounds);
                Rectangle r = e.Bounds;
                r = new Rectangle(r.X, r.Y + 3, r.Width, r.Height - 3);
                e.Graphics.DrawString(tabName, f, foreBrush, r, sf);
                sf.Dispose();
                if (e.Index == this.tabControl1.SelectedIndex)
                {
                    f.Dispose();
                    backBrush.Dispose();
                }
                else
                {
                    backBrush.Dispose();
                    foreBrush.Dispose();
                }
                //set color for Background
            }
            catch (Exception Ex)
            {
                MessageBox.Show(Ex.Message.ToString(), "Error Occured", 
                MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

我想将标签后面的区域设为黑色,但是无论我做什么,它都保持灰色。

0 个答案:

没有答案