SIL.FieldWorks.Common.Widgets.VSTabControl.DrawTabItem C# (CSharp) Method

DrawTabItem() private method

Draws certain tab.
private DrawTabItem ( Graphics g, int index, TabItemState state, Rectangle tabRect, StringFormat textFmt ) : void
g System.Drawing.Graphics The object used to draw tab control.
index int Index of the tab being drawn.
state TabItemState State of the tab item.
tabRect System.Drawing.Rectangle The object specifying tab bounds.
textFmt System.Drawing.StringFormat The object specifying text formatting /// in the tab.
return void
		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);
				}
			}
		}

Usage Example

 protected override void WndProc(ref Message m)
 {
     //if native updown is destroyed we need release our hook
     if (m.Msg == NativeMethods.WM_DESTROY || m.Msg == NativeMethods.WM_NCDESTROY)
     {
         this.ReleaseHandle();
     }
     else if (m.Msg == NativeMethods.WM_WINDOWPOSCHANGING)
     {
         //When scroller position is changed we should remember that new position.
         x = ((NativeMethods.WINDOWPOS)m.GetLParam(typeof(NativeMethods.WINDOWPOS))).x;
     }
     else if (m.Msg == NativeMethods.WM_MOUSEMOVE && fparent.lastHotIndex > 0 &&
              fparent.lastHotIndex != fparent.SelectedIndex)
     {
         //owerdrawing former hot tab as normal
         using (Graphics context = Graphics.FromHwnd(fparent.Handle))
         {
             using (StringFormat textFormat = new StringFormat())
             {
                 textFormat.Alignment     = StringAlignment.Center;
                 textFormat.LineAlignment = StringAlignment.Center;
                 fparent.DrawTabItem(context, fparent.lastHotIndex, TabItemState.Normal,
                                     fparent.GetTabRect(fparent.lastHotIndex), textFormat);
                 if (fparent.lastHotIndex - fparent.SelectedIndex == 1)
                 {
                     Rectangle selRect = fparent.GetTabRect(fparent.SelectedIndex);
                     selRect.Inflate(2, 2);
                     fparent.DrawTabItem(context, fparent.SelectedIndex, TabItemState.Selected, selRect, textFormat);
                 }
             }
         }
     }
     else if (m.Msg == NativeMethods.WM_LBUTTONDOWN)
     {
         Rectangle invalidRect = fparent.GetTabRect(fparent.SelectedIndex);
         invalidRect.X = 0; invalidRect.Width = 2;
         invalidRect.Inflate(0, 2);
         fparent.Invalidate(invalidRect);
     }
     else if (m.Msg == NativeMethods.WM_SHOWWINDOW)
     {
         // If the scroll control for the labels is being hidden we need to stop
         // clipping the labels.
         // USUALLY we get WM_WINDOWPOSCHANGING with position 0 as well as this
         // message, but not while starting up a parent control that gets laid out
         // twice, first at a width that needs the scroll control and later at a width
         // that does not. See FWR-1794 for one case where this can be a problem.
         if (m.WParam.ToInt32() == 0)                     // window being hidden
         {
             x = 0;
         }
     }
     base.WndProc(ref m);
 }
All Usage Examples Of SIL.FieldWorks.Common.Widgets.VSTabControl::DrawTabItem