移动TImage,并显示虚线:
在Form 中放置一 TImage,并装入图片。
同时在Form 单元中声明以下变量:
OP,ImgP:TPoint;
IsMoving:Boolean=false;
定义TImage 的三个事件处理过程:
OnMouseDown,OnMouseMove,OnMouseUp
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ExtCtrls;
type
TForm1 = class(TForm)
Image1: TImage;
procedure Image1MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure Image1MouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
procedure Image1MouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
OP,ImgP:TPoint;
IsMoving:Boolean=false;
implementation
{$R *.DFM}
procedure TForm1.Image1MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
ImgP:=Point(Image1.Left,Image1.top);
OP:=point(IMgP.x+x,ImgP.y+y);
IsMoving:=true;
end;
procedure TForm1.Image1MouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
begin
if not(IsMoving) then exit;
Image1.left:=ImgP.x+x-OP.x+Image1.left;
Image1.top :=ImgP.y+y-OP.y+Image1.top;
refresh;
canvas.Pen.Color:=clBlack;
canvas.Pen.style:=psDash;
canvas.Brush.style:=bsClear ;
canvas.Rectangle(Image1.left-2,Image1.top-2,
Image1.left+image1.width+2,Image1.top+Image1.Height+2);
end;
procedure TForm1.Image1MouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
IsMoving:=false;
refresh;
end;
end.