Tab control Keren untuk VB.NET - Espada Fajar

Latest

buka mata buka telinga

Tab control Keren untuk VB.NET

assalamualaikum BLogger sejatii....
dan pembaca blog ini khususnya.......

ane mau bagiin source code untuk membuat tab control pada VB.NET nih, tentunya udah tahu kan caranya pasang code ini. yaitu dengan buat class.vb baru :D

ini penampakannya
lihat di link ini gambarnya http://puu.sh/5oZyT.gif


untuk source code :

' tabcontrol keren
' Creator: om Temploit
' This control may not be redistributed without permission

Class tabkeren
Inherits TabControl

Private SideOffset As Integer = 3
Private T As New Timer
Private TotalChangeTime As Double = 0.16
Private Shared TabColors As Color() = {Color.FromArgb(94, 168, 222), Color.FromArgb(141, 191, 53), Color.FromArgb(123, 79, 157), Color.FromArgb(247, 134, 106), Color.FromArgb(216, 201, 103)}
Private Shared UnSelected_Background As Color = Color.FromArgb(240, 241, 244)
Private Shared UnSelected_Underline As Color = Color.FromArgb(216, 217, 220)
Private Shared UnSelected_Font_Color As Color = Color.FromArgb(30, 30, 30)
Private Shared Selected_Font_Color As Color = Color.White

Public Sub New()
T.Enabled = True
T.Interval = 1
AddHandler T.Tick, AddressOf Tick
SetStyle(DirectCast(139286, ControlStyles), True)
SetStyle(ControlStyles.Selectable, False)
SizeMode = TabSizeMode.Fixed
Alignment = TabAlignment.Top
ItemSize = New Size(140, 41)
DrawMode = TabDrawMode.OwnerDrawFixed
Font = New Font("Segoe UI", 10)
End Sub

Private Shared Color1 As Color = TabColors(0)
Private Shared Color2 As Color = Color1
Protected Sub PaintTransparentBackground(graphics As Graphics, clipRect As Rectangle)
graphics.Clear(Color.Transparent)
If (Me.Parent IsNot Nothing) Then
clipRect.Offset(Me.Location)
Dim e As New PaintEventArgs(graphics, clipRect)
Dim state As Drawing2D.GraphicsState = graphics.Save()
graphics.SmoothingMode = Drawing2D.SmoothingMode.HighSpeed
Try
graphics.TranslateTransform(CSng(-Me.Location.X), CSng(-Me.Location.Y))
Me.InvokePaintBackground(Me.Parent, e)
Me.InvokePaint(Me.Parent, e)
Finally
graphics.Restore(state)
clipRect.Offset(-Me.Location.X, -Me.Location.Y)
End Try
End If
End Sub

Private Shared TotalHeaderCheckSum As String = ""
Protected Overrides Sub OnPaint(e As PaintEventArgs)
Dim B As New Bitmap(Width, Height)
Dim G As Graphics = Graphics.FromImage(B)
G.TextRenderingHint = Drawing.Text.TextRenderingHint.ClearTypeGridFit
Dim rect As New Rectangle()
rect.Height = Height
rect.Width = Width
PaintTransparentBackground(G, rect)
If TabPages.Count > 0 Then
Dim CompareS As String = ""
For i As Integer = 0 To TabPages.Count - 1
CompareS += TabPages(i).Text.ToUpper()
Next
If Not (TotalHeaderCheckSum = CompareS) Then
TotalHeaderCheckSum = CompareS
Dim BiggestItemSize As Integer = 140
For i As Integer = 0 To TabPages.Count - 1
BiggestItemSize = Math.Max(Convert.ToInt32(CreateGraphics().MeasureString(TabPages(i).Text.ToUpper(), Font).Width + 20), BiggestItemSize)
Next
If Not (ItemSize.Width = BiggestItemSize) Then
ItemSize = New Size(BiggestItemSize, 41)
End If
End If
For i As Integer = 0 To TabCount - 1
Dim TR As Rectangle = GetTabRect(i)
Dim DrawTR As New Rectangle(TR.X + SideOffset, TR.Y, TR.Width - (2 * SideOffset), 32)
G.FillRectangle(New SolidBrush(UnSelected_Background), DrawTR)
G.DrawRectangle(New Pen(UnSelected_Background), DrawTR)
For p As Integer = 0 To 2
G.DrawLine(New Pen(UnSelected_Underline), DrawTR.X, DrawTR.Y + DrawTR.Height - p, DrawTR.X + DrawTR.Width, DrawTR.Y + DrawTR.Height - p)
Next
Dim DrawString As String = TabPages(i).Text.ToUpper()
Dim DrawSize As SizeF = G.MeasureString(DrawString, Font)
G.DrawString(DrawString, Font, New SolidBrush(UnSelected_Font_Color), DrawTR.X + 10, DrawTR.Y + Convert.ToInt32(DrawTR.Height / 2 - DrawSize.Height / 2))
Next
Dim TR_2 As Rectangle = GetTabRect(RealIndex)
Dim DrawTR_2 As New Rectangle(TR_2.X + SideOffset + MarginX, TR_2.Y, TR_2.Width - (2 * SideOffset), 32)
Dim BGCol As Color = CombineColor(Color1, Color2, ColorRatio)
Dim BGLine As Color = ModColor(BGCol)
G.FillRectangle(New SolidBrush(BGCol), DrawTR_2)
G.DrawRectangle(New Pen(BGCol), DrawTR_2)
For p As Integer = 0 To 2
G.DrawLine(New Pen(BGLine), DrawTR_2.X, DrawTR_2.Y + DrawTR_2.Height - p, DrawTR_2.X + DrawTR_2.Width, DrawTR_2.Y + DrawTR_2.Height - p)
Next
Dim DrawString_2 As String = TabPages(SelectedIndex).Text.ToUpper()
Dim DrawSize_2 As SizeF = G.MeasureString(DrawString_2, Font)
G.DrawString(DrawString_2, Font, New SolidBrush(Selected_Font_Color), New Point(DrawTR_2.X + 10, DrawTR_2.Y + Convert.ToInt32(DrawTR_2.Height / 2 - DrawSize_2.Height / 2)))
Dim _X As Integer = DrawTR_2.X + 22
Dim _Y As Integer = 35
Dim GP As New Drawing2D.GraphicsPath(Drawing2D.FillMode.Alternate)
GP.AddLine(_X, _Y, _X + 9, _Y + 9)
GP.AddLine(_X + 9, _Y + 9, _X + 18, _Y)
GP.CloseFigure()
G.FillPath(New SolidBrush(BGLine), GP)
End If
e.Graphics.DrawImage(B, 0, 0)
G.Dispose()
B.Dispose()
End Sub

