1

我正在尝试从我的鼠标位置向我的网格添加一个椭圆。如何定义椭圆的绘制位置?

这是我的鼠标按下事件:

private void GridCanvas_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
    _sensorPoint = Mouse.GetPosition(GridCanvas);
    AddSensor();
}

还有我的 AddSensor() 方法:

private void AddSensor()
{
    Ellipse sensorEllipse = new Ellipse();
    SolidColorBrush solidColorBrush = new SolidColorBrush();


    solidColorBrush.Color = Color.FromArgb(0, 0, 0, 0);
    sensorEllipse.Fill = solidColorBrush;
    sensorEllipse.StrokeThickness = 2;
    sensorEllipse.Stroke = Brushes.Black;
    sensorEllipse.Width = 10;
    sensorEllipse.Height = 10;

    GridCanvas.Children.Add(sensorEllipse);
}

如何在画布上单击鼠标的位置添加 sensorEllipse?

4

1 回答 1

1

您可以使用以下任何方法设置位置:

private void AddSensor()
{
        Ellipse sensorEllipse = new Ellipse();
        SolidColorBrush solidColorBrush = new SolidColorBrush();

        solidColorBrush.Color = Color.FromArgb(0, 0, 0, 0);

        sensorEllipse.Fill = solidColorBrush;

        sensorEllipse.SetValue(Canvas.LeftProperty, _sensorPoint.X);
        sensorEllipse.SetValue(Canvas.TopProperty, _sensorPoint.Y);

        //Canvas.SetLeft(sensorEllipse, _sensorPoint.X);
        //Canvas.SetTop(sensorEllipse, _sensorPoint.Y);

        sensorEllipse.StrokeThickness = 2;
        sensorEllipse.Stroke = Brushes.Black;
        sensorEllipse.Width = 10;
        sensorEllipse.Height = 10;

        //adding event handler for right mouse down:
        sensorEllipse.MouseRightButtonDown += new MouseButtonEventHandler(sensorEllipse_MouseRightButtonDown);

        GridCanvas.Children.Add(sensorEllipse);
}

编辑 用于添加/删除省略号检查,按下哪个鼠标按钮GridCanvas_MouseLeftButtonDown

private void GridCanvas_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
  _sensorPoint = Mouse.GetPosition(GridCanvas);
  AddSensor();
}

然后为新椭圆添加事件处理程序:

    void sensorEllipse_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
    {
        Ellipse ellipse = sender as Ellipse;
        GridCanvas.Children.Remove(ellipse);
    }
于 2012-08-16T06:38:41.967 回答