private void KhungVe_MouseDown(object sender, MouseEventArgs e) { if (currentShape == CurrentShape.NoDrawing) { if (isControlKeyPress) { for (int i = 0; i < shapes.Count; i++) { if (shapes[i].IsHit(e.Location)) { shapes[i].IsSelected = !shapes[i].IsSelected; KhungVe.Invalidate(); break; } } } else { shapes.ForEach(shape => shape.IsSelected = false); KhungVe.Invalidate(); for (int i = 0; i < shapes.Count; i++) { if (shapes[i].IsHit(e.Location)) { selectedShape = shapes[i]; shapes[i].IsSelected = true; KhungVe.Invalidate(); break; } } if (selectedShape != null) { isMovingShape = true; previousPoint = e.Location; } else { isMouseSelect = true; selectedRegion = new System.Drawing.Rectangle(e.Location, new Size(0, 0)); } } } else { isMouseDown = true; shapes.ForEach(shape => shape.IsSelected = false); if (currentShape == CurrentShape.Line) { Line line = new Line { Begin = e.Location, LineWidth = cbSize.SelectedIndex, Color = btnColors.BackColor, DashStyle = DashStyle.Solid }; shapes.Add(line); } else if (currentShape == CurrentShape.Rectangle) { Shapes.Rectangle rectangle = new Shapes.Rectangle { Begin = e.Location, LineWidth = cbSize.SelectedIndex, Color = btnColors.BackColor, DashStyle = DashStyle.Solid }; shapes.Add(rectangle); } else if (currentShape == CurrentShape.Ellipse) { Ellipse ellipse = new Ellipse { Begin = e.Location, LineWidth = cbSize.SelectedIndex, Color = btnColors.BackColor, DashStyle = DashStyle.Solid }; shapes.Add(ellipse); } else if (currentShape == CurrentShape.Square) { Square square = new Square { Begin = e.Location, LineWidth = cbSize.SelectedIndex, Color = btnColors.BackColor, DashStyle = DashStyle.Solid }; shapes.Add(square); } else if (currentShape == CurrentShape.Circle) { Circle circle = new Circle { Begin = e.Location, LineWidth = cbSize.SelectedIndex, Color = btnColors.BackColor, DashStyle = DashStyle.Solid }; shapes.Add(circle); } else if (currentShape == CurrentShape.Polygon) { if (!isDrawPolygon) { Polygon polygon = new Polygon { LineWidth = cbSize.SelectedIndex, Color = btnColors.BackColor, DashStyle = DashStyle.Solid }; polygon.Points.Add(e.Location); polygon.Points.Add(e.Location); shapes.Add(polygon); isDrawPolygon = true; } else { Polygon polygon = shapes[shapes.Count - 1] as Polygon; polygon.Points[polygon.Points.Count - 1] = e.Location; polygon.Points.Add(e.Location); } isMouseDown = false; } else if (currentShape == CurrentShape.Pyramid) { if (!isDrawPyramid) { Pyramid pyramid = new Pyramid { LineWidth = cbSize.SelectedIndex, Color = btnColors.BackColor, DashStyle = DashStyle.Solid }; pyramid.Points.Add(e.Location); pyramid.Points.Add(e.Location); shapes.Add(pyramid); isDrawPyramid = true; } else { Pyramid pyramid = shapes[shapes.Count - 1] as Pyramid; pyramid.Points[pyramid.Points.Count - 1] = e.Location; pyramid.Points.Add(e.Location); if (pyramid.Points.Count == 4) { isDrawPyramid = false; } } isMouseDown = false; } else if (currentShape == CurrentShape.Bezier) { if (!isDrawBezier) { Curve bezier = new Curve { LineWidth = cbSize.SelectedIndex, Color = btnColors.BackColor, DashStyle = DashStyle.Solid }; bezier.Points.Add(e.Location); bezier.Points.Add(e.Location); shapes.Add(bezier); isDrawBezier = true; } else { Curve bezier = shapes[shapes.Count - 1] as Curve; if (bezier.Points.Count < 4) { bezier.Points[bezier.Points.Count - 1] = e.Location; bezier.Points.Add(e.Location); } else { isDrawBezier = false; FindCurveRegion(bezier); } } isMouseDown = false; } } }
public void CreateAShape(MouseEventArgs e) { switch (status) { case Status.Line: { Shape shape = new Line(); shape.ItsPen = new Pen(btnColor.BackColor, Convert.ToInt16(numThickness.Value)); shape.ItsPen.DashStyle = (DashStyle)cbbStyle.SelectedIndex; shape.ListPoints[0] = shape.ListPoints[1] = e.Location; listShapes.Add(shape); break; } case Status.Rectangle: { Shape shape = new Rectangle(); shape.ItsPen = new Pen(btnColor.BackColor, Convert.ToInt16(numThickness.Value)); shape.ItsPen.DashStyle = (DashStyle)cbbStyle.SelectedIndex; shape.ListPoints[0] = shape.ListPoints[1] = e.Location; listShapes.Add(shape); break; } case Status.FillRectangle: { Shape shape = new FillRectangle(); shape.ItsPen = new Pen(btnColor.BackColor, Convert.ToInt16(numThickness.Value)); shape.ItsPen.DashStyle = (DashStyle)cbbStyle.SelectedIndex; shape.SolidBrush = solidBrush; shape.HatchBrush = hatchBrush; shape.ListPoints[0] = shape.ListPoints[1] = e.Location; listShapes.Add(shape); break; } case Status.Ellipse: { Shape shape = new Ellipse(); shape.ItsPen = new Pen(btnColor.BackColor, Convert.ToInt16(numThickness.Value)); shape.ItsPen.DashStyle = (DashStyle)cbbStyle.SelectedIndex; shape.ListPoints[0] = shape.ListPoints[1] = e.Location; listShapes.Add(shape); break; } case Status.FillEllipse: { Shape shape = new FillEllipse(); shape.ItsPen = new Pen(btnColor.BackColor, Convert.ToInt16(numThickness.Value)); shape.ItsPen.DashStyle = (DashStyle)cbbStyle.SelectedIndex; shape.SolidBrush = solidBrush; shape.HatchBrush = hatchBrush; shape.ListPoints[0] = shape.ListPoints[1] = e.Location; listShapes.Add(shape); break; } case Status.Curve: { if (isCurve) { if (isSecondClickCurve) { listShapes[listShapes.Count - 1].ListPoints[2] = e.Location; } else { listShapes[listShapes.Count - 1].ListPoints[1] = e.Location; } } else { Shape shape = new Curve(); shape.ItsPen = new Pen(btnColor.BackColor, Convert.ToInt16(numThickness.Value)); shape.ItsPen.DashStyle = (DashStyle)cbbStyle.SelectedIndex; shape.ListPoints[0] = shape.ListPoints[1] = shape.ListPoints[2] = shape.ListPoints[3] = e.Location; listShapes.Add(shape); } break; } case Status.Polygon: { if (stopDrawPolygon) { Shape shape = new Polygon(); shape.ItsPen = new Pen(btnColor.BackColor, Convert.ToInt16(numThickness.Value)); shape.ItsPen.DashStyle = (DashStyle)cbbStyle.SelectedIndex; listShapes.Add(shape); listShapes[listShapes.Count - 1].ListPoints[0] = e.Location; stopDrawPolygon = false; } Point point = e.Location; listShapes[listShapes.Count - 1].ListPoints.Add(point); listShapes[listShapes.Count - 1].ListDxDy.Add(point); break; } case Status.FillPolygon: { if (stopFillPolygon) { Shape shape = new FillPolygon(); shape.ItsPen = new Pen(btnColor.BackColor, Convert.ToInt16(numThickness.Value)); shape.ItsPen.DashStyle = (DashStyle)cbbStyle.SelectedIndex; shape.SolidBrush = solidBrush; shape.HatchBrush = hatchBrush; listShapes.Add(shape); listShapes[listShapes.Count - 1].ListPoints[0] = e.Location; stopFillPolygon = false; } Point point = e.Location; listShapes[listShapes.Count - 1].ListPoints.Add(point); listShapes[listShapes.Count - 1].ListDxDy.Add(point); break; } } }