private void DrawTabItem(Graphics g, int index, TabItemState state, Rectangle tabRect, StringFormat textFmt)
{
//if scroller is visible and the tab is fully placed under it we don't need to draw such tab
if (fUpDown.X <= 0 || tabRect.X < fUpDown.X)
{
/* We will draw our tab on the bitmap and then will transfer image on the control
* graphic context.*/
using (Bitmap bmp = new Bitmap(tabRect.Width, tabRect.Height))
{
Rectangle drawRect = new Rectangle(0, 0, tabRect.Width, tabRect.Height);
using (Graphics bitmapContext = Graphics.FromImage(bmp))
{
TabRenderer.DrawTabItem(bitmapContext, drawRect, state);
if (state == TabItemState.Selected && tabRect.X == 0)
{
int corrY = bmp.Height - 1;
bmp.SetPixel(0, corrY, bmp.GetPixel(0, corrY - 1));
}
/* Important moment. If tab alignment is bottom we should flip image to display tab
* correctly.*/
if (this.Alignment == TabAlignment.Bottom)
bmp.RotateFlip(RotateFlipType.RotateNoneFlipY);
Rectangle focusRect = Rectangle.Inflate(drawRect, -3, -3);
//focus rect
TabPage pg = this.TabPages[index];
//tab page whose tab we're drawing
//trying to get tab image if any
Image pagePict = GetImageByIndexOrKey(pg.ImageIndex, pg.ImageKey);
if (pagePict != null)
{
//If tab image is present we should draw it.
Point imgLoc = state == TabItemState.Selected ? new Point(8, 2) : new Point(6, 2);
if (this.Alignment == TabAlignment.Bottom)
imgLoc.Y = drawRect.Bottom - 2 - pagePict.Height;
bitmapContext.DrawImageUnscaled(pagePict, imgLoc);
//Correcting rectangle for drawing text.
drawRect.X += imgLoc.X + pagePict.Width;
drawRect.Width -= imgLoc.X + pagePict.Width;
}
//drawing tab text
using (Brush b = new SolidBrush(SystemColors.ControlText))
bitmapContext.DrawString(pg.Text, this.Font, b, (RectangleF)drawRect, textFmt);
//and finally drawing focus rect(if needed)
if (this.Focused && state == TabItemState.Selected)
ControlPaint.DrawFocusRectangle(bitmapContext, focusRect);
}
//If the tab has part under scroller we shouldn't draw that part.
int shift = state == TabItemState.Selected ? 2 : 0;
if (fUpDown.X > 0 && fUpDown.X >= tabRect.X - shift && fUpDown.X < tabRect.Right + shift)
tabRect.Width -= tabRect.Right - fUpDown.X + shift;
g.DrawImageUnscaledAndClipped(bmp, tabRect);
}
}
}