I don't like TabControl
, they look ugly. So I'm creating my own custom drawn TabControl
. I have some problems with header height. my control look like below:
Control constructor:
this.SetStyle(ControlStyles.UserPaint, true);
this.SetStyle(Conbut trolStyles.OptimizedDoubleBuffer, true);
SetStyle(ControlStyles.AllPaintingInWmPaint, true);
this.DrawMode = TabDrawMode.OwnerDrawFixed;
And OnPaintBackground
:
var g = pevent.Graphics;
var Count = this.TabCount - 1;
int Offset = 2;
int index = 0;
foreach (TabPage itemtab in this.TabPages)
{
//SizeF sz = g.MeasureString(itemtab.Text, this.Font);
var tabrect = this.GetTabRect(index);
var headerrect = new Rectangle(tabrect.Left + Offset, tabrect.Y, tabrect.Width, tabrect.Height);
DrawRoundedRectangle(g, Headerbrush, headerrect, 3);
g.DrawString(itemtab.Text, this.Font, textbrush, headerrect, str);
if (index < Count) index++;
}
I tried to add this line:
this.ItemSize = new Size(100, 30);
But width returns wrong value when changing the font's size or the text of header.
Is there a way increase widths and height of header?
To have larger tabs you can use either of these options:
The control automatically provide enough room for text t be drawn using the Font
of TabControl
. You can simply add extra room, using Padding
property. The Padding
property specifies the amount of space around each item on the control's tab pages.
You can set ItemSize
to a larger size and set SizeMode
to Fixed
.
ItemSize.Width
doesn't have any impact on header width, unless you set SizeMode
to Fixed
. It's the reason for unexpected width which you see in your control.
Use the Graphics.MeasureString method to get the size (width & height) of the currently used font in order to correct the width & height of the custom drawn tab component.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With