Advertisement
Mavamaarten

MetroControls for VB.Net by Mavamaarten

Jun 5th, 2012
2,269
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 17.47 KB | None | 0 0
  1. Public Class MetroSlideControl
  2.     Inherits TabControl
  3.     Dim OldIndex As Integer
  4.  
  5.     Private _Speed As Integer = 15
  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 DrawPanel(ByVal Panel As Panel)
  24.         Dim bitmap As New Bitmap(Panel.Width, Panel.Height)
  25.         Panel.DrawToBitmap(bitmap, New Rectangle(0, 0, Panel.Width, Panel.Height))
  26.         CreateGraphics.DrawImage(bitmap, New Point(0, 0))
  27.     End Sub
  28.  
  29.     Sub DoAnimationScrollLeft(ByVal Control1 As Control, Control2 As Control)
  30.         Dim G As Graphics = Control1.CreateGraphics()
  31.         Dim P1 As New Bitmap(Control1.Width, Control1.Height)
  32.         Dim P2 As New Bitmap(Control2.Width, Control2.Height)
  33.         Control1.DrawToBitmap(P1, New Rectangle(0, 0, Control1.Width, Control1.Height))
  34.         Control2.DrawToBitmap(P2, New Rectangle(0, 0, Control2.Width, Control2.Height))
  35.  
  36.         For Each c As Control In Control1.Controls
  37.             c.Hide()
  38.         Next
  39.  
  40.         Dim Slide As Integer = Control1.Width - (Control1.Width Mod _Speed)
  41.  
  42.         Dim a As Integer
  43.         For a = 0 To Slide Step _Speed
  44.             G.DrawImage(P1, New Rectangle(a, 0, Control1.Width, Control1.Height))
  45.             G.DrawImage(P2, New Rectangle(a - Control2.Width, 0, Control2.Width, Control2.Height))
  46.         Next
  47.         a = Control1.Width
  48.         G.DrawImage(P1, New Rectangle(a, 0, Control1.Width, Control1.Height))
  49.         G.DrawImage(P2, New Rectangle(a - Control2.Width, 0, Control2.Width, Control2.Height))
  50.  
  51.         SelectedTab = Control2
  52.  
  53.         For Each c As Control In Control2.Controls
  54.             c.Show()
  55.         Next
  56.  
  57.         For Each c As Control In Control1.Controls
  58.             c.Show()
  59.         Next
  60.     End Sub
  61.  
  62.     Protected Overrides Sub OnSelecting(e As System.Windows.Forms.TabControlCancelEventArgs)
  63.         If OldIndex < e.TabPageIndex Then
  64.             DoAnimationScrollRight(TabPages(OldIndex), TabPages(e.TabPageIndex))
  65.         Else
  66.             DoAnimationScrollLeft(TabPages(OldIndex), TabPages(e.TabPageIndex))
  67.         End If
  68.     End Sub
  69.  
  70.     Protected Overrides Sub OnDeselecting(e As System.Windows.Forms.TabControlCancelEventArgs)
  71.         OldIndex = e.TabPageIndex
  72.     End Sub
  73.  
  74.     Sub DoAnimationScrollRight(ByVal Control1 As Control, Control2 As Control)
  75.         Dim G As Graphics = Control1.CreateGraphics()
  76.         Dim P1 As New Bitmap(Control1.Width, Control1.Height)
  77.         Dim P2 As New Bitmap(Control2.Width, Control2.Height)
  78.         Control1.DrawToBitmap(P1, New Rectangle(0, 0, Control1.Width, Control1.Height))
  79.         Control2.DrawToBitmap(P2, New Rectangle(0, 0, Control2.Width, Control2.Height))
  80.  
  81.         For Each c As Control In Control1.Controls
  82.             c.Hide()
  83.         Next
  84.  
  85.         Dim Slide As Integer = Control1.Width - (Control1.Width Mod _Speed)
  86.  
  87.         Dim a As Integer
  88.         For a = 0 To -Slide Step -_Speed
  89.             G.DrawImage(P1, New Rectangle(a, 0, Control1.Width, Control1.Height))
  90.             G.DrawImage(P2, New Rectangle(a + Control2.Width, 0, Control2.Width, Control2.Height))
  91.         Next
  92.         a = Control1.Width
  93.         G.DrawImage(P1, New Rectangle(a, 0, Control1.Width, Control1.Height))
  94.         G.DrawImage(P2, New Rectangle(a + Control2.Width, 0, Control2.Width, Control2.Height))
  95.  
  96.         SelectedTab = Control2
  97.  
  98.         For Each c As Control In Control2.Controls
  99.             c.Show()
  100.         Next
  101.  
  102.         For Each c As Control In Control1.Controls
  103.             c.Show()
  104.         Next
  105.     End Sub
  106.  
  107.     Public Sub NextPage()
  108.         If SelectedIndex < TabPages.Count - 1 Then SelectedIndex += 1
  109.     End Sub
  110.  
  111.     Public Sub PreviousPage()
  112.         If SelectedIndex > 0 Then SelectedIndex -= 1
  113.     End Sub
  114.  
  115.     Protected Overrides Sub WndProc(ByRef m As Message)
  116.         If m.Msg = &H1328 Then
  117.             m.Result = CType(1, IntPtr)
  118.         Else
  119.             MyBase.WndProc(m)
  120.         End If
  121.     End Sub
  122.  
  123. End Class
  124.  
  125. Public Class MetroPanel
  126.     Inherits Panel
  127.  
  128.     Private _TitleText As String = "Title"
  129.     Property Title As String
  130.         Get
  131.             Return _TitleText
  132.         End Get
  133.         Set(value As String)
  134.             _TitleText = value
  135.             Invalidate()
  136.         End Set
  137.     End Property
  138.  
  139.     Private _SubTitleText As String = "Subtitle"
  140.     Property SubTitle As String
  141.         Get
  142.             Return _SubTitleText
  143.         End Get
  144.         Set(value As String)
  145.             _SubTitleText = value
  146.             Invalidate()
  147.         End Set
  148.     End Property
  149.  
  150.     Private _TitleFont As Font = New Font("Segoe UI Light", 16)
  151.     Property TitleFont As Font
  152.         Get
  153.             Return _TitleFont
  154.         End Get
  155.         Set(value As Font)
  156.             _TitleFont = value
  157.         End Set
  158.     End Property
  159.  
  160.     Private _SubTitleFont As Font = New Font("Segoe UI", 9)
  161.     Property SubTitleFont As Font
  162.         Get
  163.             Return _SubTitleFont
  164.         End Get
  165.         Set(value As Font)
  166.             _SubTitleFont = value
  167.         End Set
  168.     End Property
  169.  
  170.     Private _DrawBorders As Boolean
  171.     Property DrawBorders As Boolean
  172.         Get
  173.             Return _DrawBorders
  174.         End Get
  175.         Set(value As Boolean)
  176.             _DrawBorders = value
  177.             Invalidate()
  178.         End Set
  179.     End Property
  180.  
  181.     Private _BorderColor As Color
  182.     Property BorderColor As Color
  183.         Get
  184.             Return _BorderColor
  185.         End Get
  186.         Set(value As Color)
  187.             _BorderColor = value
  188.         End Set
  189.     End Property
  190.  
  191.     Sub New()
  192.         BackColor = Color.White
  193.     End Sub
  194.  
  195.     Protected Overrides Sub OnPaint(e As System.Windows.Forms.PaintEventArgs)
  196.         Dim G As Graphics = e.Graphics
  197.         G.Clear(BackColor)
  198.         G.DrawString(_TitleText, _TitleFont, New SolidBrush(ForeColor), New Point(8, 5))
  199.         G.DrawString(_SubTitleText, _SubTitleFont, New SolidBrush(ForeColor), New Rectangle(12, 35, Width - 25, Height - 50))
  200.         If DrawBorders Then
  201.             G.DrawRectangle(New Pen(_BorderColor), New Rectangle(0, 0, Width - 1, Height - 1))
  202.         End If
  203.         MyBase.OnPaint(e)
  204.     End Sub
  205.  
  206. End Class
  207.  
  208. Public Class MetroButton
  209.     Inherits Control
  210.     Enum MouseState
  211.         None = 0
  212.         Over = 1
  213.         Down = 2
  214.     End Enum
  215.     Private State As MouseState = MouseState.None
  216.  
  217.     Dim _BGOver As Color '= Color.FromArgb(75, 75, 75)
  218.     Property BackColorOver As Color
  219.         Get
  220.             Return _BGOver
  221.         End Get
  222.         Set(value As Color)
  223.             _BGOver = value
  224.             Invalidate()
  225.         End Set
  226.     End Property
  227.  
  228.     Dim _BGDown As Color '= Color.FromArgb(55, 55, 55)
  229.     Property BackColorDown As Color
  230.         Get
  231.             Return _BGDown
  232.         End Get
  233.         Set(value As Color)
  234.             _BGDown = value
  235.             Invalidate()
  236.         End Set
  237.     End Property
  238.  
  239.     Dim _BorderColor As Color
  240.     Property BorderColor As Color
  241.         Get
  242.             Return _BorderColor
  243.         End Get
  244.         Set(value As Color)
  245.             _BorderColor = value
  246.             Invalidate()
  247.         End Set
  248.     End Property
  249.  
  250.  
  251.     Dim BGC As Color
  252.     Property BackColorNormal As Color
  253.         Get
  254.             Return BGC
  255.         End Get
  256.         Set(value As Color)
  257.             BGC = value
  258.         End Set
  259.     End Property
  260.  
  261.     Sub New()
  262.         ForeColor = Color.White
  263.         Font = New Font("Segoe UI", 9)
  264.         SetStyle(ControlStyles.OptimizedDoubleBuffer, True)
  265.         DoubleBuffered = True
  266.  
  267.         Dim R, G, B As Integer
  268.         Dim BG As Color = BackColor
  269.  
  270.         R = BG.R - 20
  271.         G = BG.G - 20
  272.         B = BG.B - 20
  273.  
  274.         If R < 0 Then R = 0
  275.         If G < 0 Then G = 0
  276.         If B < 0 Then B = 0
  277.  
  278.         BGC = Color.FromArgb(R, G, B)
  279.  
  280.         Size = New Size(105, 27)
  281.     End Sub
  282.  
  283.     Protected Overrides Sub OnBackColorChanged(e As System.EventArgs)
  284.         Dim R, G, B As Integer
  285.  
  286.         R = BackColor.R - 20
  287.         G = BackColor.G - 20
  288.         B = BackColor.B - 20
  289.  
  290.         If R < 0 Then R = 0
  291.         If G < 0 Then G = 0
  292.         If B < 0 Then B = 0
  293.  
  294.         BGC = Color.FromArgb(R, G, B)
  295.  
  296.         R = BGC.R - 10
  297.         G = BGC.G - 10
  298.         B = BGC.B - 10
  299.  
  300.         If R < 0 Then R = 0
  301.         If G < 0 Then G = 0
  302.         If B < 0 Then B = 0
  303.  
  304.         _BGDown = Color.FromArgb(R, G, B)
  305.  
  306.         R = BGC.R + 10
  307.         G = BGC.G + 10
  308.         B = BGC.B + 10
  309.  
  310.         If R > 255 Then R = 255
  311.         If G > 255 Then G = 255
  312.         If B > 255 Then B = 255
  313.  
  314.         _BGOver = Color.FromArgb(R, G, B)
  315.  
  316.         R = BGC.R - 50
  317.         G = BGC.G - 50
  318.         B = BGC.B - 50
  319.  
  320.         If R < 0 Then R = 0
  321.         If G < 0 Then G = 0
  322.         If B < 0 Then B = 0
  323.  
  324.         _BorderColor = Color.FromArgb(R, G, B)
  325.         MyBase.OnBackColorChanged(e)
  326.     End Sub
  327.  
  328.     Protected Overrides Sub OnMouseEnter(e As System.EventArgs)
  329.         State = MouseState.Over
  330.         Invalidate()
  331.         MyBase.OnMouseEnter(e)
  332.     End Sub
  333.  
  334.     Protected Overrides Sub OnMouseLeave(e As System.EventArgs)
  335.         State = MouseState.None
  336.         Invalidate()
  337.         MyBase.OnMouseLeave(e)
  338.     End Sub
  339.  
  340.     Protected Overrides Sub OnMouseDown(e As System.Windows.Forms.MouseEventArgs)
  341.         State = MouseState.Down
  342.         Invalidate()
  343.         MyBase.OnMouseDown(e)
  344.     End Sub
  345.  
  346.     Protected Overrides Sub OnMouseUp(e As System.Windows.Forms.MouseEventArgs)
  347.         State = MouseState.Over
  348.         Invalidate()
  349.         MyBase.OnMouseUp(e)
  350.     End Sub
  351.  
  352.     Protected Overrides Sub OnPaint(e As System.Windows.Forms.PaintEventArgs)
  353.         Dim G As Graphics = e.Graphics
  354.         Select Case State
  355.             Case MouseState.None
  356.                 G.Clear(BGC)
  357.             Case MouseState.Over
  358.                 G.Clear(BackColorOver)
  359.             Case MouseState.Down
  360.                 G.Clear(BackColorDown)
  361.         End Select
  362.         G.DrawRectangle(New Pen(_BorderColor), New Rectangle(0, 0, Width - 1, Height - 1))
  363.         Dim SF As New StringFormat : SF.Alignment = StringAlignment.Center : SF.LineAlignment = StringAlignment.Center
  364.         G.DrawString(Text, Font, New SolidBrush(ForeColor), New Rectangle(0, 0, Width - 1, Height - 1), SF)
  365.         MyBase.OnPaint(e)
  366.     End Sub
  367.  
  368. End Class
  369.  
  370. Public Class MetroTextBox
  371.     Inherits Control
  372.  
  373.     Private _TextAlign As HorizontalAlignment = HorizontalAlignment.Left
  374.     Property TextAlign() As HorizontalAlignment
  375.         Get
  376.             Return _TextAlign
  377.         End Get
  378.         Set(ByVal value As HorizontalAlignment)
  379.             _TextAlign = value
  380.             If Base IsNot Nothing Then
  381.                 Base.TextAlign = value
  382.             End If
  383.         End Set
  384.     End Property
  385.     Private _MaxLength As Integer = 32767
  386.     Property MaxLength() As Integer
  387.         Get
  388.             Return _MaxLength
  389.         End Get
  390.         Set(ByVal value As Integer)
  391.             _MaxLength = value
  392.             If Base IsNot Nothing Then
  393.                 Base.MaxLength = value
  394.             End If
  395.         End Set
  396.     End Property
  397.     Private _ReadOnly As Boolean
  398.     Property [ReadOnly]() As Boolean
  399.         Get
  400.             Return _ReadOnly
  401.         End Get
  402.         Set(ByVal value As Boolean)
  403.             _ReadOnly = value
  404.             If Base IsNot Nothing Then
  405.                 Base.ReadOnly = value
  406.             End If
  407.         End Set
  408.     End Property
  409.     Private _UseSystemPasswordChar As Boolean
  410.     Property UseSystemPasswordChar() As Boolean
  411.         Get
  412.             Return _UseSystemPasswordChar
  413.         End Get
  414.         Set(ByVal value As Boolean)
  415.             _UseSystemPasswordChar = value
  416.             If Base IsNot Nothing Then
  417.                 Base.UseSystemPasswordChar = value
  418.             End If
  419.         End Set
  420.     End Property
  421.     Private _Multiline As Boolean
  422.     Property Multiline() As Boolean
  423.         Get
  424.             Return _Multiline
  425.         End Get
  426.         Set(ByVal value As Boolean)
  427.             _Multiline = value
  428.             If Base IsNot Nothing Then
  429.                 Base.Multiline = value
  430.  
  431.                 If value Then
  432.                     Base.Height = Height - 11
  433.                 Else
  434.                 End If
  435.             End If
  436.         End Set
  437.     End Property
  438.     Overrides Property Text As String
  439.         Get
  440.             Return MyBase.Text
  441.         End Get
  442.         Set(ByVal value As String)
  443.             MyBase.Text = value
  444.             If Base IsNot Nothing Then
  445.                 Base.Text = value
  446.             End If
  447.         End Set
  448.     End Property
  449.     Overrides Property Font As Font
  450.         Get
  451.             Return MyBase.Font
  452.         End Get
  453.         Set(ByVal value As Font)
  454.             MyBase.Font = value
  455.             If Base IsNot Nothing Then
  456.                 Base.Font = value
  457.                 Base.Location = New Point(3, 5)
  458.                 Base.Width = Width - 6
  459.             End If
  460.         End Set
  461.     End Property
  462.  
  463.     Protected Overrides Sub OnParentChanged(e As System.EventArgs)
  464.         If Not Controls.Contains(Base) Then
  465.             Controls.Add(Base)
  466.         End If
  467.     End Sub
  468.  
  469.     Private Base As TextBox
  470.     Dim C As Color
  471.     Sub New()
  472.         Font = New Font("Segoe UI", 9)
  473.         Base = New TextBox
  474.         Base.Font = Font
  475.         Base.Text = Text
  476.         Base.MaxLength = _MaxLength
  477.         Base.Multiline = _Multiline
  478.         Base.ReadOnly = _ReadOnly
  479.         Base.UseSystemPasswordChar = _UseSystemPasswordChar
  480.         Base.BorderStyle = BorderStyle.None
  481.         Base.Location = New Point(5, 4)
  482.         Base.Width = Width - 10
  483.  
  484.         If _Multiline Then
  485.             Base.Height = Height - 11
  486.         End If
  487.  
  488.         AddHandler Base.TextChanged, AddressOf OnBaseTextChanged
  489.         AddHandler Base.KeyDown, AddressOf OnBaseKeyDown
  490.  
  491.         Dim R, G, B As Integer
  492.  
  493.         R = BackColor.R - 15
  494.         G = BackColor.G - 15
  495.         B = BackColor.B - 15
  496.  
  497.         If R < 0 Then R = 0
  498.         If G < 0 Then G = 0
  499.         If B < 0 Then B = 0
  500.  
  501.         C = Color.FromArgb(R, G, B)
  502.         Base.BackColor = C
  503.     End Sub
  504.  
  505.     Protected Overrides Sub OnBackColorChanged(e As System.EventArgs)
  506.         MyBase.OnBackColorChanged(e)
  507.         Dim R, G, B As Integer
  508.  
  509.         R = BackColor.R - 15
  510.         G = BackColor.G - 15
  511.         B = BackColor.B - 15
  512.  
  513.         If R < 0 Then R = 0
  514.         If G < 0 Then G = 0
  515.         If B < 0 Then B = 0
  516.  
  517.         C = Color.FromArgb(R, G, B)
  518.         Base.BackColor = C
  519.         Invalidate()
  520.     End Sub
  521.  
  522.     Protected Overrides Sub OnPaint(e As System.Windows.Forms.PaintEventArgs)
  523.         Dim GG As Graphics = e.Graphics
  524.  
  525.         GG.Clear(C)
  526.         GG.DrawRectangle(Pens.Black, New Rectangle(0, 0, Width - 1, Height - 1))
  527.     End Sub
  528.     Private Sub OnBaseTextChanged(ByVal s As Object, ByVal e As EventArgs)
  529.         Text = Base.Text
  530.     End Sub
  531.     Private Sub OnBaseKeyDown(ByVal s As Object, ByVal e As KeyEventArgs)
  532.         If e.Control AndAlso e.KeyCode = Keys.A Then
  533.             Base.SelectAll()
  534.             e.SuppressKeyPress = True
  535.         End If
  536.     End Sub
  537.     Protected Overrides Sub OnResize(ByVal e As EventArgs)
  538.         Base.Location = New Point(5, 4)
  539.         Base.Width = Width - 10
  540.  
  541.         If _Multiline Then
  542.             Base.Height = Height - 11
  543.         End If
  544.  
  545.  
  546.         MyBase.OnResize(e)
  547.     End Sub
  548.  
  549. End Class
  550.  
  551. Public Class MetroProgressbar
  552.     Inherits Control
  553.  
  554.     Dim _BorderColor As Color = Color.Black
  555.     Property BorderColor As Color
  556.         Get
  557.             Return _BorderColor
  558.         End Get
  559.         Set(value As Color)
  560.             _BorderColor = value
  561.             Invalidate()
  562.         End Set
  563.     End Property
  564.  
  565.     Dim _ProgressColor As Color = Color.FromArgb(10, 150, 40)
  566.     Property ProgressColor As Color
  567.         Get
  568.             Return _ProgressColor
  569.         End Get
  570.         Set(value As Color)
  571.             _ProgressColor = value
  572.             Invalidate()
  573.         End Set
  574.     End Property
  575.  
  576.     Sub New()
  577.         SetStyle(ControlStyles.OptimizedDoubleBuffer Or ControlStyles.AllPaintingInWmPaint, True)
  578.         DoubleBuffered = True
  579.     End Sub
  580.  
  581.     Dim _Val As Integer = 0
  582.     Property Value As Integer
  583.         Get
  584.             Return _Val
  585.         End Get
  586.         Set(v As Integer)
  587.             If v <= _Max Then _Val = v Else Throw New Exception("The entered value is not valid.")
  588.             Invalidate()
  589.         End Set
  590.     End Property
  591.  
  592.     Dim _Max As Integer = 100
  593.     Property Maximum As Integer
  594.         Get
  595.             Return _Max
  596.         End Get
  597.         Set(value As Integer)
  598.             If value >= _Val Then _Max = value Else Throw New Exception("The entered value is not valid.")
  599.         End Set
  600.     End Property
  601.  
  602.     Protected Overrides Sub OnPaint(e As System.Windows.Forms.PaintEventArgs)
  603.         Dim G As Graphics = e.Graphics
  604.         Dim Progress As Double = (_Val / _Max) * (Width - 2)
  605.         G.Clear(BackColor)
  606.         G.FillRectangle(New SolidBrush(Color.FromArgb(20, Color.Black)), New Rectangle(0, 0, Width - 1, Height - 1))
  607.         If Progress > 0 Then G.FillRectangle(New SolidBrush(_ProgressColor), New Rectangle(1, 1, Progress, Height - 2))
  608.  
  609.         G.DrawRectangle(New Pen(_BorderColor), New Rectangle(0, 0, Width - 1, Height - 1))
  610.         MyBase.OnPaint(e)
  611.     End Sub
  612. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement