Advertisement
benito

Untitled

Sep 7th, 2011
237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 19.75 KB | None | 0 0
  1.     Imports System.Drawing.Drawing2D
  2.     Imports System.ComponentModel
  3.     Imports System.Runtime.InteropServices
  4.      
  5.     MustInherit Class Theme
  6.         Inherits ContainerControl
  7.      
  8.     #Region " Initialization "
  9.      
  10.         Protected G As Graphics
  11.         Sub New()
  12.             SetStyle(DirectCast(139270, ControlStyles), True)
  13.         End Sub
  14.      
  15.         Private ParentIsForm As Boolean
  16.         Protected Overrides Sub OnHandleCreated(ByVal e As EventArgs)
  17.             Dock = DockStyle.Fill
  18.             ParentIsForm = TypeOf Parent Is Form
  19.             If ParentIsForm Then
  20.                 If Not _TransparencyKey = Color.Empty Then ParentForm.TransparencyKey = _TransparencyKey
  21.                 ParentForm.FormBorderStyle = FormBorderStyle.FixedSingle
  22.             End If
  23.             MyBase.OnHandleCreated(e)
  24.         End Sub
  25.      
  26.         Overrides Property Text As String
  27.             Get
  28.                 Return MyBase.Text
  29.             End Get
  30.             Set(ByVal v As String)
  31.                 MyBase.Text = v
  32.                 Invalidate()
  33.             End Set
  34.         End Property
  35.     #End Region
  36.      
  37.     #Region " Sizing and Movement "
  38.      
  39.         Private _Resizable As Boolean = True
  40.         Property Resizable() As Boolean
  41.             Get
  42.                 Return _Resizable
  43.             End Get
  44.             Set(ByVal value As Boolean)
  45.                 _Resizable = value
  46.             End Set
  47.         End Property
  48.      
  49.         Private _MoveHeight As Integer = 24
  50.         Property MoveHeight() As Integer
  51.             Get
  52.                 Return _MoveHeight
  53.             End Get
  54.             Set(ByVal v As Integer)
  55.                 _MoveHeight = v
  56.                 Header = New Rectangle(7, 7, Width - 14, _MoveHeight - 7)
  57.             End Set
  58.         End Property
  59.      
  60.         Private Flag As IntPtr
  61.         Protected Overrides Sub OnMouseDown(ByVal e As MouseEventArgs)
  62.             If Not e.Button = MouseButtons.Left Then Return
  63.             If ParentIsForm Then If ParentForm.WindowState = FormWindowState.Maximized Then Return
  64.      
  65.             If Header.Contains(e.Location) Then
  66.                 Flag = New IntPtr(2)
  67.             ElseIf Current.Position = 0 Or Not _Resizable Then
  68.                 Return
  69.             Else
  70.                 Flag = New IntPtr(Current.Position)
  71.             End If
  72.      
  73.             Capture = False
  74.             DefWndProc(Message.Create(Parent.Handle, 161, Flag, Nothing))
  75.      
  76.             MyBase.OnMouseDown(e)
  77.         End Sub
  78.      
  79.         Private Structure Pointer
  80.             ReadOnly Cursor As Cursor, Position As Byte
  81.             Sub New(ByVal c As Cursor, ByVal p As Byte)
  82.                 Cursor = c
  83.                 Position = p
  84.             End Sub
  85.         End Structure
  86.      
  87.         Private F1, F2, F3, F4 As Boolean, PTC As Point
  88.         Private Function GetPointer() As Pointer
  89.             PTC = PointToClient(MousePosition)
  90.             F1 = PTC.X < 7
  91.             F2 = PTC.X > Width - 7
  92.             F3 = PTC.Y < 7
  93.             F4 = PTC.Y > Height - 7
  94.      
  95.             If F1 And F3 Then Return New Pointer(Cursors.SizeNWSE, 13)
  96.             If F1 And F4 Then Return New Pointer(Cursors.SizeNESW, 16)
  97.             If F2 And F3 Then Return New Pointer(Cursors.SizeNESW, 14)
  98.             If F2 And F4 Then Return New Pointer(Cursors.SizeNWSE, 17)
  99.             If F1 Then Return New Pointer(Cursors.SizeWE, 10)
  100.             If F2 Then Return New Pointer(Cursors.SizeWE, 11)
  101.             If F3 Then Return New Pointer(Cursors.SizeNS, 12)
  102.             If F4 Then Return New Pointer(Cursors.SizeNS, 15)
  103.             Return New Pointer(Cursors.Default, 0)
  104.         End Function
  105.      
  106.         Private Current, Pending As Pointer
  107.         Private Sub SetCurrent()
  108.             Pending = GetPointer()
  109.             If Current.Position = Pending.Position Then Return
  110.             Current = GetPointer()
  111.             Cursor = Current.Cursor
  112.         End Sub
  113.      
  114.         Protected Overrides Sub OnMouseMove(ByVal e As MouseEventArgs)
  115.             If _Resizable Then SetCurrent()
  116.             MyBase.OnMouseMove(e)
  117.         End Sub
  118.      
  119.         Protected Header As Rectangle
  120.         Protected Overrides Sub OnSizeChanged(ByVal e As EventArgs)
  121.             If Width = 0 OrElse Height = 0 Then Return
  122.             Header = New Rectangle(7, 7, Width - 14, _MoveHeight - 7)
  123.             Invalidate()
  124.             MyBase.OnSizeChanged(e)
  125.         End Sub
  126.      
  127.     #End Region
  128.      
  129.     #Region " Convienence "
  130.      
  131.         MustOverride Sub PaintHook()
  132.         Protected NotOverridable Overrides Sub OnPaint(ByVal e As PaintEventArgs)
  133.             If Width = 0 OrElse Height = 0 Then Return
  134.             G = e.Graphics
  135.             PaintHook()
  136.         End Sub
  137.      
  138.         Private _TransparencyKey As Color
  139.         Property TransparencyKey() As Color
  140.             Get
  141.                 Return _TransparencyKey
  142.             End Get
  143.             Set(ByVal v As Color)
  144.                 _TransparencyKey = v
  145.                 Invalidate()
  146.             End Set
  147.         End Property
  148.      
  149.         Private _Image As Image
  150.         Property Image() As Image
  151.             Get
  152.                 Return _Image
  153.             End Get
  154.             Set(ByVal value As Image)
  155.                 _Image = value
  156.                 Invalidate()
  157.             End Set
  158.         End Property
  159.         ReadOnly Property ImageWidth() As Integer
  160.             Get
  161.                 If _Image Is Nothing Then Return 0
  162.                 Return _Image.Width
  163.             End Get
  164.         End Property
  165.      
  166.         Private _Size As Size
  167.         Private _Rectangle As Rectangle
  168.         Private _Gradient As LinearGradientBrush
  169.         Private _Brush As SolidBrush
  170.      
  171.         Protected Sub DrawCorners(ByVal c As Color, ByVal rect As Rectangle)
  172.             _Brush = New SolidBrush(c)
  173.             G.FillRectangle(_Brush, rect.X, rect.Y, 1, 1)
  174.             G.FillRectangle(_Brush, rect.X + (rect.Width - 1), rect.Y, 1, 1)
  175.             G.FillRectangle(_Brush, rect.X, rect.Y + (rect.Height - 1), 1, 1)
  176.             G.FillRectangle(_Brush, rect.X + (rect.Width - 1), rect.Y + (rect.Height - 1), 1, 1)
  177.         End Sub
  178.      
  179.         Protected Sub DrawBorders(ByVal p1 As Pen, ByVal p2 As Pen, ByVal rect As Rectangle)
  180.             G.DrawRectangle(p1, rect.X, rect.Y, rect.Width - 1, rect.Height - 1)
  181.             G.DrawRectangle(p2, rect.X + 1, rect.Y + 1, rect.Width - 3, rect.Height - 3)
  182.         End Sub
  183.      
  184.         Protected Sub DrawText(ByVal a As HorizontalAlignment, ByVal c As Color, ByVal x As Integer)
  185.             DrawText(a, c, x, 0)
  186.         End Sub
  187.         Protected Sub DrawText(ByVal a As HorizontalAlignment, ByVal c As Color, ByVal x As Integer, ByVal y As Integer)
  188.             If String.IsNullOrEmpty(Text) Then Return
  189.             _Size = G.MeasureString(Text, Font).ToSize
  190.             _Brush = New SolidBrush(c)
  191.      
  192.             Select Case a
  193.                 Case HorizontalAlignment.Left
  194.                     G.DrawString(Text, Font, _Brush, x, _MoveHeight \ 2 - _Size.Height \ 2 + y)
  195.                 Case HorizontalAlignment.Right
  196.                     G.DrawString(Text, Font, _Brush, Width - _Size.Width - x, _MoveHeight \ 2 - _Size.Height \ 2 + y)
  197.                 Case HorizontalAlignment.Center
  198.                     G.DrawString(Text, Font, _Brush, Width \ 2 - _Size.Width \ 2 + x, _MoveHeight \ 2 - _Size.Height \ 2 + y)
  199.             End Select
  200.         End Sub
  201.         Protected Sub DrawText(ByVal a As HorizontalAlignment, ByVal c As Color, ByVal x As Integer, ByVal y As Integer, ByVal f As Font)
  202.             If String.IsNullOrEmpty(Text) Then Return
  203.             _Size = G.MeasureString(Text, Font).ToSize
  204.             _Brush = New SolidBrush(c)
  205.      
  206.             Select Case a
  207.                 Case HorizontalAlignment.Left
  208.                     G.DrawString(Text, f, _Brush, x, _MoveHeight \ 2 - _Size.Height \ 2 + y)
  209.                 Case HorizontalAlignment.Right
  210.                     G.DrawString(Text, f, _Brush, Width - _Size.Width - x, _MoveHeight \ 2 - _Size.Height \ 2 + y)
  211.                 Case HorizontalAlignment.Center
  212.                     G.DrawString(Text, f, _Brush, Width \ 2 - _Size.Width \ 2 + x, _MoveHeight \ 2 - _Size.Height \ 2 + y)
  213.             End Select
  214.         End Sub
  215.      
  216.         Protected Sub DrawIcon(ByVal a As HorizontalAlignment, ByVal x As Integer)
  217.             DrawIcon(a, x, 0)
  218.         End Sub
  219.         Protected Sub DrawIcon(ByVal a As HorizontalAlignment, ByVal x As Integer, ByVal y As Integer)
  220.             If _Image Is Nothing Then Return
  221.             Select Case a
  222.                 Case HorizontalAlignment.Left
  223.                     G.DrawImage(_Image, x, _MoveHeight \ 2 - _Image.Height \ 2 + y)
  224.                 Case HorizontalAlignment.Right
  225.                     G.DrawImage(_Image, Width - _Image.Width - x, _MoveHeight \ 2 - _Image.Height \ 2 + y)
  226.                 Case HorizontalAlignment.Center
  227.                     G.DrawImage(_Image, Width \ 2 - _Image.Width \ 2, _MoveHeight \ 2 - _Image.Height \ 2)
  228.             End Select
  229.         End Sub
  230.      
  231.         Protected Sub DrawGradient(ByVal c1 As Color, ByVal c2 As Color, ByVal x As Integer, ByVal y As Integer, ByVal width As Integer, ByVal height As Integer, ByVal angle As Single)
  232.             _Rectangle = New Rectangle(x, y, width, height)
  233.             _Gradient = New LinearGradientBrush(_Rectangle, c1, c2, angle)
  234.             G.FillRectangle(_Gradient, _Rectangle)
  235.         End Sub
  236.      
  237.     #End Region
  238.      
  239.     End Class
  240.     MustInherit Class ThemeControl
  241.         Inherits Control
  242.      
  243.     #Region " Initialization "
  244.      
  245.         Protected G As Graphics, B As Bitmap
  246.         Sub New()
  247.             SetStyle(DirectCast(139270, ControlStyles), True)
  248.             B = New Bitmap(1, 1)
  249.             G = Graphics.FromImage(B)
  250.         End Sub
  251.      
  252.         Sub AllowTransparent()
  253.             SetStyle(ControlStyles.Opaque, False)
  254.             SetStyle(ControlStyles.SupportsTransparentBackColor, True)
  255.         End Sub
  256.      
  257.         Overrides Property Text As String
  258.             Get
  259.                 Return MyBase.Text
  260.             End Get
  261.             Set(ByVal v As String)
  262.                 MyBase.Text = v
  263.                 Invalidate()
  264.             End Set
  265.         End Property
  266.     #End Region
  267.      
  268.     #Region " Mouse Handling "
  269.      
  270.         Protected Enum State As Byte
  271.             MouseNone = 0
  272.             MouseOver = 1
  273.             MouseDown = 2
  274.         End Enum
  275.      
  276.         Protected MouseState As State
  277.         Protected Overrides Sub OnMouseLeave(ByVal e As EventArgs)
  278.             ChangeMouseState(State.MouseNone)
  279.             MyBase.OnMouseLeave(e)
  280.         End Sub
  281.         Protected Overrides Sub OnMouseEnter(ByVal e As EventArgs)
  282.             ChangeMouseState(State.MouseOver)
  283.             MyBase.OnMouseEnter(e)
  284.         End Sub
  285.         Protected Overrides Sub OnMouseUp(ByVal e As MouseEventArgs)
  286.             ChangeMouseState(State.MouseOver)
  287.             MyBase.OnMouseUp(e)
  288.         End Sub
  289.         Protected Overrides Sub OnMouseDown(ByVal e As MouseEventArgs)
  290.             If e.Button = MouseButtons.Left Then ChangeMouseState(State.MouseDown)
  291.             MyBase.OnMouseDown(e)
  292.         End Sub
  293.      
  294.         Private Sub ChangeMouseState(ByVal e As State)
  295.             MouseState = e
  296.             Invalidate()
  297.         End Sub
  298.      
  299.     #End Region
  300.      
  301.     #Region " Convienence "
  302.      
  303.         MustOverride Sub PaintHook()
  304.         Protected NotOverridable Overrides Sub OnPaint(ByVal e As PaintEventArgs)
  305.             If Width = 0 OrElse Height = 0 Then Return
  306.             PaintHook()
  307.             e.Graphics.DrawImage(B, 0, 0)
  308.         End Sub
  309.      
  310.         Protected Overrides Sub OnSizeChanged(ByVal e As EventArgs)
  311.             If Not Width = 0 AndAlso Not Height = 0 Then
  312.                 B = New Bitmap(Width, Height)
  313.                 G = Graphics.FromImage(B)
  314.                 Invalidate()
  315.             End If
  316.             MyBase.OnSizeChanged(e)
  317.         End Sub
  318.      
  319.         Private _NoRounding As Boolean
  320.         Property NoRounding() As Boolean
  321.             Get
  322.                 Return _NoRounding
  323.             End Get
  324.             Set(ByVal v As Boolean)
  325.                 _NoRounding = v
  326.                 Invalidate()
  327.             End Set
  328.         End Property
  329.      
  330.         Private _Image As Image
  331.         Property Image() As Image
  332.             Get
  333.                 Return _Image
  334.             End Get
  335.             Set(ByVal value As Image)
  336.                 _Image = value
  337.                 Invalidate()
  338.             End Set
  339.         End Property
  340.         ReadOnly Property ImageWidth() As Integer
  341.             Get
  342.                 If _Image Is Nothing Then Return 0
  343.                 Return _Image.Width
  344.             End Get
  345.         End Property
  346.         ReadOnly Property ImageTop() As Integer
  347.             Get
  348.                 If _Image Is Nothing Then Return 0
  349.                 Return Height \ 2 - _Image.Height \ 2
  350.             End Get
  351.         End Property
  352.      
  353.         Private _Size As Size
  354.         Private _Rectangle As Rectangle
  355.         Private _Gradient As LinearGradientBrush
  356.         Private _Brush As SolidBrush
  357.      
  358.         Protected Sub DrawCorners(ByVal c As Color, ByVal rect As Rectangle)
  359.             If _NoRounding Then Return
  360.      
  361.             B.SetPixel(rect.X, rect.Y, c)
  362.             B.SetPixel(rect.X + (rect.Width - 1), rect.Y, c)
  363.             B.SetPixel(rect.X, rect.Y + (rect.Height - 1), c)
  364.             B.SetPixel(rect.X + (rect.Width - 1), rect.Y + (rect.Height - 1), c)
  365.         End Sub
  366.      
  367.         Protected Sub DrawBorders(ByVal p1 As Pen, ByVal p2 As Pen, ByVal rect As Rectangle)
  368.             G.DrawRectangle(p1, rect.X, rect.Y, rect.Width - 1, rect.Height - 1)
  369.             G.DrawRectangle(p2, rect.X + 1, rect.Y + 1, rect.Width - 3, rect.Height - 3)
  370.         End Sub
  371.      
  372.         Protected Sub DrawText(ByVal a As HorizontalAlignment, ByVal c As Color, ByVal x As Integer)
  373.             DrawText(a, c, x, 0)
  374.         End Sub
  375.         Protected Sub DrawText(ByVal a As HorizontalAlignment, ByVal c As Color, ByVal x As Integer, ByVal y As Integer)
  376.             If String.IsNullOrEmpty(Text) Then Return
  377.             _Size = G.MeasureString(Text, Font).ToSize
  378.             _Brush = New SolidBrush(c)
  379.      
  380.             Select Case a
  381.                 Case HorizontalAlignment.Left
  382.                     G.DrawString(Text, Font, _Brush, x, Height \ 2 - _Size.Height \ 2 + y)
  383.                 Case HorizontalAlignment.Right
  384.                     G.DrawString(Text, Font, _Brush, Width - _Size.Width - x, Height \ 2 - _Size.Height \ 2 + y)
  385.                 Case HorizontalAlignment.Center
  386.                     G.DrawString(Text, Font, _Brush, Width \ 2 - _Size.Width \ 2 + x, Height \ 2 - _Size.Height \ 2 + y)
  387.             End Select
  388.         End Sub
  389.      
  390.         Protected Sub DrawIcon(ByVal a As HorizontalAlignment, ByVal x As Integer)
  391.             DrawIcon(a, x, 0)
  392.         End Sub
  393.         Protected Sub DrawIcon(ByVal a As HorizontalAlignment, ByVal x As Integer, ByVal y As Integer)
  394.             If _Image Is Nothing Then Return
  395.             Select Case a
  396.                 Case HorizontalAlignment.Left
  397.                     G.DrawImage(_Image, x, Height \ 2 - _Image.Height \ 2 + y)
  398.                 Case HorizontalAlignment.Right
  399.                     G.DrawImage(_Image, Width - _Image.Width - x, Height \ 2 - _Image.Height \ 2 + y)
  400.                 Case HorizontalAlignment.Center
  401.                     G.DrawImage(_Image, Width \ 2 - _Image.Width \ 2, Height \ 2 - _Image.Height \ 2)
  402.             End Select
  403.         End Sub
  404.      
  405.         Protected Sub DrawGradient(ByVal c1 As Color, ByVal c2 As Color, ByVal x As Integer, ByVal y As Integer, ByVal width As Integer, ByVal height As Integer, ByVal angle As Single)
  406.             _Rectangle = New Rectangle(x, y, width, height)
  407.             _Gradient = New LinearGradientBrush(_Rectangle, c1, c2, angle)
  408.             G.FillRectangle(_Gradient, _Rectangle)
  409.         End Sub
  410.     #End Region
  411.      
  412.     End Class
  413.     MustInherit Class ThemeContainerControl
  414.         Inherits ContainerControl
  415.      
  416.     #Region " Initialization "
  417.      
  418.         Protected G As Graphics, B As Bitmap
  419.         Sub New()
  420.             SetStyle(DirectCast(139270, ControlStyles), True)
  421.             B = New Bitmap(1, 1)
  422.             G = Graphics.FromImage(B)
  423.         End Sub
  424.      
  425.         Sub AllowTransparent()
  426.             SetStyle(ControlStyles.Opaque, False)
  427.             SetStyle(ControlStyles.SupportsTransparentBackColor, True)
  428.         End Sub
  429.      
  430.     #End Region
  431.      
  432.     #Region " Convienence "
  433.      
  434.         MustOverride Sub PaintHook()
  435.         Protected NotOverridable Overrides Sub OnPaint(ByVal e As PaintEventArgs)
  436.             If Width = 0 OrElse Height = 0 Then Return
  437.             PaintHook()
  438.             e.Graphics.DrawImage(B, 0, 0)
  439.         End Sub
  440.      
  441.         Protected Overrides Sub OnSizeChanged(ByVal e As EventArgs)
  442.             If Not Width = 0 AndAlso Not Height = 0 Then
  443.                 B = New Bitmap(Width, Height)
  444.                 G = Graphics.FromImage(B)
  445.                 Invalidate()
  446.             End If
  447.             MyBase.OnSizeChanged(e)
  448.         End Sub
  449.      
  450.         Private _NoRounding As Boolean
  451.         Property NoRounding() As Boolean
  452.             Get
  453.                 Return _NoRounding
  454.             End Get
  455.             Set(ByVal v As Boolean)
  456.                 _NoRounding = v
  457.                 Invalidate()
  458.             End Set
  459.         End Property
  460.      
  461.         Private _Rectangle As Rectangle
  462.         Private _Gradient As LinearGradientBrush
  463.      
  464.         Protected Sub DrawCorners(ByVal c As Color, ByVal rect As Rectangle)
  465.             If _NoRounding Then Return
  466.             B.SetPixel(rect.X, rect.Y, c)
  467.             B.SetPixel(rect.X + (rect.Width - 1), rect.Y, c)
  468.             B.SetPixel(rect.X, rect.Y + (rect.Height - 1), c)
  469.             B.SetPixel(rect.X + (rect.Width - 1), rect.Y + (rect.Height - 1), c)
  470.         End Sub
  471.      
  472.         Protected Sub DrawBorders(ByVal p1 As Pen, ByVal p2 As Pen, ByVal rect As Rectangle)
  473.             G.DrawRectangle(p1, rect.X, rect.Y, rect.Width - 1, rect.Height - 1)
  474.             G.DrawRectangle(p2, rect.X + 1, rect.Y + 1, rect.Width - 3, rect.Height - 3)
  475.         End Sub
  476.      
  477.         Protected Sub DrawGradient(ByVal c1 As Color, ByVal c2 As Color, ByVal x As Integer, ByVal y As Integer, ByVal width As Integer, ByVal height As Integer, ByVal angle As Single)
  478.             _Rectangle = New Rectangle(x, y, width, height)
  479.             _Gradient = New LinearGradientBrush(_Rectangle, c1, c2, angle)
  480.             G.FillRectangle(_Gradient, _Rectangle)
  481.         End Sub
  482.     #End Region
  483.      
  484.     End Class
  485.     Class TeamViewerTheme
  486.         Inherits Theme
  487.      
  488.         Sub New()
  489.             MoveHeight = 19
  490.             'TransparencyKey = Color.Fuchsia
  491.             'Me.Resizable = False
  492.             Me.Font = New Font("Microsoft Sans Serif", 9, FontStyle.Regular)
  493.         End Sub
  494.         Public Overrides Sub PaintHook()
  495.      
  496.             G.Clear(Color.White)
  497.             DrawGradient(Color.FromArgb(0, 153, 255), Color.FromArgb(0, 102, 255), 0, 0, Width, 28, 90S)
  498.             DrawGradient(Color.FromArgb(51, 153, 255), Color.FromArgb(0, 102, 204), 0, 29, Width, 55, 90S)
  499.             DrawGradient(Color.White, Color.FromArgb(204, 204, 204), 0, 115, Width, Height - 55, 90S)
  500.             DrawGradient(Color.FromArgb(204, 204, 204), Color.White, 0, 84, Width, 35, 90S)
  501.             G.DrawLine(Pens.DarkBlue, 0, 28, Width, 28)
  502.             G.DrawLine(New Pen(New SolidBrush(Color.FromArgb(51, 204, 255))), 0, 29, Width, 29)
  503.             G.DrawLine(Pens.White, 0, 84, Width, 84)
  504.             'G.DrawString(".", Me.Parent.Font, Brushes.Black, -2, Height - 12)
  505.             'G.DrawString(".", Me.Parent.Font, Brushes.Black, Width - 5, Height - 12)
  506.             'DrawBorders(Pens.Black, Pens.Transparent, ClientRectangle)
  507.             'DrawCorners(Color.Fuchsia, ClientRectangle)
  508.      
  509.         End Sub
  510.     End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement