Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Increase Width and Height of Headers in TabControl

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:

enter image description here

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.

enter image description here

Is there a way increase widths and height of header?

like image 995
Jandy Avatar asked Dec 11 '22 15:12

Jandy


2 Answers

To have larger tabs you can use either of these options:

  1. 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.

  2. 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.

like image 192
Reza Aghaei Avatar answered Dec 30 '22 23:12

Reza Aghaei


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.

like image 33
keenthinker Avatar answered Dec 31 '22 00:12

keenthinker