调整矩形大小会移动位置

问题描述 投票:0回答:2

什么可能导致视频中出现这种奇怪的行为?我正在尝试调整矩形的大小。当鼠标移动顺畅时,一切似乎都按预期进行。我完全没有想法。

public void HandleMouseMove(object sender, MouseEventArgs e)
{
  if (isResizing)
  {
    Point newPoint = Mouse.GetPosition(MainCanvas);

    double left = Canvas.GetLeft(editor.SelectedArea);
    double top = Canvas.GetTop(editor.SelectedArea);
    double width = editor.SelectedArea.Width;
    double height = editor.SelectedArea.Height;

    double diffX = (newPoint.X - startPoint.X);
    double diffY = (newPoint.Y - startPoint.Y);

    switch (currentCorner)
    {
      case Corner.RM:
        double newWidth = width + diffX;
        if (newWidth < 0)
        {
          currentCorner = Corner.LM;
        }
        else
        {
          editor.SelectedArea.Width = newPoint.X - left;
        }
        break;

      case Corner.LM:
        double newLeft = left + diffX;
        double newWidthLm = width - diffX;
        if (newWidthLm < 0)
        {
          currentCorner = Corner.RM;
        }
        else
        {
          Canvas.SetLeft(editor.SelectedArea, newPoint.X);
          editor.SelectedArea.Width = width + (left - newPoint.X);
        }
        break;
      }
      startPoint = newPoint;
c# wpf
2个回答
0
投票

移动物体时,我建议仅使用拖动操作开始时的状态和当前鼠标位置。使用中间状态和增量可能会导致舍入错误和其他奇怪的情况。

所以程序可能看起来像这样的伪代码:

public void OnMouseDown(object sender, MouseEventArgs e){
    if(ClickedCorner(e)){
        state = States.Dragging;
        startPosition = Mouse.GetPosition(MainCanvas);
        currentCorner = GetCorner(startPosition);
        startRectangle = GetCurrentRectangle();
        cornerOffset = startRectangle.GetCornerPosition(currentCorner) - startPosition ;
    }
}

public void OnMouseMove(object sender, MouseEventArgs e){
    if(state == States.Dragging){
         var newPosition = Mouse.GetPosition(MainCanvas);
         var newCornerPosition = newPosition  + cornerOffset;
         var newRectangle = startRectangle.WithCornerPosition(currentCorner, newCornerPosition );
         UpdateUiRectangle(newRectangle);
    }
}
public void OnMouseUp(){
    state = States.Idle;
}

这应该确保不会发生任何奇怪的事情。


0
投票

当新宽度变为负值时,我们会遇到一个问题,即我们不能像往常一样简单地移动当前边缘,因为这会创建一个负宽度的矩形。因此,我们必须移动另一条边,而不是移动当前边。

switch (currentCorner)
{
    case Corner.RM:
        double newWidth = width + diffX;
        if (newWidth < 0)
        {
            newWidth = -newWidth;
            editor.SelectedArea.X -= newWidth;
            editor.SelectedArea.Width = newWidth;
            currentCorner = Corner.LM;
        }
        else
        {
            editor.SelectedArea.Width = newWidth;
        }
        break;
    case Corner.LM:
        double newLeft = left + diffX;
        double newWidthLm = width - diffX;
        if (newWidthLm < 0)
        {
            newWidthLm = -newWidthLm;
            editor.SelectedArea.X = editor.SelectedArea.Right;
            editor.SelectedArea.Width = newWidthLm;
            currentCorner = Corner.RM;
        }
        else
        {
            Canvas.SetLeft(editor.SelectedArea, newPoint.X);
            editor.SelectedArea.Width = newWidthLm;
        }
        break;
}
© www.soinside.com 2019 - 2024. All rights reserved.