现将我写的程序附上,请各位老师指正:
(这个小程序实现了,图片的放大,缩小和平移,我已经实现了用Tbutton 来实现,想请教如何用鼠标控制图片的自由平移[单击鼠标左键不放,平移图片,左键放开平移结束的功能]等于photoshop 中的 hand tool 工具)
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls, StdCtrls;
type
TForm1 = class(TForm)
Edit1: TEdit;
Button1: TButton;
Image1: TImage;
Edit2: TEdit;
Button2: TButton;
Button3: TButton;
Button4: TButton;
Button5: TButton;
Button6: TButton;
Label1: TLabel;
Label2: TLabel;
procedure FormMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure FormCreate(Sender: TObject);
procedure Image1MouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
procedure Image1MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button6Click(Sender: TObject);
procedure Button5Click(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
a,x1,x2,y1,y2:integer;
implementation
{$R *.dfm}
procedure TForm1.FormMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
edit1.Text:=inttostr(x);
edit2.Text:=inttostr
;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
x1:=0;
y1:=0;
x2:=image1.Width;
y2:=image1.Height;
end;
procedure TForm1.Image1MouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
begin
edit1.Text:=inttostr(x);
edit2.Text:=inttostr
;
end;
procedure TForm1.Image1MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
var
bitmap: Tbitmap;
myrect,myother:trect;
begin
if a=1 then
begin
x1:=x1+5;
y1:=y1+5;
x2:=x2-5;
y2:=y2-5;
Myother:= Rect(0,0,image1.Width,image1.Height);
myrect := rect(x1,y1,x2,y2);
bitmap := Tbitmap.Create;
bitmap.LoadFromFile('D:/新建文件夹/新建文件夹/未命名2.bmp');
image1.canvas.fillrect(myother);
image1.Canvas.CopyRect(Myother,Bitmap.Canvas,Myrect);
end;
if a=0 then
begin
x1:=x1-5;
y1:=y1-5;
x2:=x2+5;
y2:=y2+5;
Myother:= Rect(0,0,image1.Width,image1.Height);
myrect := rect(x1,y1,x2,y2);
bitmap := Tbitmap.Create;
bitmap.LoadFromFile('D:/新建文件夹/新建文件夹/未命名2.bmp');
image1.canvas.fillrect(myother);
image1.Canvas.CopyRect(Myother,Bitmap.Canvas,Myrect);
end;
if a=3 then
begin
x1:=x1;
y1:=y1-10;
x2:=x2;
y2:=y2-10;
if y2<image1.Height then
begin
a:=4;
end;
Myother:= Rect(0,0,image1.Width,image1.Height);
myrect:=rect(x1,y1,x2,y2);
bitmap := Tbitmap.Create;
bitmap.LoadFromFile('D:/新建文件夹/新建文件夹/未命名2.bmp');
image1.canvas.fillrect(myother);
image1.Canvas.CopyRect(Myother,Bitmap.Canvas,Myrect);
end;
if a=4 then
begin
x1:=x1;
y1:=y1+10;
x2:=x2;
y2:=y2+10;
if y2<image1.Height then
begin
a:=3;
end;
Myother:= Rect(0,0,image1.Width,image1.Height);
myrect:=rect(x1,y1,x2,y2);
bitmap := Tbitmap.Create;
bitmap.LoadFromFile('D:/新建文件夹/新建文件夹/未命名2.bmp');
image1.canvas.fillrect(myother);
image1.Canvas.CopyRect(Myother,Bitmap.Canvas,Myrect);
end;
if a=5 then
begin
x1:=x1+10;
y1:=y1;
x2:=x2+10;
y2:=y2;
if (x2-630)>image1.width then
begin
a:=6;
end
else
Myother:= Rect(0,0,image1.Width,image1.Height);
myrect:=rect(x1,y1,x2,y2);
bitmap := Tbitmap.Create;
bitmap.LoadFromFile('D:/新建文件夹/新建文件夹/未命名2.bmp');
image1.canvas.fillrect(myother);
image1.Canvas.CopyRect(Myother,Bitmap.Canvas,Myrect);
end;
if a=6 then
begin
x1:=x1-10;
y1:=y1;
x2:=x2-10;
y2:=y2;
if x1=0 then
begin
a:=5;
end;
Myother:= Rect(0,0,image1.Width,image1.Height);
myrect:=rect(x1,y1,x2,y2);
bitmap := Tbitmap.Create;
bitmap.LoadFromFile('D:/新建文件夹/新建文件夹/未命名2.bmp');
image1.canvas.fillrect(myother);
image1.Canvas.CopyRect(Myother,Bitmap.Canvas,Myrect);
end;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
a:=1;
end;
procedure TForm1.Button3Click(Sender: TObject);
begin
a:=0;
end;
procedure TForm1.Button6Click(Sender: TObject);
begin
a:=3;
end;
procedure TForm1.Button5Click(Sender: TObject);
begin
a:=4;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
a:=5;
end;
procedure TForm1.Button4Click(Sender: TObject);
begin
a:=6;
end;
end.