主题:关于图片拖动的问题请教!!
是这样的我在 ScrollBox 上加了 Panel 和 Image
Image 上的图片大于 Panel 的尺寸想让 Image 上图片可以跟随鼠标拖动。
我在 Image 上写了:
在 private 定义了:
Origin : TPoint;
Image_Left: Integer;
Image_Top : Integer;
Visa1 : TPoint;
Visa2 : TPoint;
CanMove : Boolean;
procedure TForm1.ImageMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
if (Image1.Width <= Panel1.Width) and (Image1.Height <= Panel1.Height) then
begin
CanMove:=false;
exit;
end;
if Button=mbLeft then
begin
Origin.X:= X;
Origin.Y:= Y;
Image_Left:= Image1.Left;
Image_Top := Image1.Top;
Visa1.X:= X-(Image1.Width - Panel1.Width + Image1.Left);
Visa1.y:= Y-(Image1.Height - Panel1.Height + Image1.Top);
Visa2.x:= X-Image1.left;
Visa2.y:= Y-Image1.top;
CanMove:= true;
end;
end;
procedure TForm1.ImageMouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
begin
if (Image1.Width > Panel1.Width) or (Image1.Height > Panel1.Height) then
if CanMove then
begin
if X < Visa1.X then X:= Visa1.X;
if X > Visa2.X then X:= Visa2.X;
if Y < Visa1.Y then Y:= Visa1.Y;
if Y > Visa2.Y then Y:= Visa2.Y;
if Image1.Left <= Panel1.Left then
Image1.Left:= Image_Left+(X-Origin.X);
if Image1.Top <= Panel1.Top then
Image1.Top:= Image_top+(Y-Origin.y);
end;
end;
procedure TForm1.ImageMouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
CanMove:=false;
end;
这样写不行,Image上的图片不能跟鼠标移动,有谁能帮我一下。。。。
Image 上的图片大于 Panel 的尺寸想让 Image 上图片可以跟随鼠标拖动。
我在 Image 上写了:
在 private 定义了:
Origin : TPoint;
Image_Left: Integer;
Image_Top : Integer;
Visa1 : TPoint;
Visa2 : TPoint;
CanMove : Boolean;
procedure TForm1.ImageMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
if (Image1.Width <= Panel1.Width) and (Image1.Height <= Panel1.Height) then
begin
CanMove:=false;
exit;
end;
if Button=mbLeft then
begin
Origin.X:= X;
Origin.Y:= Y;
Image_Left:= Image1.Left;
Image_Top := Image1.Top;
Visa1.X:= X-(Image1.Width - Panel1.Width + Image1.Left);
Visa1.y:= Y-(Image1.Height - Panel1.Height + Image1.Top);
Visa2.x:= X-Image1.left;
Visa2.y:= Y-Image1.top;
CanMove:= true;
end;
end;
procedure TForm1.ImageMouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
begin
if (Image1.Width > Panel1.Width) or (Image1.Height > Panel1.Height) then
if CanMove then
begin
if X < Visa1.X then X:= Visa1.X;
if X > Visa2.X then X:= Visa2.X;
if Y < Visa1.Y then Y:= Visa1.Y;
if Y > Visa2.Y then Y:= Visa2.Y;
if Image1.Left <= Panel1.Left then
Image1.Left:= Image_Left+(X-Origin.X);
if Image1.Top <= Panel1.Top then
Image1.Top:= Image_top+(Y-Origin.y);
end;
end;
procedure TForm1.ImageMouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
CanMove:=false;
end;
这样写不行,Image上的图片不能跟鼠标移动,有谁能帮我一下。。。。