Advertisement
Guest User

KeyboardMouse

a guest
Feb 12th, 2016
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.80 KB | None | 0 0
  1. using System.Drawing;
  2. using System.Windows.Forms;
  3.  
  4. namespace KeyboardAndMouse
  5. {
  6. public enum Selection { None, Circle, Rectangle, Pie};
  7.  
  8. public partial class Form1 : Form
  9. {
  10. private Color NeonRed = Color.FromArgb(221,0,72);
  11. private Color NeonGreen = Color.FromArgb(57,255,20);
  12. private Color NeonBlue = Color.FromArgb (198,226,255);
  13.  
  14. private Rectangle MyRectangle;
  15. private int Width;
  16. private int Height;
  17. private int CircleX;
  18. private int CircleY;
  19. private int RectangleX;
  20. private int RectangleY;
  21. private int PieX;
  22. private int PieY;
  23. Selection selected;
  24. // #2: Make a Selection field variable and name it "Selected"
  25.  
  26. public Form1 ()
  27. {
  28. InitializeComponent();
  29. Width = 100;
  30. Height = Width;
  31. CircleX = 50;
  32. CircleY = 100;
  33. RectangleX = 150;
  34. RectangleY = 200;
  35. PieX = 250;
  36. PieY = 150;
  37. selected = Selection.None;
  38. // #3 Set the "Selected" variable to none
  39. }
  40.  
  41. private void Form1_Paint(object sender, PaintEventArgs e)
  42. {
  43. Brush RedBrush = new SolidBrush(NeonRed);
  44. Brush GreenBrush = new SolidBrush(NeonGreen);
  45. Brush BlueBrush = new SolidBrush (NeonBlue);
  46.  
  47. /* #4: call the DrawCircle, DrawBox, and DrawPie methods
  48. * by passing in the PaintEventArgs parameter and a brush.
  49. */
  50. DrawCircle(e, RedBrush);
  51. DrawBox (e, GreenBrush);
  52. DrawPie (e, BlueBrush);
  53. }
  54.  
  55. private void DrawCircle(PaintEventArgs e, Brush b) {
  56. e.Graphics.FillEllipse (b, CircleX, CircleY, Width, Width);
  57. }
  58.  
  59. private void DrawBox(PaintEventArgs e, Brush b) {
  60. e.Graphics.FillRectangle (b, RectangleX, RectangleY, Width, Width / 2);
  61. }
  62.  
  63. private void DrawPie(PaintEventArgs e, Brush b) {
  64. e.Graphics.FillPie (b, PieX, PieY, Width, Width, -45, 45);
  65. }
  66.  
  67. private void Mouse_Click(object sender, MouseEventArgs e) {
  68.  
  69. int Left = CircleX;
  70. int Right = CircleX + Width;
  71. int Top = CircleY;
  72. int Bottom = CircleY + Height;
  73.  
  74. selected = Selection.None;
  75.  
  76. if (e.X > Left && e.X < Right && e.Y > Top && e.Y < Bottom) {
  77. selected = Selection.Circle;
  78. DialogResult msg = MessageBox.Show ("You selected a Circle!");
  79. }
  80.  
  81. Left = RectangleX;
  82. Right = RectangleX + Width;
  83. Top = RectangleY;
  84. Bottom = RectangleY + Height;
  85.  
  86. if (e.X > Left && e.X < Right && e.Y > Top && e.Y < Bottom) {
  87. selected = Selection.Rectangle;
  88. DialogResult msg = MessageBox.Show ("You selected the rectangle!");
  89. }
  90.  
  91. Left = PieX;
  92. Right = PieX + Width;
  93. Top = PieY;
  94. Bottom = PieY + Height;
  95.  
  96. if (e.X > Left && e.X < Right && e.Y > Top && e.Y < Bottom) {
  97. selected = Selection.Pie;
  98. DialogResult msg = MessageBox.Show ("You selected the pie slice!");
  99.  
  100. }
  101. }
  102.  
  103. private void Key_Press(object sender, KeyEventArgs e) {
  104. switch (selected) {
  105. case Selection.Circle:
  106.  
  107. switch (e.KeyCode) {
  108. case Keys.Down:
  109. CircleY += 10;
  110. break;
  111. case Keys.Up:
  112. CircleY -= 10;
  113. break;
  114. case Keys.Right:
  115. CircleX += 10;
  116. break;
  117. case Keys.Left:
  118. CircleX -= 10;
  119. break;
  120. }
  121. break;
  122.  
  123.  
  124. case Selection.Rectangle:
  125. switch (e.KeyCode) {
  126. case Keys.Down:
  127. RectangleY += 10;
  128. break;
  129. case Keys.Up:
  130. RectangleY -= 10;
  131. break;
  132. case Keys.Right:
  133. RectangleX += 10;
  134. break;
  135. case Keys.Left:
  136. RectangleX -= 10;
  137. break;
  138. }
  139. break;
  140.  
  141. case Selection.Pie:
  142. switch (e.KeyCode) {
  143. case Keys.Down:
  144. PieY += 10;
  145. break;
  146. case Keys.Up:
  147. PieY -= 10;
  148. break;
  149. case Keys.Right:
  150. PieX += 10;
  151. break;
  152. case Keys.Left:
  153. PieX -= 10;
  154. break;
  155. }
  156.  
  157. break;
  158.  
  159. }
  160. this.Invalidate ();
  161.  
  162. }
  163. }
  164.  
  165. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement