如何在IMAGE上用鼠标拖出矩形?(50分)

  • 主题发起人 主题发起人 ywws
  • 开始时间 开始时间
Y

ywws

Unregistered / Unconfirmed
GUEST, unregistred user!
具有橡皮筋的效果,并且得到左上和右下角的坐标。
 
unit draw;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ExtCtrls;

type
TForm1 = class(TForm)
procedure FormMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure FormMouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
procedure FormMouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
private
p1,p2:TPoint;
public
{ Public declarations }
end;

var
Form1: TForm1;
lastx,lasty:integer;
implementation

{$R *.DFM}

procedure TForm1.FormMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
P1.x:=x;
P1.y:=y;
p2:=p1;
canvas.pen.Mode:=pmNot;
canvas.pen.Color:=clblack;
canvas.brush.Style:=bsclear;
end;

procedure TForm1.FormMouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
begin
if (shift=[ssleft]) then begin
canvas.Rectangle(p1.x,p1.y,p2.x,p2.y); //擦除
canvas.Rectangle(p1.x,p1.y,x,y); //重画
p2.x:=x;
p2.y:=y;
end;

end;

procedure TForm1.FormMouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
canvas.Rectangle(p1.x,p1.y,p2.x,p2.y); //擦除}
end;

end.
 
随时纪录鼠标位置,然后在image_OnMouseMove里面用:
canvas.DrawFocusRect
 
谢谢,我好象以前看到一个函数不用擦除的,不知有没有?
 
多人接受答案了。
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
D
回复
0
查看
2K
DelphiTeacher的专栏
D
D
回复
0
查看
1K
DelphiTeacher的专栏
D
后退
顶部