Дополнительный отступ в тексте заголовка вкладки TabControl - PullRequest
1 голос
/ 17 июня 2020

Я хочу создать собственный TabControl, но есть проблема с текстом заголовка вкладки. По какой-то причине, когда я устанавливаю стили для пользовательской раскраски: ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint кажется, что после текста в заголовке вкладки есть дополнительный отступ. You can see that there is extra padding after the tab text.

Для короткого текста табуляции заполнение подходит. Проблема в более длинных. Как убрать эту лишнюю обивку? Заполнение TabControl установлено на 0.

Вот мой код:

using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Text;
using System.Windows.Forms;

public class NewTabControl : TabControl
{
  public NewTabControl()
  {
    SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint |
             ControlStyles.ResizeRedraw | ControlStyles.OptimizedDoubleBuffer,
             true);
    DoubleBuffered = true;
  }

  protected override void CreateHandle()
  {
    base.CreateHandle();
    Alignment = TabAlignment.Top;
  }

  protected override void OnPaint(PaintEventArgs e)
  {
    var g = e.Graphics;
    var Drawer = g;

    Drawer.SmoothingMode = SmoothingMode.HighQuality;
    Drawer.PixelOffsetMode = PixelOffsetMode.HighQuality;
    Drawer.TextRenderingHint = TextRenderingHint.SystemDefault;
    Drawer.Clear(Color.Gainsboro);

    for (var i = 0; i <= TabCount - 1; i++)
    {
      var Header = new Rectangle(new Point(GetTabRect(i).Location.X + 2, 
                                 GetTabRect(i).Location.Y),
                                 new Size(GetTabRect(i).Width, GetTabRect(i).Height));
      var HeaderSize = new Rectangle(Header.Location, new Size(Header.Width, Header.Height));

      if (i == SelectedIndex)
      {
        Drawer.FillRectangle(new SolidBrush(Color.Gainsboro), HeaderSize);

        Drawer.FillRectangle(new SolidBrush(Color.White),
                             new Rectangle(Header.X - 5, Header.Y - 3, Header.Width, Header.Height + 5));

        Drawer.DrawString(TabPages[i].Text, Font,
                          new SolidBrush(Color.Black),
                          new Rectangle(Header.Location, new Size(Header.Width, Header.Height)));

      }
      else
      {
        Drawer.DrawString(TabPages[i].Text, Font,
                          new SolidBrush(Color.Black), new Rectangle(Header.Location, new Size(Header.Width, Header.Height)));
      }
    }

    Drawer.FillRectangle(new SolidBrush(Color.WhiteSmoke), new Rectangle(0, 20, Width - 80, Height - 20));

    Drawer.DrawRectangle(new Pen(Color.WhiteSmoke, 2), new Rectangle(0, 0, Width, Height));
    Drawer.InterpolationMode = InterpolationMode.HighQualityBicubic;
  }
}
...