Ethernal_Five

[C#] Tabcontrol by Mavamaarten

Mar 10th, 2012
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.20 KB | None | 0 0
  1. using System;
  2. using System.Drawing;
  3. using System.Windows.Forms;
  4. using System.Drawing.Drawing2D;
  5.  
  6. public class DotNetBarTabcontrol : TabControl
  7. {
  8.     public DotNetBarTabcontrol()
  9.     {
  10.         SetStyle(ControlStyles.AllPaintingInWmPaint, true);
  11.         SetStyle(ControlStyles.ResizeRedraw, true);
  12.         SetStyle(ControlStyles.UserPaint, true);
  13.         SetStyle(ControlStyles.DoubleBuffer, true);
  14.         DoubleBuffered = true;
  15.         SizeMode = TabSizeMode.Fixed;
  16.         ItemSize = new Size(44, 136);
  17.     }
  18.  
  19.     protected override void CreateHandle()
  20.     {
  21.         base.CreateHandle();
  22.         Alignment = TabAlignment.Left;
  23.     }
  24.  
  25.     Pen ToPen(Color color)
  26.     {
  27.         return new Pen(color);
  28.     }
  29.  
  30.     Brush ToBrush(Color color)
  31.     {
  32.         return new SolidBrush(color);
  33.     }
  34.  
  35.     protected override void OnPaint(PaintEventArgs e)
  36.     {
  37.         Bitmap B = new Bitmap(Width, Height);
  38.         Graphics G = Graphics.FromImage(B);
  39.  
  40.         try
  41.         {
  42.             SelectedTab.BackColor = Color.White;
  43.         } catch { }
  44.  
  45.         G.Clear(Color.White);
  46.         StringFormat sf = new StringFormat();
  47.         sf.Alignment = StringAlignment.Center;
  48.         sf.LineAlignment = StringAlignment.Center;
  49.         G.FillRectangle(new SolidBrush(Color.FromArgb(218, 231, 244)), new Rectangle(0, 0, (ItemSize.Height + 4), Height));
  50.         G.DrawLine(new Pen(Color.FromArgb(170, 187, 204)), new Point((Width - 1), 0), new Point((Width - 1), (Height - 1)));
  51.         // comment out to get rid of the borders
  52.         G.DrawLine(new Pen(Color.FromArgb(170, 187, 204)), new Point((ItemSize.Height + 1), 0), new Point((Width - 1), 0));
  53.         // comment out to get rid of the borders
  54.         G.DrawLine(new Pen(Color.FromArgb(170, 187, 204)), new Point((ItemSize.Height + 3), (Height - 1)), new Point((Width - 1), (Height - 1)));
  55.         // comment out to get rid of the borders
  56.         G.DrawLine(new Pen(Color.FromArgb(170, 187, 204)), new Point((ItemSize.Height + 3), 0), new Point((ItemSize.Height + 3), 999));
  57.         for (int i = 0; (i <= (TabCount - 1)); i++)
  58.         {
  59.             if ((i == SelectedIndex))
  60.             {
  61.                 Rectangle x2 = new Rectangle(new Point((GetTabRect(i).Location.X - 2), (GetTabRect(i).Location.Y - 2)), new Size((GetTabRect(i).Width + 3), (GetTabRect(i).Height - 1)));
  62.                 ColorBlend myBlend = new ColorBlend();
  63.                 myBlend.Colors = new Color[] { Color.FromArgb(38, 102, 192), Color.FromArgb(71, 134, 220), Color.FromArgb(38, 102, 192) };
  64.                 myBlend.Positions = new float[] { 0.0F, 0.5F, 1.0F};
  65.                 LinearGradientBrush lgBrush = new LinearGradientBrush(x2, Color.Black, Color.Black, 90.0F);
  66.                 lgBrush.InterpolationColors = myBlend;
  67.                 G.FillRectangle(lgBrush, x2);
  68.                 G.DrawRectangle(new Pen(Color.FromArgb(12, 63, 149)), x2);
  69.                 G.SmoothingMode = SmoothingMode.HighQuality;
  70.                 Point[] p = new Point[] { new Point((ItemSize.Height - 3), (GetTabRect(i).Location.Y + 20)), new Point((ItemSize.Height + 4), (GetTabRect(i).Location.Y + 14)), new Point((ItemSize.Height + 4), (GetTabRect(i).Location.Y + 27)) };
  71.                 G.FillPolygon(Brushes.White, p);
  72.                 if (ImageList != null)
  73.                 {
  74.                     try
  75.                     {
  76.                         if (ImageList.Images[TabPages[i].ImageIndex] != null)
  77.                         {
  78.                             G.DrawImage(ImageList.Images[TabPages[i].ImageIndex], new Point((x2.Location.X + 8), (x2.Location.Y + 6)));
  79.                             G.DrawString(("      " + TabPages[i].Text), new Font(Font.FontFamily, Font.Size, FontStyle.Bold), Brushes.White, x2, sf);
  80.                         }
  81.                         else
  82.                         {
  83.                             G.DrawString(TabPages[i].Text, new Font(Font.FontFamily, Font.Size, FontStyle.Bold), Brushes.White, x2, sf);
  84.                         }
  85.                     }
  86.                     catch
  87.                     {
  88.                         G.DrawString(TabPages[i].Text, new Font(Font.FontFamily, Font.Size, FontStyle.Bold), Brushes.White, x2, sf);
  89.                     }
  90.                 }
  91.                 else
  92.                 {
  93.                     G.DrawString(TabPages[i].Text, new Font(Font.FontFamily, Font.Size, FontStyle.Bold), Brushes.White, x2, sf);
  94.                 }
  95.                 G.DrawLine(new Pen(Color.FromArgb(200, 200, 250)), new Point((x2.Location.X - 1), (x2.Location.Y - 1)), new Point(x2.Location.X, x2.Location.Y));
  96.                 G.DrawLine(new Pen(Color.FromArgb(200, 200, 250)), new Point((x2.Location.X - 1), (x2.Bottom - 1)), new Point(x2.Location.X, x2.Bottom));
  97.             }
  98.             else
  99.             {
  100.                 Rectangle x2 = new Rectangle(new Point((GetTabRect(i).Location.X - 2), (GetTabRect(i).Location.Y - 2)), new Size((GetTabRect(i).Width + 3), (GetTabRect(i).Height + 1)));
  101.                 G.FillRectangle(new SolidBrush(Color.FromArgb(218, 231, 244)), x2);
  102.                 G.DrawLine(new Pen(Color.FromArgb(170, 187, 204)), new Point(x2.Right, x2.Top), new Point(x2.Right, x2.Bottom));
  103.                 if (ImageList != null)
  104.                 {
  105.                     try
  106.                     {
  107.                         if (ImageList.Images[TabPages[i].ImageIndex] != null)
  108.                         {
  109.                             G.DrawImage(ImageList.Images[TabPages[i].ImageIndex], new Point((x2.Location.X + 8), (x2.Location.Y + 6)));
  110.                             G.DrawString(("      " + TabPages[i].Text), Font, Brushes.Black, x2, sf);
  111.                         }
  112.                         else
  113.                         {
  114.                             G.DrawString(TabPages[i].Text, Font, Brushes.Black, x2, sf);
  115.                         }
  116.                     }
  117.                     catch
  118.                     {
  119.                         G.DrawString(TabPages[i].Text, Font, Brushes.Black, x2, sf);
  120.                     }
  121.                 }
  122.                 else
  123.                 {
  124.                     G.DrawString(TabPages[i].Text, Font, Brushes.Black, x2, sf);
  125.                 }
  126.             }
  127.         }
  128.         e.Graphics.DrawImage(B, 0, 0);
  129.         G.Dispose();
  130.         B.Dispose();
  131.     }
  132. }
Advertisement
Add Comment
Please, Sign In to add comment