如何在Windows窗体中移动鼠标指针时突出显示TabControl选项卡页?

2018-10-01 c# highlight tabcontrol windows-forms-designer tabpage

我找到了一种更改背景色的解决方案,但选项卡突出显示没有任何帮助。 另外,我不明白这段代码在做什么:

private void tcSupportApp_MouseMove(object sender, MouseEventArgs e)
{
    Rectangle mouseRect = new Rectangle(e.X, e.Y, 1, 1);
    for (int i = 0; i < tcSupportApp.TabCount; i++)
    {
        if (tcSupportApp.GetTabRect(i).Contains(e.Location))
        {
            if (tcSupportApp.SelectedIndex != i)
            {
                tcSupportApp.SelectedIndex = i;

            }
        }
    }
}

Answers

为此,您必须执行以下任务:

  1. 将DrawMode属性更改为OwnerDrawFixed
  2. 使用以下代码将tcSupportApp_DrawItem事件处理程序添加到此控件中


private void tcSupportApp_DrawItem(object sender, DrawItemEventArgs e)
{
    TabPage SelectedTab = tcSupportApp.TabPages[e.Index];
    Rectangle HeaderRect = tcSupportApp.GetTabRect(e.Index);
    SolidBrush TextBrush = new SolidBrush(Color.Black);
    StringFormat sf = new StringFormat();
    sf.Alignment = StringAlignment.Center;
    sf.LineAlignment = StringAlignment.Center;
    if (e.State == DrawItemState.Selected)
    {
        Font BoldFont = new Font(tcSupportApp.Font.Name, tcSupportApp.Font.Size, FontStyle.Bold);
        e.Graphics.DrawString(SelectedTab.Text, BoldFont, TextBrush, HeaderRect, sf);
    }
    else
        e.Graphics.DrawString(SelectedTab.Text, e.Font, TextBrush, HeaderRect, sf);
    TextBrush.Dispose();
}

鸣谢: http : //vbcity.com/blogs/xtab/archive/2014/09/14/windows-forms-how-to-bold-the-header-of-a-selected-tab.aspx

如果创建从选项卡控件派生的自定义tabControl,则可以通过OnPaintBackground处理。在此代码段中,悬停时处于非活动状态的选项卡将用蓝色突出显示。

从TabControl派生

public partial class MyTabControl : TabControl
{
...

设置用户绘画的样式

public MyTabControl()
{
  InitializeComponent();
  //set style for user controlled painting
  this.SetStyle(ControlStyles.UserPaint |
                ControlStyles.AllPaintingInWmPaint | 
                ControlStyles.DoubleBuffer, true);
...

在绘制选项卡时,检查非活动选项卡上的当前鼠标位置

protected override void OnPaintBackground(PaintEventArgs pevent)
{
  using (SolidBrush backBrush = new SolidBrush(Color.FromArgb(250, 250, 250)))
  {
    Graphics g = pevent.Graphics;
    g.FillRectangle(backBrush, 0, 0, this.Size.Width, this.Size.Height); //paint background

    foreach (TabPage tp in this.TabPages)
    {
      int index = this.TabPages.IndexOf(tp);
      this.TabBoundary = this.GetTabRect(index);
      this.TabTextBoundary = (RectangleF)this.GetTabRect(index);
      bool isTabHover = false;

      if (this.TabBoundary.Contains(this.PointToClient(MousePosition)))
      {
        isTabHover = true;
      }
      if (tp == SelectedTab)
      {
        //paint active tab
        g.FillRectangle(backBrush, this.TabBoundary);
        g.FillRectangle(Brushes.Black, this.TabBoundary.X, this.TabBoundary.Y, this.TabBoundary.Width, 2);
        g.DrawString(tp.Text, _activeTabFont, Brushes.Black, this.TabTextBoundary, _tabStringFormat);
      }
      else
      {
        //paint inactive tab
        if (isTabHover)
        {
          g.FillRectangle(Brushes.LightBlue, this.TabBoundary)
        }
        else
        {
          g.FillRectangle(backBrush, this.TabBoundary);
        }
        g.DrawString(tp.Text, _inactiveTabFont, Brushes.Black, this.TabTextBoundary, _tabStringFormat);
      }
    }
  }
}

Related