CircleDragInfo AddCylinder(Cylinder area) { var info = new CircleDragInfo { Area = area, Center = new Ellipse { Fill = Brushes.Yellow, Width = centerSize, Height = centerSize, Opacity = 0.5 }, Circle = new Ellipse { Fill = Brushes.Red, StrokeThickness = 5, Opacity = 0.3, Stroke = Brushes.Yellow, Width = area.R * 2, Height = area.R * 2 } }; Canvas.SetLeft(info.Circle, area.X - area.R); Canvas.SetTop(info.Circle, area.Y - area.R); Canvas.SetLeft(info.Center, area.X - centerSize / 2.0); Canvas.SetTop(info.Center, area.Y - centerSize / 2.0); areaInfos.Add(info); canvas.Children.Add(info.Circle); canvas.Children.Add(info.Center); return info; }
void canvas_MouseDown(object sender, MouseButtonEventArgs e) { if (Keyboard.Modifiers == ModifierKeys.None) { e.Handled = true; var startPoint = e.GetPosition(canvas); if (dragInfo is NoArea) { if (circleButton.IsChecked == true) { // create circle canvas.CaptureMouse(); var area = new Cylinder { X = startPoint.X, Y = startPoint.Y, R = 0 }; var info = AddCylinder(area); dragInfo = info; Region.Areas.Add(info.Area); } else if (rectangleButton.IsChecked == true) { // create rectangle canvas.CaptureMouse(); var area = new RectangularArea { X = startPoint.X, Y = startPoint.Y, Height = 0, Width = 0 }; var info = AddRectangle(area); dragInfo = info; Region.Areas.Add(info.Area); } else if (deleteButton.IsChecked == true && e.Source is Shape) { // delete shape foreach (var info in areaInfos.ToArray()) { // remove area var circleDragInfo = info as CircleDragInfo; if (circleDragInfo != null && (circleDragInfo.Center == e.Source || circleDragInfo.Circle == e.Source)) { areaInfos.Remove(circleDragInfo); Region.Areas.Remove(circleDragInfo.Area); canvas.Children.Remove(circleDragInfo.Center); canvas.Children.Remove(circleDragInfo.Circle); } var rectangleDragInfo = info as RectangleDragInfo; if (rectangleDragInfo != null && (rectangleDragInfo.Rectangle == e.Source)) { areaInfos.Remove(rectangleDragInfo); Region.Areas.Remove(rectangleDragInfo.Area); canvas.Children.Remove(rectangleDragInfo.Rectangle); } if (info is NoArea) throw new Exception("NoArea should not be in the list"); } } } } }