Raulp
Raulp

Reputation: 8136

Styling Windows Form Tab

I am creating windows Tabbed Application. Everything is good but the tabs are quiet faded and borders are very dull. I have tried changing the border style to 3D as well but no effect. Below is the screenshot

enter image description here

There are forums where people have suggested to use third party library to make Google Chrome type tabs. But I want the native way to get beautiful tabs.

Upvotes: 1

Views: 794

Answers (1)

Xavier
Xavier

Reputation: 1430

You can take control of how the tabs are drawn by setting the DrawMode = TabDrawMode.OwnerDrawFixed. The example below assumes you have a TabControl named tabControl1 on the form, this will add a new tab with a blue box.

private Rectangle myTabRect;
        private Rectangle myInsideRect;
        private Rectangle myOutsideRect;
        
        public Form1()
        {
            InitializeComponent();

            TabPage tabPage1 = new TabPage();

            // Sets the tabs to be drawn by the parent window Form1.
            // OwnerDrawFixed allows access to DrawItem. 
            tabControl1.DrawMode = TabDrawMode.OwnerDrawFixed;
            tabControl1.Controls.Add(tabPage1);
            tabControl1.Location = new Point(25, 25);
            tabControl1.Size = new Size(250, 250);
            tabPage1.TabIndex = 0;

            myTabRect = tabControl1.GetTabRect(0);
            myInsideRect = new Rectangle(tabControl1.DisplayRectangle.X -1, tabControl1.DisplayRectangle.Y -1, tabControl1.DisplayRectangle.Width + 1, tabControl1.DisplayRectangle.Height + 1);
            myOutsideRect = tabControl1.ClientRectangle;
            myOutsideRect.Width--;
            myOutsideRect.Height--;

            ClientSize = new Size(300, 500);
            Controls.Add(tabControl1);
            tabControl1.DrawItem += new DrawItemEventHandler(OnDrawItem);
        }

        private void OnDrawItem(object sender, DrawItemEventArgs e)
        {
            // Draw your tab graphics here
            Graphics g = e.Graphics;
            Pen p = new Pen(Color.Blue);
            g.DrawRectangle(p, myTabRect);

            p = new Pen(Color.Red);
            g.DrawRectangle(p, myInsideRect);

            p = new Pen(Color.Green);
            g.DrawRectangle(p, myOutsideRect);
            
        }

You can draw whatever style you like into the graphic context, add text, images, etc

enter image description here

Upvotes: 1

Related Questions