从WPF中删除某些形状

时间:2017-08-13 03:08:01

标签: c# wpf

我尝试使用wpf并希望能够通过左键单击创建形状,但是右键单击删除鼠标指针当前悬停的形状,然而,会发生什么反而是最后一个形状已删除已创建。我该如何解决?

这会创建形状:

 private List<Shape> shapes = new List<Shape>();
    private Shape shape;
    public static Random rand = new Random();

    private Rectangle CreateRectangle()
    {

        int height = rand.Next(0, 151);
        int width = rand.Next(0, 101);
        byte alpha = (byte)rand.Next(0, 256);
        byte alpha2 = (byte)rand.Next(0, 256);
        byte alpha3 = (byte)rand.Next(0, 256);
        Rectangle rect = new Rectangle();
        rect.Width = width;
        rect.Height = height;
        SolidColorBrush color = new SolidColorBrush();
        color.Color = Color.FromRgb(alpha, alpha2, alpha3);
        rect.Fill = color;
        return rect;
    }

    private Ellipse CreateEllipse()
    {

        int height = rand.Next(0, 151);
        int width = rand.Next(0, 101);
        byte alpha = (byte)rand.Next(0, 256);
        byte alpha2 = (byte)rand.Next(0, 256);
        byte alpha3 = (byte)rand.Next(0, 256);
        Ellipse ellipse = new Ellipse();
        ellipse.Width = width;
        ellipse.Height = height;
        SolidColorBrush color = new SolidColorBrush();
        color.Color = Color.FromRgb(alpha, alpha2, alpha3);
        ellipse.Fill = color;
        return ellipse;

    }

    private void ColumnDefinition_OnClick(object sender, MouseButtonEventArgs e)
    {
        Point area = System.Windows.Input.Mouse.GetPosition(mc);
        int num = rand.Next(1, 3);
        switch (num)
        {
            case 1:
              shape = CreateRectangle();

                mc.Children.Add(shape);
                shapes.Add(shape);
                Canvas.SetLeft(shape, area.X);
                Canvas.SetTop(shape, area.Y);
                break;
            case 2:
                shape = CreateEllipse();
                 mc.Children.Add(shape);
                shapes.Add(shape);
                Canvas.SetLeft(shape, area.X);
                Canvas.SetTop(shape, area.Y);
                break;
        }
    }

    private void Clear_Click(object sender, RoutedEventArgs e)
    {
        mc.Children.Clear();
    }

这是什么意思删除形状:

    private void mc_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
    {

        mc.Children.Remove(shape);
        shapes.Remove(shape);
    }
}

}

非常感谢任何帮助。

3 个答案:

答案 0 :(得分:0)

我不确定我在代码示例中是否拥有所需的一切,但它看起来像是正在创建一个形状并添加到列表中,但也分配给一个名为“shape”的成员,该成员被最新的覆盖形状创造。然后,您将始终删除最新的形状。您应该将mousedown处理程序附加到每个形状,从对象发送方捕获形状,然后将其从列表中删除。

private void shape_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
{
    Shape s = sender as Shape;
    mc.Children.Remove(s);
    shapes.Remove(s);
}

答案 1 :(得分:0)

我建议你学习WPF中的热门测试。看看this问题我在哪里回答了如何在鼠标下获取几何体并将其删除。您只需要为您的任务调整此代码,但这非常简单。

XAML:

<Canvas Mouse.MouseDown="Canvas_MouseDown">

代码背后:

private void Canvas_MouseDown(object sender, MouseButtonEventArgs e)
{
    var canvas = sender as Canvas;
    if (canvas == null)
        return;

    HitTestResult hitTestResult = VisualTreeHelper.HitTest(canvas, e.GetPosition(canvas));
    var shape = hitTestResult.VisualHit as Shape;
    if (shape == null)
        return;

    canvas.Children.Remove(shape);
    shapes.Remove(shape); // I think you don't need list of shapes
}

答案 2 :(得分:0)

MouseButtonEventArgs.OriginalSource属性会返回点击的Shape,因此您可以像这样实现mc_MouseRightButtonDown事件处理程序:

private void mc_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
{
    Shape clickedShape = e.OriginalSource as Shape;
    if (clickedShape != null)
    {
        mc.Children.Remove(clickedShape);
        shapes.Remove(clickedShape);
    }
}