Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- using System.Drawing;
- using System.Drawing.Drawing2D;
- using System.Drawing.Text;
- //------------------
- //Creator: aeonhack
- //Converted by: recuperare
- //Site: elitevs.net
- //Created: 03/25/2013
- //Changed: 03/25/2013
- //Version: 1.0.0
- //------------------
- class VirtualKeyboard : Control
- {
- private Bitmap TextBitmap;
- private Graphics TextGraphics;
- const string LowerKeys = "`1234567890-=qwertyuiop[]\\asdfghjkl;'zxcvbnm,./";
- const string UpperKeys = "~!@#$%^&*()_+QWERTYUIOP{}|ASDFGHJKL:\"ZXCVBNM<>?";
- public VirtualKeyboard()
- {
- SetStyle((ControlStyles)139270, true);
- Font = new Font("Verdana", 8.25f);
- TextBitmap = new Bitmap(1, 1);
- TextGraphics = Graphics.FromImage(TextBitmap);
- MinimumSize = new Size(386, 162);
- MaximumSize = new Size(386, 162);
- Randomize();
- PrepareCache();
- }
- private Control Target;
- public void AssignControl(Control c)
- {
- Target = c;
- }
- private bool Shift;
- private int Pressed = -1;
- private Rectangle[] Buttons;
- private char[] Lower;
- private char[] Upper;
- private string[] Other = {
- "Shift",
- "Space",
- "Back"
- };
- private void PrepareCache()
- {
- Buttons = new Rectangle[51];
- int I = 0;
- for (int Y = 0; Y <= 3; Y++)
- {
- for (int X = 0; X <= 11; X++)
- {
- I = (Y * 12) + X;
- Buttons[I] = new Rectangle(X * 32, Y * 32, 32, 32);
- }
- }
- Buttons[48] = new Rectangle(0, 4 * 32, 2 * 32, 32);
- Buttons[49] = new Rectangle(Buttons[48].Right, 4 * 32, 8 * 32, 32);
- Buttons[50] = new Rectangle(Buttons[49].Right, 4 * 32, 2 * 32, 32);
- }
- private void Randomize()
- {
- Lower = LowerKeys.ToCharArray();
- Upper = UpperKeys.ToCharArray();
- Random R = new Random();
- int X = 0;
- int Y = 0;
- char C1 = '\0';
- char C2 = '\0';
- for (int I = 1; I <= 12; I++)
- {
- X = R.Next(Lower.Length);
- Y = R.Next(Lower.Length);
- C1 = Lower[X];
- C2 = Upper[X];
- Lower[X] = Lower[Y];
- Upper[X] = Upper[Y];
- Lower[Y] = C1;
- Upper[Y] = C2;
- }
- }
- private Graphics G;
- protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
- {
- G = e.Graphics;
- G.TextRenderingHint = TextRenderingHint.ClearTypeGridFit;
- G.Clear(SystemColors.Control);
- SizeF S = default(SizeF);
- Rectangle R = default(Rectangle);
- int Offset = 0;
- G.DrawRectangle(SystemPens.ControlDarkDark, 0, 0, (12 * 32) + 1, (5 * 32) + 1);
- for (int I = 0; I <= Buttons.Length - 1; I++)
- {
- R = Buttons[I];
- Offset = 0;
- if (I == Pressed)
- Offset = 1;
- switch (I)
- {
- case 48:
- case 49:
- case 50:
- S = G.MeasureString(Other[I - 48], Font);
- 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);
- break;
- case 47:
- DrawArrow(R.X + Offset, R.Y + Offset);
- break;
- default:
- if (Shift)
- {
- G.DrawString(Upper[I].ToString(), Font, SystemBrushes.ControlText, R.X + 4 + Offset, R.Y + 3 + Offset);
- }
- else
- {
- G.DrawString(Lower[I].ToString(), Font, SystemBrushes.ControlText, R.X + 4 + Offset, R.Y + 3 + Offset);
- }
- break;
- }
- G.DrawRectangle(SystemPens.ControlLightLight, R.X + 1 + Offset, R.Y + 1 + Offset, R.Width - 2, R.Height - 2);
- G.DrawRectangle(SystemPens.ControlDark, R.X + Offset, R.Y + Offset, R.Width, R.Height);
- if (I == Pressed)
- {
- G.DrawLine(SystemPens.ControlDarkDark, R.X, R.Y, R.Right, R.Y);
- G.DrawLine(SystemPens.ControlDarkDark, R.X, R.Y, R.X, R.Bottom);
- }
- }
- }
- private void DrawArrow(int rx, int ry)
- {
- Rectangle R = new Rectangle(rx + 8, ry + 8, 16, 16);
- G.SmoothingMode = SmoothingMode.AntiAlias;
- Pen P = new Pen(SystemColors.ControlText, 1);
- AdjustableArrowCap C = new AdjustableArrowCap(3, 2);
- P.CustomEndCap = C;
- G.DrawArc(P, R, 0f, 290f);
- P.Dispose();
- C.Dispose();
- G.SmoothingMode = SmoothingMode.None;
- }
- protected override void OnMouseDown(MouseEventArgs e)
- {
- int Index = ((e.Y / 32) * 12) + (e.X / 32);
- if (Index > 47)
- {
- for (int I = 48; I <= Buttons.Length - 1; I++)
- {
- if (Buttons[I].Contains(e.X, e.Y))
- {
- Pressed = I;
- break; // TODO: might not be correct. Was : Exit For
- }
- }
- }
- else
- {
- Pressed = Index;
- }
- HandleKey();
- Invalidate();
- }
- protected override void OnMouseUp(MouseEventArgs e)
- {
- Pressed = -1;
- Invalidate();
- }
- private void HandleKey()
- {
- if (Target == null)
- return;
- switch (Pressed)
- {
- case 47:
- Randomize();
- break;
- case 48:
- Shift = !Shift;
- break;
- case 49:
- Target.Text += " ";
- break;
- case 50:
- if (!(Target.Text.Length == 0))
- {
- Target.Text = Target.Text.Remove(Target.Text.Length - 1);
- }
- break;
- default:
- if (Shift)
- {
- Target.Text += Upper[Pressed];
- }
- else
- {
- Target.Text += Lower[Pressed];
- }
- break;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement