Mavamaarten

Animated Tabcontrol: themed

May 26th, 2012
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 7.90 KB | None | 0 0
  1. Imports System.Drawing.Drawing2D
  2.  
  3. Class DotNetBarTabcontrol
  4.     Inherits TabControl
  5.  
  6.     Sub New()
  7.   SetStyle(ControlStyles.AllPaintingInWmPaint Or ControlStyles.ResizeRedraw Or ControlStyles.UserPaint Or ControlStyles.DoubleBuffer, True)
  8.   DoubleBuffered = True
  9.   SizeMode = TabSizeMode.Fixed
  10.   ItemSize = New Size(44, 136)
  11.     End Sub
  12.     Protected Overrides Sub CreateHandle()
  13.   MyBase.CreateHandle()
  14.   Alignment = TabAlignment.Left
  15.     End Sub
  16.  
  17.     Function ToPen(ByVal color As Color) As Pen
  18.   Return New Pen(color)
  19.     End Function
  20.  
  21.     Function ToBrush(ByVal color As Color) As Brush
  22.   Return New SolidBrush(color)
  23.     End Function
  24.  
  25.     Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
  26.   Dim B As New Bitmap(Width, Height)
  27.   Dim G As Graphics = Graphics.FromImage(B)
  28.   Try : SelectedTab.BackColor = Color.White : Catch : End Try
  29.   G.Clear(Color.White)
  30.   G.FillRectangle(New SolidBrush(Color.FromArgb(246, 248, 252)), New Rectangle(0, 0, ItemSize.Height + 4, Height))
  31.   G.DrawLine(New Pen(Color.FromArgb(170, 187, 204)), New Point(ItemSize.Height + 3, 0), New Point(ItemSize.Height + 3, 999))
  32.   For i = 0 To TabCount - 1
  33.     If i = SelectedIndex Then
  34.     Dim x2 As Rectangle = New Rectangle(New Point(GetTabRect(i).Location.X - 2, GetTabRect(i).Location.Y - 2), New Size(GetTabRect(i).Width + 3, GetTabRect(i).Height - 1))
  35.     Dim myBlend As New ColorBlend()
  36.     myBlend.Colors = {Color.FromArgb(232, 232, 240), Color.FromArgb(232, 232, 240), Color.FromArgb(232, 232, 240)}
  37.     myBlend.Positions = {0.0F, 0.5F, 1.0F}
  38.     Dim lgBrush As New LinearGradientBrush(x2, Color.Black, Color.Black, 90.0F)
  39.     lgBrush.InterpolationColors = myBlend
  40.     G.FillRectangle(lgBrush, x2)
  41.     G.DrawRectangle(New Pen(Color.FromArgb(170, 187, 204)), x2)
  42.  
  43.     G.SmoothingMode = SmoothingMode.HighQuality
  44.     Dim p() As 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)}
  45.     G.FillPolygon(Brushes.White, p)
  46.     G.DrawPolygon(New Pen(Color.FromArgb(170, 187, 204)), p)
  47.  
  48.     If ImageList IsNot Nothing Then
  49.     Try
  50.     If ImageList.Images(TabPages(i).ImageIndex) IsNot Nothing Then
  51.  
  52.     G.DrawImage(ImageList.Images(TabPages(i).ImageIndex), New Point(x2.Location.X + 8, x2.Location.Y + 6))
  53.     G.DrawString("  " & TabPages(i).Text, Font, Brushes.Black, x2, New StringFormat With {.LineAlignment = StringAlignment.Center, .Alignment = StringAlignment.Center})
  54.     Else
  55.     G.DrawString(TabPages(i).Text, New Font(Font.FontFamily, Font.Size, FontStyle.Bold), Brushes.Black, x2, New StringFormat With {.LineAlignment = StringAlignment.Center, .Alignment = StringAlignment.Center})
  56.     End If
  57.     Catch ex As Exception
  58.     G.DrawString(TabPages(i).Text, New Font(Font.FontFamily, Font.Size, FontStyle.Bold), Brushes.Black, x2, New StringFormat With {.LineAlignment = StringAlignment.Center, .Alignment = StringAlignment.Center})
  59.     End Try
  60.     Else
  61.     G.DrawString(TabPages(i).Text, New Font(Font.FontFamily, Font.Size, FontStyle.Bold), Brushes.Black, x2, New StringFormat With {.LineAlignment = StringAlignment.Center, .Alignment = StringAlignment.Center})
  62.     End If
  63.  
  64.     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))
  65.     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))
  66.     Else
  67.     Dim x2 As Rectangle = New Rectangle(New Point(GetTabRect(i).Location.X - 2, GetTabRect(i).Location.Y - 2), New Size(GetTabRect(i).Width + 3, GetTabRect(i).Height - 1))
  68.     G.FillRectangle(New SolidBrush(Color.FromArgb(246, 248, 252)), x2)
  69.     G.DrawLine(New Pen(Color.FromArgb(170, 187, 204)), New Point(x2.Right, x2.Top), New Point(x2.Right, x2.Bottom))
  70.     If ImageList IsNot Nothing Then
  71.     Try
  72.     If ImageList.Images(TabPages(i).ImageIndex) IsNot Nothing Then
  73.     G.DrawImage(ImageList.Images(TabPages(i).ImageIndex), New Point(x2.Location.X + 8, x2.Location.Y + 6))
  74.     G.DrawString("  " & TabPages(i).Text, Font, Brushes.DimGray, x2, New StringFormat With {.LineAlignment = StringAlignment.Center, .Alignment = StringAlignment.Center})
  75.     Else
  76.     G.DrawString(TabPages(i).Text, Font, Brushes.DimGray, x2, New StringFormat With {.LineAlignment = StringAlignment.Center, .Alignment = StringAlignment.Center})
  77.     End If
  78.     Catch ex As Exception
  79.     G.DrawString(TabPages(i).Text, Font, Brushes.DimGray, x2, New StringFormat With {.LineAlignment = StringAlignment.Center, .Alignment = StringAlignment.Center})
  80.     End Try
  81.     Else
  82.     G.DrawString(TabPages(i).Text, Font, Brushes.DimGray, x2, New StringFormat With {.LineAlignment = StringAlignment.Center, .Alignment = StringAlignment.Center})
  83.     End If
  84.     End If
  85.   Next
  86.  
  87.   e.Graphics.DrawImage(B.Clone, 0, 0)
  88.   G.Dispose() : B.Dispose()
  89.     End Sub
  90.  
  91.     Dim OldIndex As Integer
  92.  
  93.     Private _Speed As Integer = 5
  94.     Property Speed As Integer
  95.   Get
  96.     Return _Speed
  97.   End Get
  98.   Set(value As Integer)
  99.     If value > 20 Or value < -20 Then
  100.     MsgBox("Speed needs to be in between -20 and 20.")
  101.     Else
  102.     _Speed = value
  103.     End If
  104.   End Set
  105.     End Property
  106.  
  107.     Sub DoAnimationScrollLeft(ByVal Control1 As Control, Control2 As Control)
  108.   Dim G As Graphics = Control1.CreateGraphics()
  109.   Dim P1 As New Bitmap(Control1.Width, Control1.Height)
  110.   Dim P2 As New Bitmap(Control2.Width, Control2.Height)
  111.   Control1.DrawToBitmap(P1, New Rectangle(0, 0, Control1.Width, Control1.Height))
  112.   Control2.DrawToBitmap(P2, New Rectangle(0, 0, Control2.Width, Control2.Height))
  113.  
  114.   For Each c As Control In Control1.Controls
  115.     c.Hide()
  116.   Next
  117.  
  118.   Dim Slide As Integer = Control1.Width - (Control1.Width Mod _Speed)
  119.  
  120.   Dim a As Integer
  121.   For a = 0 To Slide Step _Speed
  122.     G.DrawImage(P1, New Rectangle(a, 0, Control1.Width, Control1.Height))
  123.     G.DrawImage(P2, New Rectangle(a - Control2.Width, 0, Control2.Width, Control2.Height))
  124.   Next
  125.   a = Control1.Width
  126.   G.DrawImage(P1, New Rectangle(a, 0, Control1.Width, Control1.Height))
  127.   G.DrawImage(P2, New Rectangle(a - Control2.Width, 0, Control2.Width, Control2.Height))
  128.  
  129.   SelectedTab = Control2
  130.  
  131.   For Each c As Control In Control2.Controls
  132.     c.Show()
  133.   Next
  134.  
  135.   For Each c As Control In Control1.Controls
  136.     c.Show()
  137.   Next
  138.     End Sub
  139.  
  140.     Protected Overrides Sub OnSelecting(e As System.Windows.Forms.TabControlCancelEventArgs)
  141.   If OldIndex < e.TabPageIndex Then
  142.     DoAnimationScrollRight(TabPages(OldIndex), TabPages(e.TabPageIndex))
  143.   Else
  144.     DoAnimationScrollLeft(TabPages(OldIndex), TabPages(e.TabPageIndex))
  145.   End If
  146.     End Sub
  147.  
  148.     Protected Overrides Sub OnDeselecting(e As System.Windows.Forms.TabControlCancelEventArgs)
  149.   OldIndex = e.TabPageIndex
  150.     End Sub
  151.  
  152.     Sub DoAnimationScrollRight(ByVal Control1 As Control, Control2 As Control)
  153.   Dim G As Graphics = Control1.CreateGraphics()
  154.   Dim P1 As New Bitmap(Control1.Width, Control1.Height)
  155.   Dim P2 As New Bitmap(Control2.Width, Control2.Height)
  156.   Control1.DrawToBitmap(P1, New Rectangle(0, 0, Control1.Width, Control1.Height))
  157.   Control2.DrawToBitmap(P2, New Rectangle(0, 0, Control2.Width, Control2.Height))
  158.  
  159.   For Each c As Control In Control1.Controls
  160.     c.Hide()
  161.   Next
  162.  
  163.   Dim Slide As Integer = Control1.Width - (Control1.Width Mod _Speed)
  164.  
  165.   Dim a As Integer
  166.   For a = 0 To -Slide Step -_Speed
  167.     G.DrawImage(P1, New Rectangle(a, 0, Control1.Width, Control1.Height))
  168.     G.DrawImage(P2, New Rectangle(a + Control2.Width, 0, Control2.Width, Control2.Height))
  169.   Next
  170.   a = Control1.Width
  171.   G.DrawImage(P1, New Rectangle(a, 0, Control1.Width, Control1.Height))
  172.   G.DrawImage(P2, New Rectangle(a + Control2.Width, 0, Control2.Width, Control2.Height))
  173.  
  174.   SelectedTab = Control2
  175.  
  176.   For Each c As Control In Control2.Controls
  177.     c.Show()
  178.   Next
  179.  
  180.   For Each c As Control In Control1.Controls
  181.     c.Show()
  182.   Next
  183.     End Sub
  184. End Class
Advertisement
Add Comment
Please, Sign In to add comment