Delphi 如何删除使用画布创建的矩形?
我用鼠标在Delphi 如何删除使用画布创建的矩形?,delphi,tcanvas,Delphi,Tcanvas,我用鼠标在tpainbox组件中绘制了一个矩形。 那么,在tpainbox的“鼠标向上移动事件”之后,如何从我的应用程序中删除这个矩形(全部) 欢迎提出任何建议 以下是绘制此矩形的代码: private FSelecting: Boolean; FSelection: TRect; end; procedure TForm1.PaintBox1MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftS
tpainbox
组件中绘制了一个矩形。
那么,在tpainbox
的“鼠标向上移动事件”之后,如何从我的应用程序中删除这个矩形(全部)
欢迎提出任何建议
以下是绘制此矩形的代码:
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;
不能删除图形,必须在其上方绘制其他图形
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;
在显示的代码中,只需将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;
或者,假设您需要记住所选矩形以便与其他东西一起使用,那么只要在fselection
为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;
在绘制矩形之前保存该区域,并在需要时将其恢复原状。或者,只需重新绘制先前在该点绘制的区域,而不是保存先前的图形。为什么要使用实心红色框来选择某个对象,而不是使用带边框的透明框?您想让用户看到所选内容吗?非常感谢您的帮助。