开发者

Background color for Tabcontrol in c# windows application

I am trying to add font color and background color for tabs created in the tabcontrol and also background color for the place which is empty without tabs. Because my tabcontrol is stretched to the size of my full screen form. Say if only one tab is created in the tabcontrol, the tab background color should be in blue color and the rest place of tabcontrol should be in Dark blue color. The default color is grey for that, which is very ugly.

I am using the below code, which works fine for font color and background color of tab. But i am not getting the bg color for rest of the places.

am i missing anything ?

     private void tabControl1_DrawItem_1(object sender, DrawItemEventArgs e)
    {
            //set color for Background
            Brush BG_BackBrush = new SolidBrush(Color.FromArgb(111开发者_JAVA百科, 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();
            }
    }


I would check out this codeproject example. It shows how to color the tabs as well as the entire selected control.

In your example, you can change the BackBrush to a Solid brush and add this before the sf.Dispose line:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