Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

TImage Drag Drop 1

Status
Not open for further replies.

callwright

Programmer
Jan 8, 2003
2
US
I am trying to drag and drop a TImage on a form. Problem is that the TImage does not move from it's original location until the mouse button is released. What I need is for the TImage to remain visible while following the mouse movement. This is a somewhat dorky question, but I've found that information on this particular topic is almost impossible to find on the Internet or in books.

Here's what I've got ...

A project with one form containing a single TImage:

unit Unit1;

interface

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

type
TForm1 = class(TForm)
Image1: TImage;
procedure FormDragOver(Sender, Source: TObject; X, Y: Integer;
State: TDragState; var Accept: Boolean);
procedure FormMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure FormDragDrop(Sender, Source: TObject; X, Y: Integer);
procedure FormCreate(Sender: TObject);
procedure Image1MouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure Image1MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
downY: integer;
mouseIsDown: boolean;

implementation

{$R *.dfm}

procedure TForm1.FormDragOver(Sender, Source: TObject; X, Y: Integer;
State: TDragState; var Accept: Boolean);
begin
if mouseIsDown then
begin
Image1.Top := Y;
Image1.Repaint;
end;

Accept := (Source is TImage);
end;

procedure TForm1.FormMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
if Button = mbLeft then
Image1.BeginDrag(true);
end;

procedure TForm1.FormDragDrop(Sender, Source: TObject; X, Y: Integer);
begin
TImage(Source).Top := Y;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
mouseIsDown := false;
end;

procedure TForm1.Image1MouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
mouseIsDown := false;
end;

procedure TForm1.Image1MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
mouseIsDown := true;
end;

end.

 
Try investigating the MouseMove event in TImage. It gives you the current X, Y position of the mouse and you could then adjust the Left and Top positions of your Image.

Andrew
 
These should give you some clues. The Timage can be dragged when the left mouse is down, release the button to stop.

xdiff and ydiff are globals their only purpose is to keep the cursor in the same place on the image as it drags.


procedure TCamera.MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
if (ssleft in shift) then
begin
Xdiff := X;
Ydiff := y;
end;
end;

procedure TCamera.MouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
begin
if (ssleft in shift) then
begin
CameraImage.Top := CameraImage.Top + Y - Xdiff;
CameraImage.Left := CameraImage.Left + X - YDiff;
end;
end;

Steve
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top