M
majorsoft
Unregistered / Unconfirmed
GUEST, unregistred user!
我为了避免直接拖动Image导致的重画,我使用了ScrollBox做为它的容器,
其中一个背景image充满Scrollbox整个客户区,在ScrollBox中还有很多其他的小Image,和Tshape
现在要求在容器中拖动时,使所有的图片一起移动,相对位置不能变,并且能有象ACDSee中的拖动效果。
我实现了下,发现还是有点抖动。请大家指点。
unit Unit2;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, jpeg, ExtCtrls, StdCtrls;
type
TForm1 = class(TForm)
GroupBox1: TGroupBox;
ScrollBox1: TScrollBox;
Image1: TImage;
Button1: TButton;
Button2: TButton;
Edt_x: TEdit;
Edt_y: TEdit;
Shape2: TShape;
Shape3: TShape;
Button3: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
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);
procedure Button3Click(Sender: TObject);
private
originPos:Tpoint;
originScrbarPos:Tpoint;
canDrag:boolean;
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
begin
image1.Picture.LoadFromFile(ExtractFilePath(Application.ExeName)+'map.wmf');
end;
procedure TForm1.Image1MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
originPos.X:=x;
OriginPos.Y:=y;
originScrbarPos.X:=scrollbox1.HorzScrollBar.Position;
originScrBarPos.Y:=ScrollBox1.VertScrollBar.Position;
CanDrag:=true;
edt_x.Text:=inttostr(x);
edt_y.Text:=inttostr;
end;
procedure TForm1.Image1MouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
var
MovX,MovY:integer;
begin
if canDrag then
begin
screen.Cursor:=crHandPoint;
MovX:=originPos.x-x;
MovY:=originPos.y-y;
if (originScrbarPos.X+Movx)>0 then
Scrollbox1.HorzScrollBar.Position:=originScrbarPos.X+MovX
else
Scrollbox1.HorzScrollBar.Position:=0;
if (originScrbarPos.Y+MovY)>0 then
Scrollbox1.VertScrollBar.Position:=originScrbarPos.Y+MovY
else
Scrollbox1.VertScrollBar.Position:=0;
end;
edt_x.Text:=inttostr(x);
edt_y.Text:=inttostr;
end;
procedure TForm1.Image1MouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
canDrag:=false;
screen.Cursor:=crDefault;
end;
procedure TForm1.Button3Click(Sender: TObject);
begin
showmessage(inttostr(scrollbox1.HorzScrollBar.Position)+':'
+inttostr(scrollbox1.VertScrollBar.Position));
end;
end.
其中一个背景image充满Scrollbox整个客户区,在ScrollBox中还有很多其他的小Image,和Tshape
现在要求在容器中拖动时,使所有的图片一起移动,相对位置不能变,并且能有象ACDSee中的拖动效果。
我实现了下,发现还是有点抖动。请大家指点。
unit Unit2;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, jpeg, ExtCtrls, StdCtrls;
type
TForm1 = class(TForm)
GroupBox1: TGroupBox;
ScrollBox1: TScrollBox;
Image1: TImage;
Button1: TButton;
Button2: TButton;
Edt_x: TEdit;
Edt_y: TEdit;
Shape2: TShape;
Shape3: TShape;
Button3: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
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);
procedure Button3Click(Sender: TObject);
private
originPos:Tpoint;
originScrbarPos:Tpoint;
canDrag:boolean;
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
begin
image1.Picture.LoadFromFile(ExtractFilePath(Application.ExeName)+'map.wmf');
end;
procedure TForm1.Image1MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
originPos.X:=x;
OriginPos.Y:=y;
originScrbarPos.X:=scrollbox1.HorzScrollBar.Position;
originScrBarPos.Y:=ScrollBox1.VertScrollBar.Position;
CanDrag:=true;
edt_x.Text:=inttostr(x);
edt_y.Text:=inttostr;
end;
procedure TForm1.Image1MouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
var
MovX,MovY:integer;
begin
if canDrag then
begin
screen.Cursor:=crHandPoint;
MovX:=originPos.x-x;
MovY:=originPos.y-y;
if (originScrbarPos.X+Movx)>0 then
Scrollbox1.HorzScrollBar.Position:=originScrbarPos.X+MovX
else
Scrollbox1.HorzScrollBar.Position:=0;
if (originScrbarPos.Y+MovY)>0 then
Scrollbox1.VertScrollBar.Position:=originScrbarPos.Y+MovY
else
Scrollbox1.VertScrollBar.Position:=0;
end;
edt_x.Text:=inttostr(x);
edt_y.Text:=inttostr;
end;
procedure TForm1.Image1MouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
canDrag:=false;
screen.Cursor:=crDefault;
end;
procedure TForm1.Button3Click(Sender: TObject);
begin
showmessage(inttostr(scrollbox1.HorzScrollBar.Position)+':'
+inttostr(scrollbox1.VertScrollBar.Position));
end;
end.