使用鼠标事件在画布上绘制具有“负”宽度/高度的椭圆

时间:2015-02-22 00:11:41

标签: c# .net wpf

在MouseDownEvent上我设置Ellipse的左上角我正在尝试绘制。

public MyCircle(Point location)
    {
        ellipseObject = new Ellipse
        {
            Stroke = Brushes.Black,
            StrokeThickness = 2,
            Margin = new Thickness(location.X, location.Y, 0, 0)
        };
    }

然后在MouseMoveEvent上我更新Width和Height属性,只要我不将鼠标移动到我的椭圆左上角的左上方和/或左侧,它就可以正常工作了,在这种情况下,我得到了这些属性的异常不能是消极的(这当然是完全合理的)。

public void Draw(Point location)
    {
        if (ellipseObject != null)
        {                
            ellipseObject.Width = location.X - ellipseObject.Margin.Left;
            ellipseObject.Height = location.Y - ellipseObject.Margin.Top;               
        }
    }

绘图线不存在问题:

public void Draw(Point location)
    {
        lineObject.X2 = location.X;
        lineObject.Y2 = location.Y;
    }

我知道这是微不足道的,但我完全坚持这一点。我该如何处理绘制椭圆?

2 个答案:

答案 0 :(得分:1)

单独保存原点并将椭圆的边距的X和Y属性设置为鼠标位置,将宽度和高度设置为鼠标和原点之间的距离。

未测试:

public MyCircle(Point location)
{
    ellipseObject = new Ellipse
    {
        Stroke = Brushes.Black,
        StrokeThickness = 2,
        Margin = new Thickness(location.X, location.Y, 0, 0)
        Tag = new Point(location.X, location.Y)
    };
}

public void Draw(Point location)
{
    if (ellipseObject != null)
    {                
        Point o = (Point)ellipseObject.Tag;
        double x = Math.Min(location.X, o.Left);
        double y = Math.Min(location.Y, o.Top);
        double width = Math.Abs(Math.Max(location.X, o.Left) - x);
        double height = Math.Abs(Math.Max(location.Y, o.Top) - y);
        ellipseObject.Margin.X = x;
        ellipseObject.Margin.Y = y;
        ellipseObject.Width = width;
        ellipseObject.Height = height;               
    }
}

答案 1 :(得分:1)

尝试创建裁剪工具时遇到了这个问题。问题是你需要创建if语句,以便当光标从起点变为负X或负Y时。对于初学者,您需要有一个全局点,您将其标记为“开始”点。同时指定我们将在一分钟内讨论的全局当前点位置。

public Point startingPoint;
public Point currentPoint;

然后,确保在尝试放置椭圆的任何控件上都有onMouseDown事件。

private void control_MouseDown(object sender, MouseEventArgs e)
    {
       startingPoint.X = e.X;
       startingPoint.Y = e.Y;
    }

然后,您需要在MouseMove事件中创建if语句以检查点(当前鼠标位置或起始点)具有较低的X / Y值

private void control_MouseMove(object sender, MouseEventArgs e)
    {
          //The below point is what we'll draw the ellipse with.
          Point ellipsePoint;
          Ellipse ellipseObject = new Ellipse();
          currentPoint.X = e.X;
          currentPoint.Y = e.Y;
         //Then we need to get the proper width/height;
          if (currentPoint.X >= startingPoint.X)
          {
                 ellipsePoint.X = startingPoint.X;
                 ellipseObject.Width = currentPoint.X - startingPoint.X;
          }
          else
          {
                ellipsePoint.X = currentPoint.X;
                ellipseObject.Width = startingPoint.X - currentPoint.X;
          }
          if (currentPoint.Y >= startingPoint.Y)
          {
                ellipsePoint.Y = startingPoint.Y;
                ellipseObject.Height = currentPoint.Y - startingPoint.Y;
          }
          else
          {
                ellipsePoint.Y = currentPoint.Y;
                ellipseObject.Height = startingPoint.Y - currentPoint.Y;
          }
           ellipseObject.Stroke = Brushes.Black;
           ellipseObject.StrokeThickness = 2;
           ellipseObject.Margin = new Thickness(ellipsePoint.X, ellipsePoint.Y, 0, 0);
    }

希望这有帮助!