2

我在我的 Windows 窗体上绘制了一个矩形,我想使用提供的手柄之一来调整它的大小!

在此处输入图像描述

Rectangle areaRect = new Rectangle(100,100, 300, 300);
Bool dragging = false;
Point ptOld = new Point(0, 0);

protected override void OnPaint(PaintEventArgs e)
{
  Graphics dcPaint = e.Graphics;
  dcPaint.DrawRectangle(rectPen, areaRect);
}

protected override void OnMouseDown(MouseEventArgs e)
{
  ptOld = new Point(e.X, e.Y);
  dragging = true;
}

protected override void OnMouseMove(MouseEventArgs e)
{
  if(dragging = true) 
  {
    Point ptNew = new Point(e.X, e.Y);
    Int32 handleSelected = GetSelectedHandle(ptNew);

   // Lets say I want to resize this rectangle using Handle 2 now.
    if(handleSelected == 2) 
    {
      // I am resizing this rectangle Width
      areaRect.X += ptNew.X - ptOld.X;
      areaRect.Width -= ptNew .X - ptOld.X;

      this.Invalidate();
    }
  }
}

protected override void OnMouseUp(MouseEventArgs e)
{
  dragging = false;
}

它会给我这样的效果。哪个是对的,

在此处输入图像描述

我想对此进行一些小的调整,我也想改变这个矩形的高度,当我移动点 2 时,我的点 7 应该保持原样,像这样......类似地当我移动第 4 点,我的第 5 点应该完好无损,第 7 点和第 2 点也是如此。

在此处输入图像描述

任何想法,如何进行,因为如果我改变高度,我的第 7 点位置也会改变?

4

2 回答 2

2

在 WinForms 中绘制MouseMove这样的图形不会很顺利。

在调整矩形大小之前,您基本上需要对矩形的引用。

我添加了以下代码来跟踪矩形和 8 个可拖动点:

private Point GetHandlePoint(int value) {
  Point result = Point.Empty;

  if (value == 1)
    result = new Point(areaRect.Left, areaRect.Top);
  else if (value == 2)
    result = new Point(areaRect.Left, areaRect.Top + (areaRect.Height / 2));
  else if (value == 3)
    result = new Point(areaRect.Left, areaRect.Bottom);
  else if (value == 4)
    result = new Point(areaRect.Left + (areaRect.Width / 2), areaRect.Top);
  else if (value == 5)
    result = new Point(areaRect.Left + (areaRect.Width / 2), areaRect.Bottom);
  else if (value == 6)
    result = new Point(areaRect.Right, areaRect.Top);
  else if (value == 7)
    result = new Point(areaRect.Right, areaRect.Top + (areaRect.Height / 2));
  else if (value == 8)
    result = new Point(areaRect.Right, areaRect.Bottom);

  return result;
}

private Rectangle GetHandleRect(int value) {
  Point p = GetHandlePoint(value);
  p.Offset(-2, -2);
  return new Rectangle(p, new Size(5, 5));
}

以下是我如何修改您的表单代码:

private Rectangle areaRect = new Rectangle(100, 100, 300, 300);
private Rectangle oldRect;
private int dragHandle = 0;
private Point dragPoint;

public Form1() {
  InitializeComponent();
  this.DoubleBuffered = true;
}

protected override void OnMouseDown(MouseEventArgs e) {
  for (int i = 1; i < 9; i++) {
    if (GetHandleRect(i).Contains(e.Location)) {
      dragHandle = i;
      oldRect = areaRect;
      dragPoint = GetHandlePoint(i);
    }
  }
  base.OnMouseDown(e);
}

protected override void OnMouseMove(MouseEventArgs e) {
  if (dragHandle == 1) {
    // to do
  } else if (dragHandle == 2) {
    int diff = dragPoint.X - e.Location.X;
    areaRect = new Rectangle(oldRect.Left - diff, oldRect.Top, oldRect.Width + diff, oldRect.Height);
  } else if (dragHandle == 7) {
    int diff = dragPoint.X - e.Location.X;
    areaRect = new Rectangle(oldRect.Left, oldRect.Top, oldRect.Width - diff, oldRect.Height);
  }

  if (dragHandle > 0)
    this.Invalidate();

  base.OnMouseMove(e);
}

