C# 用“在画布上绘制椭圆”;“否定”;使用鼠标事件的宽度/高度

C# 用“在画布上绘制椭圆”;“否定”;使用鼠标事件的宽度/高度,c#,.net,wpf,C#,.net,Wpf,在MouseDownEvent上,我设置了要绘制的椭圆的左上角 public MyCircle(Point location) { ellipseObject = new Ellipse { Stroke = Brushes.Black, StrokeThickness = 2, Margin = new Thickness(location.X, location.Y, 0, 0)

在MouseDownEvent上,我设置了要绘制的椭圆的左上角

public MyCircle(Point location)
    {
        ellipseObject = new Ellipse
        {
            Stroke = Brushes.Black,
            StrokeThickness = 2,
            Margin = new Thickness(location.X, location.Y, 0, 0)
        };
    }
然后,在MouseMoveEvent上,我更新了宽度和高度属性,只要我不将鼠标移到椭圆左上角的上方或/或左侧,它就可以正常工作,在这种情况下,我会得到一个例外,即这些属性不能为负(这当然很有意义)

绘制线条时不存在此问题:

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

我知道这很琐碎,但我完全被这件事缠住了。如何处理绘制椭圆?

分别保存原点,并将椭圆边距的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;               
    }
}

单独保存原点,并将椭圆边距的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;               
    }
}

单独保存原点,并将椭圆边距的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;               
    }
}

单独保存原点,并将椭圆边距的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;               
    }
}

我在尝试创建裁剪工具时遇到了这个问题。问题是,当光标从起点开始为负X或负Y时,需要创建if语句。首先,你需要有一个全局点作为你的“起点”。还要指定一个全局当前点位置,稍后我们将讨论这个位置

public Point startingPoint;
public Point currentPoint;
然后,确保在试图将椭圆放置到的任何控件上都有onMouseDown事件

private void control_MouseDown(object sender, MouseEventArgs e)
    {
       startingPoint.X = e.X;
       startingPoint.Y = e.Y;
    }
然后,您需要在MouseMove事件中创建用于检查点(当前鼠标位置或起点)的语句是否具有较低的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);
    }

希望这有帮助

我在尝试创建裁剪工具时遇到了这个问题。问题是,当光标从起点开始为负X或负Y时,需要创建if语句。首先,你需要有一个全局点作为你的“起点”。还要指定一个全局当前点位置,稍后我们将讨论这个位置

public Point startingPoint;
public Point currentPoint;
然后,确保在试图将椭圆放置到的任何控件上都有onMouseDown事件

private void control_MouseDown(object sender, MouseEventArgs e)
    {
       startingPoint.X = e.X;
       startingPoint.Y = e.Y;
    }
然后,您需要在MouseMove事件中创建用于检查点(当前鼠标位置或起点)的语句是否具有较低的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);
    }

希望这有帮助

我在尝试创建裁剪工具时遇到了这个问题。问题是,当光标从起点开始为负X或负Y时,需要创建if语句。首先,你需要有一个全局点作为你的“起点”。还要指定一个全局当前点位置,稍后我们将讨论这个位置

public Point startingPoint;
public Point currentPoint;
然后,确保在试图将椭圆放置到的任何控件上都有onMouseDown事件

private void control_MouseDown(object sender, MouseEventArgs e)
    {
       startingPoint.X = e.X;
       startingPoint.Y = e.Y;
    }
然后,您需要在MouseMove事件中创建用于检查点(当前鼠标位置或起点)的语句是否具有较低的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);
    }

希望这有帮助

我在尝试创建裁剪工具时遇到了这个问题。问题是,当光标从起点开始为负X或负Y时,需要创建if语句。首先,你需要有一个全局点作为你的“起点”。还要指定一个全局当前点位置,稍后我们将讨论这个位置

public Point startingPoint;
public Point currentPoint;
然后,确保在试图将椭圆放置到的任何控件上都有onMouseDown事件

private void control_MouseDown(object sender, MouseEventArgs e)
    {
       startingPoint.X = e.X;
       startingPoint.Y = e.Y;
    }
然后,您需要在MouseMove事件中创建用于检查点(当前鼠标位置或起点)的语句是否具有较低的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);
    }

希望这有帮助

为什么不使用Math.Abs来删除负片呢?这样椭圆将以相反的方向增长,而不是抛出异常。我需要实现类似绘画的功能。为什么不使用Math.Abs来删除负片?这样椭圆将以与我希望的相反的方向增长,而不是抛出异常。我需要实现类似绘画的功能。为什么不使用Math.Abs来删除负片?这样椭圆将以与我希望的相反的方向增长,而不是抛出异常。我需要实现类似绘画的功能。为什么不使用Math.Abs来删除负片?这样椭圆将以与我希望的相反的方向增长,而不是抛出异常。我需要实现绘画般的功能。