请教怎么移动图像?(30分)

  • 主题发起人 主题发起人 liuer
  • 开始时间 开始时间
L

liuer

Unregistered / Unconfirmed
GUEST, unregistred user!
经常看到一些图像软件中,当图像超过屏莫时,不显示滚动条,而直接用变成手形的鼠标按住左
键拖动,这是怎么做的?
谢谢。
 
我在大富翁检索了很多个,可都没有答案
:(((((
 
用的是内存BMP呀,完整的图像在这里
屏幕上显示用的是一个TCanvas。
当你改变视口的时候,根据当前视口的大小把内存里的BMP的全部or指定部分贴到Canvas上



 

最简单的方法,将一个Image放在一个Panel上。注意控制大小。

procedure TForm1.Image1MouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
begin
if ssleft in shift then
releasecapture;
panel1.perform(WM_SYSCOMMAND,$F012,0);
end;
 
unit ViewImgU;

interface

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

type
TViewImgF = class(TForm)
Image1: TImage;
Label1: TLabel;
procedure Label1MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure Label1MouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
procedure Label1MouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure Label1DblClick(Sender: TObject);
private
moving:Boolean;
image_left:integer;
image_top:integer;
origin:Tpoint;
visa1:Tpoint;//鼠标当前位置相对图象右下角的坐标
visa2:Tpoint;//鼠标当前位置相对图象左上角的坐标
{ Private declarations }
public
{ Public declarations }
end;

var
ViewImgF: TViewImgF;
procedure ViewImage(CaptionString:String;Picture:TPicture);


implementation

uses AProFunU;
{$R *.dfm}

procedure ViewImage(CaptionString:String;Picture:TPicture);
var
ph,pw,ih,iw:Integer;
sw,sh:Integer;
begin
ViewImgF:=TViewImgF.Create(Application);
try
With ViewImgF do begin
sw:=ClientWidth;
sh:=ClientHeight;
Image1.Picture.Assign(Picture);
Caption:=CaptionString;
ih:=sh;
iw:=sw;
ph:=Image1.Picture.Height;
pw:=Image1.Picture.Width;
if (ph>ih) and (pw>iw) then begin
Label1.Cursor:=crSizeAll;
end else if (ph>ih) then begin
Image1.Left:=Trunc((sw-Image1.Picture.Width)/2);
Label1.Cursor:=crSizeNS;
end else if (pw>iw) then begin
Image1.Top:=Trunc((sh-Image1.Picture.Height)/2);
Label1.Cursor:=crSizeWE;
end else begin
Image1.Left:=Trunc((sw-Image1.Picture.Width)/2);
Image1.Top:=Trunc((sh-Image1.Picture.Height)/2);
end;
ShowModal;
end;
finally
ViewImgF.Free;
end;
end;

procedure TViewImgF.Label1MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
var
ph,pw,ih,iw:Integer;
sw,sh:Integer;
begin
if (Button=mbleft) and (not(Label1.Cursor=crDefault)) then begin
sw:=ClientWidth;
sh:=ClientHeight;
Moving:=True;
origin.x:=X;
origin.y:=Y;
image_left:=Image1.Left;
image_top:=Image1.Top;
ih:=sh;
iw:=sw;
ph:=Image1.Picture.Height;
pw:=Image1.Picture.Width;
if (ph>ih) and (pw>iw) then begin
visa1.x:=X-(Image1.Width-sw+Image1.Left);
visa1.y:=Y-(Image1.Height-sh+Image1.Top);
end else if (ph>ih) then begin
visa1.x:=X-(((Image1.Width-sw) Div 2)+Image1.Left);
visa1.y:=Y-(Image1.Height-sh+Image1.Top);
end else if (pw>iw) then begin
visa1.x:=X-(Image1.Width-sw+Image1.Left);
visa1.y:=Y-(((Image1.Height-sh) Div 2)+Image1.Top);
end;
visa2.x:=X-Image1.left;
visa2.y:=Y-Image1.top;
end;
end;

procedure TViewImgF.Label1MouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
begin
if moving then begin
if X>visa2.x then X:=visa2.x;
if Y>visa2.y then Y:=visa2.y;
if X<visa1.x then X:=visa1.x;
if Y<visa1.y then Y:=visa1.y;
image1.left:=image_left+(X-origin.x);
image1.top:=image_top+(Y-origin.y);
end;
end;

procedure TViewImgF.Label1MouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
if Button=mbLeft then begin
Moving:=False;
end;
end;

procedure TViewImgF.Label1DblClick(Sender: TObject);
begin
Close;
end;

end.
 
谢谢,已收藏
 
后退
顶部