如何删除使用 Canvas 创建的矩形?

How delete a rectangle created with Canvas?

我有一个使用鼠标在 TPaintBox 组件中绘制的矩形。 那么,如何在 TPaintBox 的“mouse up event”之后从我的应用程序中删除这个矩形(完全)?

欢迎任何建议。

这是我绘制这个矩形的代码:

private
    FSelecting: Boolean;
    FSelection: TRect;
  end;

procedure TForm1.PaintBox1MouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  FSelection.Left := X;
  FSelection.Top := Y;
  FSelecting := True;
end;

procedure TForm1.PaintBox1MouseMove(Sender: TObject; Shift: TShiftState; X,
  Y: Integer);
begin
  if FSelecting then
  begin
    FSelection.Right := X;
    FSelection.Bottom := Y;
    PaintBox1.Invalidate;
  end;
end;

procedure TForm1.PaintBox1MouseUp(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  FSelecting := False;
  FSelection.Right := X;
  FSelection.Bottom := Y;
  PaintBox1.Invalidate;
end;

procedure TForm1.PaintBox1Paint(Sender: TObject);
begin
  PaintBox1.Canvas.Brush.Color := clRed;
  PaintBox1.Canvas.Rectangle(FSelection);
end;

您不能删除绘图,您必须在其上绘制其他内容。

在您显示的代码中,您可以简单地将 FSelection 设置为一个空的 0x0 矩形,然后再次 Invalidate() PaintBox。它的正常图片会被绘制出来,你不会在它上面画一个矩形。

procedure TForm1.PaintBox1MouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  if Button = mbLeft then
  begin
    FSelection := Rect(X, Y, X, Y);
    FSelecting := True;
  end;
end;

procedure TForm1.PaintBox1MouseMove(Sender: TObject; Shift: TShiftState; X,
  Y: Integer);
begin
  if FSelecting then
  begin
    FSelection.Right := X;
    FSelection.Bottom := Y;
    PaintBox1.Invalidate;
  end;
end;

procedure TForm1.PaintBox1MouseUp(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  if (Button = mbLeft) and FSelecting then
  begin
    FSelecting := False;
    FSelection := Rect(0, 0, 0, 0);
    PaintBox1.Invalidate;
  end;
end;

procedure TForm1.PaintBox1Paint(Sender: TObject);
begin
  //...
  PaintBox1.Canvas.Brush.Color := clRed;
  PaintBox1.Canvas.Rectangle(FSelection);
end;

或者,假设您需要记住选定的矩形以用于其他用途,那么当 FSelecting 为 false 时,不要将选定的矩形绘制到 PaintBox 上。

procedure TForm1.PaintBox1MouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  if Button = mbLeft then
  begin
    FSelection := Rect(X, Y, X, Y);
    FSelecting := True;
  end;
end;

procedure TForm1.PaintBox1MouseMove(Sender: TObject; Shift: TShiftState; X,
  Y: Integer);
begin
  if FSelecting then
  begin
    FSelection.Right := X;
    FSelection.Bottom := Y;
    PaintBox1.Invalidate;
  end;
end;

procedure TForm1.PaintBox1MouseUp(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  if (Button = mbLeft) and FSelecting then
  begin
    FSelecting := False;
    PaintBox1.Invalidate;
  end;
end;

procedure TForm1.PaintBox1Paint(Sender: TObject);
begin
  //...
  if FSelecting then
  begin
    PaintBox1.Canvas.Brush.Color := clRed;
    PaintBox1.Canvas.Rectangle(FSelection);
  end;
end;

无论哪种方式,为了更好地衡量,您都应该绘制带有虚线边框的透明矩形,以便用户可以看到他们正在选择的内容而不会过于干扰:

procedure TForm1.PaintBox1Paint(Sender: TObject);
begin
  //...
  if FSelecting then
  begin
    PaintBox1.Canvas.Brush.Style := bsClear;
    PaintBox1.Canvas.Pen.Style := psDot;
    PaintBox1.Canvas.Rectangle(FSelection);
  end;
end;