Guillaume17

[VB.net] TabControl animated

Jun 3rd, 2012
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 3.21 KB | None | 0 0
  1. Public Class AnimTab
  2.     Inherits TabControl
  3.     Dim OldIndex As Integer
  4.  
  5.     Private _Speed As Integer = 9
  6.     Property Speed As Integer
  7.   Get
  8.     Return _Speed
  9.   End Get
  10.   Set(value As Integer)
  11.     If value > 20 Or value < -20 Then
  12.     MsgBox("Speed needs to be in between -20 and 20.")
  13.     Else
  14.     _Speed = value
  15.     End If
  16.   End Set
  17.     End Property
  18.  
  19.     Sub New()
  20.   SetStyle(ControlStyles.AllPaintingInWmPaint Or ControlStyles.OptimizedDoubleBuffer Or ControlStyles.ResizeRedraw, True)
  21.     End Sub
  22.  
  23.     Sub DoAnimationScrollLeft(ByVal Control1 As Control, Control2 As Control)
  24.   Dim G As Graphics = Control1.CreateGraphics()
  25.   Dim P1 As New Bitmap(Control1.Width, Control1.Height)
  26.   Dim P2 As New Bitmap(Control2.Width, Control2.Height)
  27.   Control1.DrawToBitmap(P1, New Rectangle(0, 0, Control1.Width, Control1.Height))
  28.   Control2.DrawToBitmap(P2, New Rectangle(0, 0, Control2.Width, Control2.Height))
  29.  
  30.   For Each c As Control In Control1.Controls
  31.     c.Hide()
  32.   Next
  33.  
  34.   Dim Slide As Integer = Control1.Width - (Control1.Width Mod _Speed)
  35.  
  36.   Dim a As Integer
  37.   For a = 0 To Slide Step _Speed
  38.     G.DrawImage(P1, New Rectangle(a, 0, Control1.Width, Control1.Height))
  39.     G.DrawImage(P2, New Rectangle(a - Control2.Width, 0, Control2.Width, Control2.Height))
  40.   Next
  41.   a = Control1.Width
  42.   G.DrawImage(P1, New Rectangle(a, 0, Control1.Width, Control1.Height))
  43.   G.DrawImage(P2, New Rectangle(a - Control2.Width, 0, Control2.Width, Control2.Height))
  44.  
  45.   SelectedTab = Control2
  46.  
  47.   For Each c As Control In Control2.Controls
  48.     c.Show()
  49.   Next
  50.  
  51.   For Each c As Control In Control1.Controls
  52.     c.Show()
  53.   Next
  54.     End Sub
  55.  
  56.     Protected Overrides Sub OnSelecting(e As System.Windows.Forms.TabControlCancelEventArgs)
  57.   If OldIndex < e.TabPageIndex Then
  58.     DoAnimationScrollRight(TabPages(OldIndex), TabPages(e.TabPageIndex))
  59.   Else
  60.     DoAnimationScrollLeft(TabPages(OldIndex), TabPages(e.TabPageIndex))
  61.   End If
  62.     End Sub
  63.  
  64.     Protected Overrides Sub OnDeselecting(e As System.Windows.Forms.TabControlCancelEventArgs)
  65.   OldIndex = e.TabPageIndex
  66.     End Sub
  67.  
  68.     Sub DoAnimationScrollRight(ByVal Control1 As Control, Control2 As Control)
  69.   Dim G As Graphics = Control1.CreateGraphics()
  70.   Dim P1 As New Bitmap(Control1.Width, Control1.Height)
  71.   Dim P2 As New Bitmap(Control2.Width, Control2.Height)
  72.   Control1.DrawToBitmap(P1, New Rectangle(0, 0, Control1.Width, Control1.Height))
  73.   Control2.DrawToBitmap(P2, New Rectangle(0, 0, Control2.Width, Control2.Height))
  74.  
  75.   For Each c As Control In Control1.Controls
  76.     c.Hide()
  77.   Next
  78.  
  79.   Dim Slide As Integer = Control1.Width - (Control1.Width Mod _Speed)
  80.  
  81.   Dim a As Integer
  82.   For a = 0 To -Slide Step -_Speed
  83.     G.DrawImage(P1, New Rectangle(a, 0, Control1.Width, Control1.Height))
  84.     G.DrawImage(P2, New Rectangle(a + Control2.Width, 0, Control2.Width, Control2.Height))
  85.   Next
  86.   a = Control1.Width
  87.   G.DrawImage(P1, New Rectangle(a, 0, Control1.Width, Control1.Height))
  88.   G.DrawImage(P2, New Rectangle(a + Control2.Width, 0, Control2.Width, Control2.Height))
  89.  
  90.   SelectedTab = Control2
  91.  
  92.   For Each c As Control In Control2.Controls
  93.     c.Show()
  94.   Next
  95.  
  96.   For Each c As Control In Control1.Controls
  97.     c.Show()
  98.   Next
  99.     End Sub
  100.  
  101. End Class
Add Comment
Please, Sign In to add comment