Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Drawing;
- using System.Windows.Forms;
- namespace KeyboardAndMouse
- {
- public enum Selection { None, Circle, Rectangle, Pie};
- public partial class Form1 : Form
- {
- private Color NeonRed = Color.FromArgb(221,0,72);
- private Color NeonGreen = Color.FromArgb(57,255,20);
- private Color NeonBlue = Color.FromArgb (198,226,255);
- private Rectangle MyRectangle;
- private int Width;
- private int Height;
- private int CircleX;
- private int CircleY;
- private int RectangleX;
- private int RectangleY;
- private int PieX;
- private int PieY;
- Selection selected;
- // #2: Make a Selection field variable and name it "Selected"
- public Form1 ()
- {
- InitializeComponent();
- Width = 100;
- Height = Width;
- CircleX = 50;
- CircleY = 100;
- RectangleX = 150;
- RectangleY = 200;
- PieX = 250;
- PieY = 150;
- selected = Selection.None;
- // #3 Set the "Selected" variable to none
- }
- private void Form1_Paint(object sender, PaintEventArgs e)
- {
- Brush RedBrush = new SolidBrush(NeonRed);
- Brush GreenBrush = new SolidBrush(NeonGreen);
- Brush BlueBrush = new SolidBrush (NeonBlue);
- /* #4: call the DrawCircle, DrawBox, and DrawPie methods
- * by passing in the PaintEventArgs parameter and a brush.
- */
- DrawCircle(e, RedBrush);
- DrawBox (e, GreenBrush);
- DrawPie (e, BlueBrush);
- }
- private void DrawCircle(PaintEventArgs e, Brush b) {
- e.Graphics.FillEllipse (b, CircleX, CircleY, Width, Width);
- }
- private void DrawBox(PaintEventArgs e, Brush b) {
- e.Graphics.FillRectangle (b, RectangleX, RectangleY, Width, Width / 2);
- }
- private void DrawPie(PaintEventArgs e, Brush b) {
- e.Graphics.FillPie (b, PieX, PieY, Width, Width, -45, 45);
- }
- private void Mouse_Click(object sender, MouseEventArgs e) {
- int Left = CircleX;
- int Right = CircleX + Width;
- int Top = CircleY;
- int Bottom = CircleY + Height;
- selected = Selection.None;
- if (e.X > Left && e.X < Right && e.Y > Top && e.Y < Bottom) {
- selected = Selection.Circle;
- DialogResult msg = MessageBox.Show ("You selected a Circle!");
- }
- Left = RectangleX;
- Right = RectangleX + Width;
- Top = RectangleY;
- Bottom = RectangleY + Height;
- if (e.X > Left && e.X < Right && e.Y > Top && e.Y < Bottom) {
- selected = Selection.Rectangle;
- DialogResult msg = MessageBox.Show ("You selected the rectangle!");
- }
- Left = PieX;
- Right = PieX + Width;
- Top = PieY;
- Bottom = PieY + Height;
- if (e.X > Left && e.X < Right && e.Y > Top && e.Y < Bottom) {
- selected = Selection.Pie;
- DialogResult msg = MessageBox.Show ("You selected the pie slice!");
- }
- }
- private void Key_Press(object sender, KeyEventArgs e) {
- switch (selected) {
- case Selection.Circle:
- switch (e.KeyCode) {
- case Keys.Down:
- CircleY += 10;
- break;
- case Keys.Up:
- CircleY -= 10;
- break;
- case Keys.Right:
- CircleX += 10;
- break;
- case Keys.Left:
- CircleX -= 10;
- break;
- }
- break;
- case Selection.Rectangle:
- switch (e.KeyCode) {
- case Keys.Down:
- RectangleY += 10;
- break;
- case Keys.Up:
- RectangleY -= 10;
- break;
- case Keys.Right:
- RectangleX += 10;
- break;
- case Keys.Left:
- RectangleX -= 10;
- break;
- }
- break;
- case Selection.Pie:
- switch (e.KeyCode) {
- case Keys.Down:
- PieY += 10;
- break;
- case Keys.Up:
- PieY -= 10;
- break;
- case Keys.Right:
- PieX += 10;
- break;
- case Keys.Left:
- PieX -= 10;
- break;
- }
- break;
- }
- this.Invalidate ();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement