更改tabControl選項卡顏色

來源:互聯網
上載者:User

private void Form1_Load(object sender, EventArgs e)
        {
            this.tabControl1.DrawMode = System.Windows.Forms.TabDrawMode.OwnerDrawFixed;
            this.tabControl1.DrawItem += new DrawItemEventHandler(this.tabControl1_DrawItem);
        }
        private void tabControl1_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e)
        {
            StringFormat sf = new StringFormat();

            sf.LineAlignment = StringAlignment.Center;
            sf.Alignment = StringAlignment.Center;

            if (e.Index == tabControl1.SelectedIndex)

                e.Graphics.FillRectangle(Brushes.Red, e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height);

            else

                e.Graphics.FillRectangle(Brushes.White, e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height);

            e.Graphics.DrawString(((TabControl)sender).TabPages[e.Index].Text,

            System.Windows.Forms.SystemInformation.MenuFont, new SolidBrush(Color.Black), e.Bounds, sf);

        }

 

 

 

 

1.在Form類的建構函式中添加下列語句:
this.tabControl1.DrawMode = System.Windows.Forms.TabDrawMode.OwnerDrawFixed;
this.tabControl1.DrawItem += new DrawItemEventHandler(this.tabControl1_DrawItem);

2.實現下列函數:
         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;
             }
             else
             {
                 fntTab = e.Font;
                 bshBack = new SolidBrush(Color.Blue );
                 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);

         }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.