TabControl 標籤重繪

抄襲師姐的代碼,實現標籤選擇顏色變化,給標籤添加關閉標示。無法實現鼠標移到標籤上發生明顯變化。  

 public MainForm()

        {
            this.tabControlForInfoM.DrawItem += new DrawItemEventHandler(tabControlForInfoM_DrawItem);
            tabControlForInfoM.MouseDown += new System.Windows.Forms.MouseEventHandler(this.tabControlForInfoM_MouseDown);

        }

   /*****************************************************************************
       * 函數功能:重繪tabpage
       * 函數說明:如果 DrawMode 屬性設置爲 OwnerDrawFixed,則當 TabControl 需要繪製它的每一個選項卡時發生
       * **************************************************************************/
      private void tabControlForInfoM_DrawItem(object sender, DrawItemEventArgs e)
      {
                   
          try
          {
              Rectangle myTabRect = this.tabControlForInfoM.GetTabRect(e.Index);//返回該選項卡控件中的指定選項卡的邊框
              Rectangle origRect = myTabRect;
              Pen p = new Pen(Color.White);
              Color recColor = Color.WhiteSmoke;
              Brush b = new SolidBrush(recColor);


              e.Graphics.DrawRectangle(p, myTabRect);//畫邊框
              e.Graphics.FillRectangle(b, myTabRect);//填充矩形框內顏色
             
              //先添加TabPage 名稱
              e.Graphics.DrawString(this.tabControlForInfoM.TabPages[e.Index].Text
                  , this.Font, SystemBrushes.ControlText, myTabRect.X +18, myTabRect.Y + 8);


              if (e.Index == tabControlForInfoM.SelectedIndex)
              {
                  e.Graphics.FillRectangle(new SolidBrush(Color.Aqua), myTabRect);


                  //再畫一個矩形框
                  myTabRect.Offset(myTabRect.Width - (CLOSE_SIZE + 5), 5);
                  myTabRect.Width = CLOSE_SIZE;
                  myTabRect.Height = CLOSE_SIZE;
                  e.Graphics.DrawRectangle(p, myTabRect);
                  //填充矩形框
                  e.Graphics.FillRectangle(new SolidBrush(Color.DodgerBlue), myTabRect);


                  //畫關閉符號
                  using (Pen objpen = new Pen(Color.Black))
                  {
                      //"\"線
                      Point p1 = new Point(myTabRect.X + 3, myTabRect.Y + 3);
                      Point p2 = new Point(myTabRect.X + myTabRect.Width - 3, myTabRect.Y + myTabRect.Height - 3);
                      e.Graphics.DrawLine(objpen, p1, p2);


                      //"/"線
                      Point p3 = new Point(myTabRect.X + 3, myTabRect.Y + myTabRect.Height - 3);
                      Point p4 = new Point(myTabRect.X + myTabRect.Width - 3, myTabRect.Y + 3);
                      e.Graphics.DrawLine(objpen, p3, p4);
                  }
                  //寫標籤文字
                  e.Graphics.DrawString(this.tabControlForInfoM.TabPages[e.Index].Text, this.Font, SystemBrushes.ControlText, origRect.X + 18, origRect.Y + 8);
                  p.Dispose();
                  b.Dispose();
                  e.Graphics.Dispose();
              }
              TabPage newpag=tabControlForInfoM.TabPages[e.Index];
              if(newpag.Tag==(object)1)
              {
                  TabPage newp = tabControlForInfoM.TabPages[e.Index];
                  e.Graphics.FillRectangle(Brushes.Yellow, myTabRect);
                  e.Graphics.DrawString(this.tabControlForInfoM.TabPages[e.Index].Text, this.Font, SystemBrushes.ControlText, origRect.X + 18, origRect.Y + 8);
              }
          }
          catch (Exception ex)
          {
              MessageBox.Show(ex.Message);
          }
      }
      /*********************************************************************************************
       * 函數功能:處理tabcontrol鼠標按下事件
       * 函數說明:計算位置 實現關閉tabpage 關閉操作
       * *****************************************************************************************/
      private void tabControlForInfoM_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
      {
          if (e.Button == MouseButtons.Left)
          {
              int x = e.X, y = e.Y;
              //計算關閉區域  
              Rectangle myTabRect = tabControlForInfoM.GetTabRect(tabControlForInfoM.SelectedIndex);
              myTabRect.Offset(myTabRect.Width - (CLOSE_SIZE + 5), 5);
              myTabRect.Width = CLOSE_SIZE;
              myTabRect.Height = CLOSE_SIZE;


              ////如果鼠標在區域內就關閉選項卡  
          
            if (x > myTabRect.Left && x < myTabRect.Right && y > myTabRect.Top && y < myTabRect.Bottom)
            {
                tabControlForInfoM.TabPages.Remove(tabControlForInfoM.SelectedTab);
            }
             
          }
          if (tabControlForInfoM.TabPages.Count == 0)
          {
              tabControlForInfoM.Visible = false;
              webBrowserForFirm.Visible = true;
          }
      }


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章