Advertisement
coderail

Virtual Keyboard Control - VB.NET

Mar 25th, 2013
1,585
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 6.49 KB | None | 0 0
  1. Imports System.Drawing.Drawing2D
  2. Imports System.Drawing.Text
  3.  
  4. '------------------
  5. 'Creator: aeonhack
  6. 'Site: elitevs.net
  7. 'Created: 03/25/2013
  8. 'Changed: 03/28/2013
  9. 'Version: 1.1.0
  10. '------------------
  11.  
  12. Class VirtualKeyboard
  13.     Inherits Control
  14.  
  15.     Private TextBitmap As Bitmap
  16.     Private TextGraphics As Graphics
  17.  
  18.     Const LowerKeys As String = "1234567890-=qwertyuiop[]asdfghjkl\;'zxcvbnm,./`"
  19.     Const UpperKeys As String = "!@#$%^&*()_+QWERTYUIOP{}ASDFGHJKL|:""ZXCVBNM<>?~"
  20.  
  21.     Sub New()
  22.         SetStyle(DirectCast(139270, ControlStyles), True)
  23.  
  24.         Font = New Font("Verdana", 8.25F)
  25.  
  26.         TextBitmap = New Bitmap(1, 1)
  27.         TextGraphics = Graphics.FromImage(TextBitmap)
  28.  
  29.         MinimumSize = New Size(386, 162)
  30.         MaximumSize = New Size(386, 162)
  31.  
  32.         Lower = LowerKeys.ToCharArray()
  33.         Upper = UpperKeys.ToCharArray()
  34.  
  35.         PrepareCache()
  36.     End Sub
  37.  
  38.     Public Target As Control
  39.     Public Sub AssignControl(c As Control)
  40.         Target = c
  41.     End Sub
  42.  
  43.     Private Shift As Boolean
  44.  
  45.     Private Pressed As Integer = -1
  46.     Private Buttons As Rectangle()
  47.  
  48.     Private Lower As Char()
  49.     Private Upper As Char()
  50.     Private Other As String() = {"Shift", "Space", "Back"}
  51.  
  52.     Private UpperCache As PointF()
  53.     Private LowerCache As PointF()
  54.  
  55.     Private Sub PrepareCache()
  56.         Buttons = New Rectangle(50) {}
  57.         UpperCache = New PointF(Upper.Length - 1) {}
  58.         LowerCache = New PointF(Lower.Length - 1) {}
  59.  
  60.         Dim I As Integer
  61.  
  62.         Dim S As SizeF
  63.         Dim R As Rectangle
  64.  
  65.         For Y As Integer = 0 To 3
  66.             For X As Integer = 0 To 11
  67.                 I = (Y * 12) + X
  68.                 R = New Rectangle(X * 32, Y * 32, 32, 32)
  69.  
  70.                 Buttons(I) = R
  71.  
  72.                 If Not I = 47 AndAlso Not Char.IsLetter(Upper(I)) Then
  73.                     S = TextGraphics.MeasureString(Upper(I), Font)
  74.                     UpperCache(I) = New PointF(R.X + (R.Width \ 2 - S.Width / 2), R.Y + R.Height - S.Height - 2)
  75.  
  76.                     S = TextGraphics.MeasureString(Lower(I), Font)
  77.                     LowerCache(I) = New PointF(R.X + (R.Width \ 2 - S.Width / 2), R.Y + R.Height - S.Height - 2)
  78.                 End If
  79.             Next
  80.         Next
  81.  
  82.         Buttons(48) = New Rectangle(0, 4 * 32, 2 * 32, 32)
  83.         Buttons(49) = New Rectangle(Buttons(48).Right, 4 * 32, 8 * 32, 32)
  84.         Buttons(50) = New Rectangle(Buttons(49).Right, 4 * 32, 2 * 32, 32)
  85.     End Sub
  86.  
  87.     Private G As Graphics
  88.     Protected Overrides Sub OnPaint(e As System.Windows.Forms.PaintEventArgs)
  89.         G = e.Graphics
  90.         G.TextRenderingHint = TextRenderingHint.ClearTypeGridFit
  91.  
  92.         G.Clear(SystemColors.Control)
  93.  
  94.         Dim S As SizeF
  95.         Dim P As PointF
  96.         Dim R As Rectangle
  97.  
  98.         Dim Offset As Integer
  99.  
  100.         G.DrawRectangle(SystemPens.ControlDarkDark, 0, 0, (12 * 32) + 1, (5 * 32) + 1)
  101.  
  102.         For I As Integer = 0 To Buttons.Length - 1
  103.             R = Buttons(I)
  104.  
  105.             Offset = 0
  106.             If I = Pressed Then Offset = 1
  107.  
  108.             Select Case I
  109.                 Case 48, 49, 50
  110.                     S = G.MeasureString(Other(I - 48), Font)
  111.                     G.DrawString(Other(I - 48), Font, SystemBrushes.ControlText, R.X + (R.Width \ 2 - S.Width / 2) + Offset, R.Y + (R.Height \ 2 - S.Height / 2) + Offset)
  112.                 Case 47
  113.                     DrawArrow(R.X + Offset, R.Y + Offset)
  114.                 Case Else
  115.                     If Shift Then
  116.                         G.DrawString(Upper(I), Font, SystemBrushes.ControlText, R.X + 3 + Offset, R.Y + 2 + Offset)
  117.  
  118.                         If Not Char.IsLetter(Lower(I)) Then
  119.                             P = LowerCache(I)
  120.                             G.DrawString(Lower(I), Font, SystemBrushes.ControlDark, P.X + Offset, P.Y + Offset)
  121.                         End If
  122.                     Else
  123.                         G.DrawString(Lower(I), Font, SystemBrushes.ControlText, R.X + 3 + Offset, R.Y + 2 + Offset)
  124.  
  125.                         If Not Char.IsLetter(Upper(I)) Then
  126.                             P = UpperCache(I)
  127.                             G.DrawString(Upper(I), Font, SystemBrushes.ControlDark, P.X + Offset, P.Y + Offset)
  128.                         End If
  129.                     End If
  130.             End Select
  131.  
  132.             G.DrawRectangle(SystemPens.ControlLightLight, R.X + 1 + Offset, R.Y + 1 + Offset, R.Width - 2, R.Height - 2)
  133.             G.DrawRectangle(SystemPens.ControlDark, R.X + Offset, R.Y + Offset, R.Width, R.Height)
  134.  
  135.             If I = Pressed Then
  136.                 G.DrawLine(SystemPens.ControlDarkDark, R.X, R.Y, R.Right, R.Y)
  137.                 G.DrawLine(SystemPens.ControlDarkDark, R.X, R.Y, R.X, R.Bottom)
  138.             End If
  139.         Next
  140.     End Sub
  141.  
  142.     Private Sub DrawArrow(rx As Integer, ry As Integer)
  143.         Dim R As New Rectangle(rx + 8, ry + 8, 16, 16)
  144.         G.SmoothingMode = SmoothingMode.AntiAlias
  145.  
  146.         Dim P As New Pen(SystemColors.ControlText, 1)
  147.         Dim C As New AdjustableArrowCap(3, 2)
  148.         P.CustomEndCap = C
  149.  
  150.         G.DrawArc(P, R, 0.0F, 290.0F)
  151.  
  152.         P.Dispose()
  153.         C.Dispose()
  154.         G.SmoothingMode = SmoothingMode.None
  155.     End Sub
  156.  
  157.     Protected Overrides Sub OnMouseDown(e As MouseEventArgs)
  158.         Dim Index As Integer = ((e.Y \ 32) * 12) + (e.X \ 32)
  159.  
  160.         If Index > 47 Then
  161.             For I As Integer = 48 To Buttons.Length - 1
  162.                 If Buttons(I).Contains(e.X, e.Y) Then
  163.                     Pressed = I
  164.                     Exit For
  165.                 End If
  166.             Next
  167.         Else
  168.             Pressed = Index
  169.         End If
  170.  
  171.         HandleKey()
  172.         Invalidate()
  173.     End Sub
  174.  
  175.     Protected Overrides Sub OnMouseUp(e As MouseEventArgs)
  176.         Pressed = -1
  177.         Invalidate()
  178.     End Sub
  179.  
  180.     Private Sub HandleKey()
  181.         If Target Is Nothing Then Return
  182.         If Pressed = -1 Then Return
  183.  
  184.         Select Case Pressed
  185.             Case 47
  186.                 Target.Text = String.Empty
  187.             Case 48
  188.                 Shift = Not Shift
  189.             Case 49
  190.                 Target.Text &= " "
  191.             Case 50
  192.                 If Not Target.Text.Length = 0 Then
  193.                     Target.Text = Target.Text.Remove(Target.Text.Length - 1)
  194.                 End If
  195.             Case Else
  196.                 If Shift Then
  197.                     Target.Text &= Upper(Pressed)
  198.                 Else
  199.                     Target.Text &= Lower(Pressed)
  200.                 End If
  201.         End Select
  202.     End Sub
  203.  
  204. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement