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.