protected override void OnMouseUp(MouseEventArgs e) {
  dragHandle = 0;
  base.OnMouseUp(e);
}

protected override void OnPaint(PaintEventArgs e) {
  e.Graphics.DrawRectangle(Pens.Red, areaRect);
  for (int i = 1; i < 9; i++) {
    e.Graphics.FillRectangle(Brushes.DarkRed, GetHandleRect(i));
  }
  base.OnPaint(e);
}

发布的代码仅执行第 2 点和第 7 点,但这应该会给您一些逻辑来使用。我确信这段代码可以改进,它只是一个工作示例。

于 2012-04-17T21:16:52.480 回答
1

虽然这有点旧,但这是此类任务的第一个(据我所知也是唯一有用的)结果。

我已经使用上述示例来增强和实施经过测试的解决方案。就我而言,我还想让矩形严格位于另一个矩形内。具体来说,我在 PictureBox 中绘制它,我希望它永远不会超出图片之外。这就是 max_width 和 max_height 对应的内容。

请注意,有时它有点有趣 - 当在某些方向上达到最小尺寸时,它会在另一个方向上重新调整尺寸。我决定我喜欢这种行为,并且它应该是一个功能。:)

protected void pictureBox1_OnMouseMove(object sender, MouseEventArgs e)
{
        // Where I started - where I stopped
        int x_diff = dragPoint.X - e.Location.X;
        int y_diff = dragPoint.Y - e.Location.Y;

        // Minimum values
        int small_offset = 5;
        int left = small_offset;
        int top = small_offset;
        int width = small_offset;
        int height = small_offset;

        // Max values
        int max_width = this.pictureBox1.Image.Width;
        int max_height = this.pictureBox1.Image.Height;

        if (dragHandle == 1)
        {
            left = Math.Max(oldRect.Left - x_diff, left);
            top = Math.Max(oldRect.Top - y_diff, top);
            width = Math.Min(Math.Max(oldRect.Width + x_diff, width), max_width - left - small_offset);
            height = Math.Min(Math.Max(oldRect.Height + y_diff, height), max_height - top - small_offset);
        }
        else if (dragHandle == 2)
        {
            left = Math.Max(oldRect.Left - x_diff, left);
            top = oldRect.Top;
            width = Math.Min(Math.Max(oldRect.Width + x_diff, width), max_width - left - small_offset);
            height = oldRect.Height;
        }
        else if (dragHandle == 3)
        {
            left = Math.Max(oldRect.Left - x_diff, left);
            top = oldRect.Top;
            width = Math.Min(Math.Max(oldRect.Width + x_diff, width), max_width - left - small_offset);
            height = Math.Min(Math.Max(oldRect.Height - y_diff, height), max_height - top - small_offset);
        }
        else if (dragHandle == 4)
        {
            left = oldRect.Left;
            top = Math.Max(oldRect.Top - y_diff, top);
            width = oldRect.Width;
            height = Math.Min(Math.Max(oldRect.Height + y_diff, height), max_height - top - small_offset);
        }
        else if (dragHandle == 5)
        {
            left = oldRect.Left;
            top = oldRect.Top;
            width = oldRect.Width;
            height = Math.Min(Math.Max(oldRect.Height - y_diff, height), max_height - top - small_offset);
        }
        else if (dragHandle == 6)
        {
            left = oldRect.Left;
            top = Math.Max(oldRect.Top - y_diff, top);
            width = Math.Min(Math.Max(oldRect.Width - x_diff, width), max_width - left - small_offset);
            height = Math.Min(Math.Max(oldRect.Height + y_diff, height), max_height - top - small_offset);
        }
        else if (dragHandle == 7)
        {
            left = oldRect.Left;
            top = oldRect.Top;
            width = Math.Min(Math.Max(oldRect.Width - x_diff, width), max_width - left - small_offset);
            height = oldRect.Height;

        }
        else if (dragHandle == 8)
        {
            left = oldRect.Left;
            top = oldRect.Top ;
            width = Math.Min(Math.Max(oldRect.Width - x_diff, width), max_width - left - small_offset);
            height = Math.Min(Math.Max(oldRect.Height - y_diff, height), max_height - top - small_offset);
        }


        if (dragHandle > 0)
        {
            areaRect = new Rectangle(left, top, width, height);
            this.Invalidate();
        }
}
于 2015-02-01T22:03:27.623 回答