private void DrawTabControlTabs(TabControl tabControl, DrawItemEventArgs e, ImageList images)
{
// Get the bounding end of tab strip rectangles.
var tabstripEndRect = tabControl.GetTabRect(tabControl.TabPages.Count - 1);
var tabstripEndRectF = new RectangleF(tabstripEndRect.X + tabstripEndRect.Width, tabstripEndRect.Y - 5,
tabControl.Width - (tabstripEndRect.X + tabstripEndRect.Width), tabstripEndRect.Height + 5);
var leftVerticalLineRect = new RectangleF(2, tabstripEndRect.Y + tabstripEndRect.Height + 2, 2, tabControl.TabPages[tabControl.SelectedIndex].Height + 2);
var rightVerticalLineRect = new RectangleF(tabControl.TabPages[tabControl.SelectedIndex].Width + 4, tabstripEndRect.Y + tabstripEndRect.Height + 2, 2, tabControl.TabPages[tabControl.SelectedIndex].Height + 2);
var bottomHorizontalLineRect = new RectangleF(2, tabstripEndRect.Y + tabstripEndRect.Height + tabControl.TabPages[tabControl.SelectedIndex].Height + 2, tabControl.TabPages[tabControl.SelectedIndex].Width + 4, 2);
RectangleF leftVerticalBarNearFirstTab = new Rectangle(0, 0, 2, tabstripEndRect.Height + 2);
// First, do the end of the tab strip.
// If we have an image use it.
if (tabControl.Parent.BackgroundImage != null)
{
var src = new RectangleF(tabstripEndRectF.X + tabControl.Left, tabstripEndRectF.Y + tabControl.Top, tabstripEndRectF.Width, tabstripEndRectF.Height);
e.Graphics.DrawImage(tabControl.Parent.BackgroundImage, tabstripEndRectF, src, GraphicsUnit.Pixel);
}
// If we have no image, use the background color.
else
{
using (var backBrush = new SolidBrush(tabControl.Parent.BackColor))
{
e.Graphics.FillRectangle(backBrush, tabstripEndRectF);
e.Graphics.FillRectangle(backBrush, leftVerticalLineRect);
e.Graphics.FillRectangle(backBrush, rightVerticalLineRect);
e.Graphics.FillRectangle(backBrush, bottomHorizontalLineRect);
if (mainTabControl.SelectedIndex != 0)
{
e.Graphics.FillRectangle(backBrush, leftVerticalBarNearFirstTab);
}
}
}
// Set up the page and the various pieces.
var page = tabControl.TabPages[e.Index];
using (var backBrush = new SolidBrush(page.BackColor))
{
using (var foreBrush = new SolidBrush(page.ForeColor))
{
var tabName = page.Text;
// Set up the offset for an icon, the bounding rectangle and image size and then fill the background.
var iconOffset = 0;
Rectangle tabBackgroundRect;
if (e.Index == mainTabControl.SelectedIndex)
{
tabBackgroundRect = e.Bounds;
e.Graphics.FillRectangle(backBrush, tabBackgroundRect);
}
else
{
tabBackgroundRect = new Rectangle(e.Bounds.X, e.Bounds.Y - 2, e.Bounds.Width, e.Bounds.Height + 4);
e.Graphics.FillRectangle(backBrush, tabBackgroundRect);
var rect = new Rectangle(e.Bounds.X - 2, e.Bounds.Y - 2, 1, 2);
e.Graphics.FillRectangle(backBrush, rect);
rect = new Rectangle(e.Bounds.X - 1, e.Bounds.Y - 2, 1, 2);
e.Graphics.FillRectangle(backBrush, rect);
rect = new Rectangle(e.Bounds.X + e.Bounds.Width, e.Bounds.Y - 2, 1, 2);
e.Graphics.FillRectangle(backBrush, rect);
rect = new Rectangle(e.Bounds.X + e.Bounds.Width + 1, e.Bounds.Y - 2, 1, 2);
e.Graphics.FillRectangle(backBrush, rect);
}
// If we have images, process them.
if (images != null)
{
// Get sice and image.
var size = images.ImageSize;
Image icon = null;
if (page.ImageIndex > -1)
icon = images.Images[page.ImageIndex];
else if (page.ImageKey != "")
icon = images.Images[page.ImageKey];
// If there is an image, use it.
if (icon != null)
{
var startPoint =
new Point(tabBackgroundRect.X + 2 + ((tabBackgroundRect.Height - size.Height) / 2),
tabBackgroundRect.Y + 2 + ((tabBackgroundRect.Height - size.Height) / 2));
e.Graphics.DrawImage(icon, new Rectangle(startPoint, size));
iconOffset = size.Width + 4;
}
}
// Draw out the label.
var labelRect = new Rectangle(tabBackgroundRect.X + iconOffset, tabBackgroundRect.Y + 5,
tabBackgroundRect.Width - iconOffset, tabBackgroundRect.Height - 3);
using (var sf = new StringFormat { Alignment = StringAlignment.Center })
{
e.Graphics.DrawString(tabName, new Font(e.Font.FontFamily, 8.25F, e.Font.Style), foreBrush, labelRect, sf);
}
}
}
}