Public Function ModColor(c As Color) As Color
Return Color.FromArgb(Convert.ToInt32((If(c.R - 16 < 0, 0, c.R - 16))), Convert.ToInt32((If(c.G - 16 < 0, 0, c.G - 16))), Convert.ToInt32((If(c.B - 16 < 0, 0, c.B - 16))))
End Function

Public Function CombineColor(c1 As Color, c2 As Color, ratio As Double) As Color
Return Color.FromArgb(Convert.ToInt32((c1.R * (1 - ratio)) + (c2.R * ratio)), Convert.ToInt32((c1.G * (1 - ratio)) + (c2.G * ratio)), Convert.ToInt32((c1.B * (1 - ratio)) + (c2.B * ratio)))
End Function

Private CurVal As Double = 0
Private RealIndex As Integer = 0
Private MarginX As Integer = 0
Private PrevIndex As Integer = 0
Private GotoIndex As Integer = -1337
Public Sub StopTimer()
Try
T.Stop()
PrevIndex = GotoIndex
SelectedIndex = GotoIndex
TotalPixelMove = 0
MarginX = 0
RealIndex = GotoIndex
Catch ex As Exception
Console.WriteLine(ex.ToString())
End Try
End Sub

Private ColorRatio As Double = 0
Public Sub Tick(sender As Object, e As EventArgs)
Try
If CurVal > TotalChangeTime - 0.01 Then
StopTimer()
Else
CurVal += 0.01
MarginX = Convert.ToInt32((CurVal / TotalChangeTime) * TotalPixelMove)
ColorRatio = CurVal / TotalChangeTime
End If
Invalidate()
Catch ex As Exception
Console.WriteLine(ex.ToString())
End Try
End Sub

Private TotalPixelMove As Integer = 0
Public Sub ZoomTo(ind As Integer)
GotoIndex = ind
Dim OldTabRectTemp As Rectangle = GetTabRect(RealIndex)
Dim OldTabRect As New Rectangle(OldTabRectTemp.X + SideOffset, OldTabRectTemp.Y, OldTabRectTemp.Width - (2 * SideOffset), 32)
Dim NewTabRectTemp As Rectangle = GetTabRect(ind)
Dim NewTabRect As New Rectangle(NewTabRectTemp.X + SideOffset, NewTabRectTemp.Y, NewTabRectTemp.Width - (2 * SideOffset), 32)
TotalPixelMove = Convert.ToInt32((NewTabRect.X + (NewTabRect.Width / 2)) - (OldTabRect.X + (OldTabRect.Width / 2)))
CurVal = 0
Color1 = Color2
Color2 = TabColors(Convert.ToInt32(((ind + 1) - ((Math.Ceiling(CDbl(ind + 1) / TabColors.Length) - 1) * TabColors.Length))))
T.Start()
End Sub

Protected Overrides Sub OnSelectedIndexChanged(e As EventArgs)
T.Stop()
ZoomTo(SelectedIndex)
MyBase.OnSelectedIndexChanged(e)
End Sub
End Class




ok om. itu aja y :D

4 comments:

  1. gan .. mau tanya ini code di class. pemanggilannya gimana ? belum ada perubahan sepertinya jika untuk di class saja. mohon pencerahannya gan

    ReplyDelete
  2. untuk pemanggilannya. tekan f5 atau di run om.
    kemudian. close aplikasi yang dirun.
    terus lihat di toolbox component nya :)

    ReplyDelete
  3. gan klo di pasang di form1 sukses tapi klo dipasang di MDIParent ga runing tu solusinya gmna ya..??

    ReplyDelete
  4. bro kenapa ya cuma bisa 4 tab, mau tambah tab lagi error. gimana ya caranya?

    ReplyDelete