简体   繁体   中英

Change the Tab size of tabControl

I redraw the graphics of the Tab for the TabControl but I can't set the Width of it.

What I want is that the text of the Tab and the icon is contained in its size.

Now is something like this:

标签被覆盖

My Code

private void tabControlForm_DrawItem(object sender, DrawItemEventArgs e)
{

   try
   {
       using (Brush br = new SolidBrush(TabColors[tabControlForm.TabPages[e.Index]]))
       {
           Rectangle rect = e.Bounds;

           rect.Width += 10;

           e.Graphics.FillRectangle(br, rect);
           SizeF sz = e.Graphics.MeasureString(tabControlForm.TabPages[e.Index].Text, e.Font);
           e.Graphics.DrawString(tabControlForm.TabPages[e.Index].Text, e.Font, Brushes.Black, rect.Left + (rect.Width - sz.Width) / 2, rect.Top + (rect.Height - sz.Height) / 2 + 1);

           using (var src = new Bitmap(System.Reflection.Assembly.GetEntryAssembly().GetManifestResourceStream("Castor.Gestionale.images.close-button.png")))
           {
              e.Graphics.DrawImage(src, rect.Right - 18, rect.Top + 10);
           }

           e.Graphics.DrawRectangle(Pens.DarkGray, rect);
           e.DrawFocusRectangle();
       }
   }
   catch {}
}

Thanks

Actually you can set the size of the tabs, but not individually.

The combination of SizeMode = Fixed and some suitable value for the TabControl.Itemsize will create any size, but always the same..:

在此处输入图片说明

So for individually enlarging each tab to make the icon fit you indeed need to use Ian's 'spacey' method..

Unfortunately, there isn't built-in property to control the width of the TabPages' tab header of the TabControl individually (Edit: apparently, there is TabControl.ItemSize to control it collectively. See TaW's answer to fix the width of all tab pages under a tab control).

But a little trick you could do is to give additional spaces in the left or in the right of the TabPage.Text to give you enough space for your icon.

Without space:

在此处输入图片说明

With 7 spaces:

在此处输入图片说明

It should be enough to put your icon

Try increasing "myTabControl.Padding.X". It works for me!

Use it...

private void FrmSqlMain_Load(object sender, EventArgs e)
 {
     myTabControl.SizeMode = TabSizeMode.Normal;
     myTabControl.DrawMode = TabDrawMode.OwnerDrawFixed;
 }

Also i would like to add one more thing. At first, I tried adding spaces but it didn't seem like working for me. Later, I realized I had to add more spaces than usual to see the difference.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM