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;
          }
      }


  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值