private void DrawingApp_MouseMove(object sender, MouseEventArgs e) { if (mouse_down) { var mouse_pos = this.PointToClient(Cursor.Position); int size_x = mouse_pos.X - initial_mouse_pos.X; int size_y = mouse_pos.Y - initial_mouse_pos.Y; if (mode == "Move") { foreach (shape current_shape in shapequeue) { if (current_shape.is_selected) { //Change the position of every shape that has the is_selected boolean active. current_shape.pos_x = mouse_pos.X - current_shape.size_x / 2; current_shape.pos_y = mouse_pos.Y - current_shape.size_y / 2; this.Refresh(); } } } if (mode == "Resize") { foreach (shape current_shape in shapequeue) { if (current_shape.is_selected) { //Change the size of every shape that has the is_selected boolean active. current_shape.size_x = mouse_pos.X - current_shape.pos_x; current_shape.size_y = mouse_pos.Y - current_shape.pos_y; this.Refresh(); } } } else { label1.Text = "Coordinates: " + this.PointToClient(Cursor.Position).X + "x" + this.PointToClient(Cursor.Position).Y; //Every posible direction to draw a new shape is handled here. if (size_x < 0 && size_y > 0) { if (mode == "Create Rectangle") { //Just the outline rectangle is drawn first outline = new shape("Outline Rectangle", Color.Black, mouse_pos.X, initial_mouse_pos.Y, size_x * -1, size_y, false); } else if (mode == "Create Ellipse") { outline = new shape("Outline Ellipse", Color.Black, mouse_pos.X, initial_mouse_pos.Y, size_x * -1, size_y, false); } } else if (size_x > 0 && size_y < 0) { if (mode == "Create Rectangle") { outline = new shape("Outline Rectangle", Color.Black, initial_mouse_pos.X, mouse_pos.Y, size_x, size_y * -1, false); } else if (mode == "Create Ellipse") { outline = new shape("Outline Ellipse", Color.Black, initial_mouse_pos.X, mouse_pos.Y, size_x, size_y * -1, false); } } else if (size_x < 0 && size_y < 0) { if (mode == "Create Rectangle") { outline = new shape("Outline Rectangle", Color.Black, mouse_pos.X, mouse_pos.Y, size_x * -1, size_y * -1, false); } else if (mode == "Create Ellipse") { outline = new shape("Outline Ellipse", Color.Black, mouse_pos.X, mouse_pos.Y, size_x * -1, size_y * -1, false); } } else { if (mode == "Create Rectangle") { outline = new shape("Outline Rectangle", Color.Black, initial_mouse_pos.X, initial_mouse_pos.Y, size_x, size_y, false); } else if (mode == "Create Ellipse") { outline = new shape("Outline Ellipse", Color.Black, initial_mouse_pos.X, initial_mouse_pos.Y, size_x, size_y, false); } } this.Refresh(); } } }
private void DrawingApp_MouseUp(object sender, MouseEventArgs e) { mouse_down = false; //This is the code to select a shape while Move or Resize is selected. if (mode == "Move" | mode == "Resize") { //The reverse queue will pick the top most shape to be selected. foreach (shape current_shape in shapequeue.Reverse()) { if (new Rectangle(current_shape.pos_x, current_shape.pos_y, current_shape.size_x, current_shape.size_y).Contains(initial_mouse_pos)) { current_shape.is_selected = !current_shape.is_selected; //Make sure to break or else all the underlaying shapes will also be selected. break; } } } else { var mouse_pos = this.PointToClient(Cursor.Position); //A random color is chosen to give to the newly created shape. Color randomColor = Color.FromArgb(Random.Next(255), Random.Next(255), Random.Next(255)); //The size of the new shape can already be calculated. int size_x = mouse_pos.X - initial_mouse_pos.X; int size_y = mouse_pos.Y - initial_mouse_pos.Y; if (size_x < 0 && size_y > 0) { if (mode == "Create Rectangle") { //To not have any negative numbers some of the variables need to be multiplied by -1. shapequeue.Enqueue(new shape("Rectangle", randomColor, mouse_pos.X, initial_mouse_pos.Y, size_x * -1, size_y, false)); } else if (mode == "Create Ellipse") { shapequeue.Enqueue(new shape("Ellipse", randomColor, mouse_pos.X, initial_mouse_pos.Y, size_x * -1, size_y, false)); } } else if (size_x > 0 && size_y < 0) { if (mode == "Create Rectangle") { shapequeue.Enqueue(new shape("Rectangle", randomColor, initial_mouse_pos.X, mouse_pos.Y, size_x, size_y * -1, false)); } else if (mode == "Create Ellipse") { shapequeue.Enqueue(new shape("Ellipse", randomColor, initial_mouse_pos.X, mouse_pos.Y, size_x, size_y * -1, false)); } } else if (size_x < 0 && size_y < 0) { if (mode == "Create Rectangle") { shapequeue.Enqueue(new shape("Rectangle", randomColor, mouse_pos.X, mouse_pos.Y, size_x * -1, size_y * -1, false)); } else if (mode == "Create Ellipse") { shapequeue.Enqueue(new shape("Ellipse", randomColor, mouse_pos.X, mouse_pos.Y, size_x * -1, size_y * -1, false)); } } else { if (mode == "Create Rectangle") { shapequeue.Enqueue(new shape("Rectangle", randomColor, initial_mouse_pos.X, initial_mouse_pos.Y, size_x, size_y, false)); } else if (mode == "Create Ellipse") { shapequeue.Enqueue(new shape("Ellipse", randomColor, initial_mouse_pos.X, initial_mouse_pos.Y, size_x, size_y, false)); } } outline = null; } //Once the shape is added the whole window needs to be refreshed. this.Refresh(); }