0

I have custom tab control where OnPaint method is override. Then strange growth of tabs occurs. Tabs getting bigger (padding getting bigger) and they width depends on length of the text. When I use default Tab Control - padding is OK. How to avoid this situation when I use UserPaint?

enter image description here

partial class Tab : TabControl
{
    public Tab()
    {
        InitializeComponent();
        Init();
    }

     private void Init()
    {
        this.SetStyle(ControlStyles.UserPaint, true);
        this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
        this.SetStyle(ControlStyles.DoubleBuffer, true);
        this.SetStyle(ControlStyles.ResizeRedraw, true);
    }
    protected override void OnPaint(PaintEventArgs e)
    {
       DrawTabPane(e.Graphics);
    }

    private void DrawTabPane(Graphics g)
{
       if (!Visible)
      return;

       // here we draw our tabs
       for (int i = 0; i < this.TabCount; i++)
         DrawTab(g, this.TabPages[i], i);
    }
    internal void DrawTab(Graphics g, TabPage tabPage, int nIndex)
    {
        Rectangle recBounds = this.GetTabRect(nIndex);
        RectangleF tabTextArea = recBounds;

        Point[] pt = new Point[4];
        pt[0] = new Point(recBounds.Left + 1, recBounds.Bottom);
        pt[1] = new Point(recBounds.Left + 1, recBounds.Top + 1);
        pt[2] = new Point(recBounds.Right - 1, recBounds.Top + 1);
        pt[3] = new Point(recBounds.Right - 1, recBounds.Bottom);

        Brush br = new SolidBrush(clr_tab_norm);
        g.FillPolygon(br, pt);
        br.Dispose();

        StringFormat stringFormat = new StringFormat();
        stringFormat.Alignment = StringAlignment.Center;
        stringFormat.LineAlignment = StringAlignment.Center;
        br = new SolidBrush(clr_txt);

        g.DrawString(tabPage.Text, Font, br, tabTextArea, stringFormat);
    }

}

3
  • 1
    Please provide the code of your overridden OnPaint method Commented May 3, 2011 at 8:23
  • When do they get bigger? I don't see it. Can you attach a picture of what you mean? Commented May 3, 2011 at 23:56
  • OK. See the picture in original message Commented May 4, 2011 at 4:35

2 Answers 2

2

Turning on ControlStyles.UserPaint for controls that are built into Windows, like TabControl, is not the proper thing to do. I assume the bug is in GetTabRect(), it isn't visible in the snippet.

Instead, you should use the TabControl.DrawMode property and implement the DrawItem event. There's a good example in the MSDN Library.

Sign up to request clarification or add additional context in comments.

1 Comment

DrawItem. It is a good solution but not for me, because focus rectagle is appear.
2

It would appear from the image that your code is setting the size of the tabs to be wider than they need to be. The extra padding is present in all your tabs but it is just more visible in the tabs with longer text.

I can't be sure why this is but I'd guess that the code to calculate the size of the tabs (based on font metrics) is using a different font from that used to draw the tabs.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.