0

I have a TabControl with five Tabs and DrawMode set to OwnerDrawFixed. What I actually try to do is to change the font color of the last tab header from black to blue. The following code that handles the DrawItem-Event works fine and shows the also following tabs:

Code:

private void tabControl_DrawItem(object sender, DrawItemEventArgs e)
        {
            if (e.Index == 4)
            {                
                e.Graphics.DrawString("Notifications", new Font(tabControl.Font, FontStyle.Bold), new SolidBrush(SystemColors.HotTrack), new PointF(e.Bounds.X + 2, e.Bounds.Y + 3));
            }
            else
            {
                e.Graphics.DrawString(tabControl.TabPages[e.Index].Text, tabControl.Font, Brushes.Black, new PointF(e.Bounds.X + 5, e.Bounds.Y + 3));
            }
        }

GUI:

enter image description here

As soon as I click on one of the tabs, the header font itself changes a bit, it becomes a bit "thicker". I don't really know how to describe it, so here's a picture:

enter image description here

What I did here was clicking the first three tabs. I already know that it's because of the DrawItem-Event that paints over the already existing font.

So my question now is: Can I "reset" the tab header's font inside the handler so the font isn't painted over every time and becomes thicker when the event is fired?

I already tried using e.Graphics.Clear() (because I thought it would just "clear" the header), however this doesn't show the header title afterwards when I draw it using e.Graphics.DrawString().

Does anyone have an idea how to bypass this "process of overpainting"?

EDIT:

I figured out that the behavior only occurs when you set the TabControl's property Appearance to Buttons, as I did. The other values do not make the behavior occur.

So is there any possibility to bypass this when the Appearance property is set to Buttons?

Pete Hilde
  • 659
  • 1
  • 10
  • 24
  • I made this and am not getting the behavior you are seeing. – BugFinder Apr 25 '18 at 08:31
  • I figured out that the behavior only occurs when you set the `TabControl`'s property `Appearance` to `Buttons`, as I did. The other values do not make the behavior occur. Does it happen to you too? – Pete Hilde Apr 25 '18 at 08:40
  • 1
    try putting e.DrawBackground() in before your statement so it paints the underlying part - however.. for me this didnt paint it in the colour expected.. – BugFinder Apr 25 '18 at 08:51
  • Setting `e.Graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.SingleBitPerPixelGridFit;` seems to help. – TaW Apr 25 '18 at 09:47
  • That fixed it, thanks @TaW! – Pete Hilde Apr 25 '18 at 09:50
  • Also highly recommended: Using TextRenderer.DrawText instead of Graphics.DrawString !! It will work even without the rendering hint.. – TaW Apr 25 '18 at 10:00

0 Answers0