QAZY
asked on
Drag&Drop with Images
How can make a drag & drop operation between 2 objects (particulary between two listboxes) representing the objects dragged by an image and not using a cursor?
There is one nice solution for all such things: imagelists.
Put a TImage and a TImageList on a new form, then set the events of the image and the form on the same events:
procedure TForm1.FormMouseDown(Sende r: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
var
p: TPoint;
const
ImageIndex=0; HotSpotX=16; HotSpotY=16;
begin
GetCursorPos(p);
SetCaptureControl(Self);
Image1.Hide;
Image1.Update;
ImageList_BeginDrag(ImageL ist1.Handl e, ImageIndex, HotSpotX, HotSpotY);
p:=ClientToScreen(Point(x, y));
ImageList_DragEnter(0, p.x, p.y);
end;
procedure TForm1.FormMouseMove(Sende r: TObject; Shift: TShiftState; X,
Y: Integer);
var
p: TPoint;
begin
p:=ClientToScreen(Point(x, y));
ImageList_DragMove(p.x, p.y);
end;
procedure TForm1.FormMouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
ImageList_EndDrag;
ImageList_DragLeave(0);
SetCaptureControl(nil);
Image1.Show;
end;
The imagelist should contain the same image as the image component.
Regards, ptm.
Put a TImage and a TImageList on a new form, then set the events of the image and the form on the same events:
procedure TForm1.FormMouseDown(Sende
Shift: TShiftState; X, Y: Integer);
var
p: TPoint;
const
ImageIndex=0; HotSpotX=16; HotSpotY=16;
begin
GetCursorPos(p);
SetCaptureControl(Self);
Image1.Hide;
Image1.Update;
ImageList_BeginDrag(ImageL
p:=ClientToScreen(Point(x,
ImageList_DragEnter(0, p.x, p.y);
end;
procedure TForm1.FormMouseMove(Sende
Y: Integer);
var
p: TPoint;
begin
p:=ClientToScreen(Point(x,
ImageList_DragMove(p.x, p.y);
end;
procedure TForm1.FormMouseUp(Sender:
Shift: TShiftState; X, Y: Integer);
begin
ImageList_EndDrag;
ImageList_DragLeave(0);
SetCaptureControl(nil);
Image1.Show;
end;
The imagelist should contain the same image as the image component.
Regards, ptm.
good one ptmcomp :-)
To RBertora: Have you tried it?
Yes I did try it but unfortunately could not get it to work.. is that delphi 5 you are using????
Here is a versy simple code to achieve dragging of images:
var xx,yy : Integer;
procedure TForm1.Image1MouseDown(Sen der: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
xx := x;
yy := y;
end;
procedure TForm1.Image1MouseMove(Sen der: TObject; Shift: TShiftState; X,
Y: Integer);
begin
if ssLeft in shift then
begin
image1.Top := image1.Top +y -yy;
image1.Left := image1.Left +x -xx;
end;
end;
//credit to Kretszchmar
Rob ;-)
Here is a versy simple code to achieve dragging of images:
var xx,yy : Integer;
procedure TForm1.Image1MouseDown(Sen
Shift: TShiftState; X, Y: Integer);
begin
xx := x;
yy := y;
end;
procedure TForm1.Image1MouseMove(Sen
Y: Integer);
begin
if ssLeft in shift then
begin
image1.Top := image1.Top +y -yy;
image1.Left := image1.Left +x -xx;
end;
end;
//credit to Kretszchmar
Rob ;-)
To RBertora: No I used Delhpi2 and Delphi4 but I'm shure it works with Delphi5 if you assign all events correctly. But if you're using Winnt you have to write ImageList_DragEnter(Self.H andle, p.x, p.y); insted of ImageList_DragEnter(0, p.x, p.y); because the drawing on the desktop (Handle=0) appears behind all open windows.
Regards, ptm.
Regards, ptm.
ImageList_DragEnter where is the code for this written? or any of your underscore procedurecalls?
Rob ;-)
Rob ;-)
ImageList_DragEnter where is the code for this written? or any of your underscore procedurecalls?
Rob ;-)
Rob ;-)
You have to add 'uses CommCtrl, ImgList;' in your unit.
ptm. #:-o
ptm. #:-o
:-)
I should have guessed so...
Rob ;-)
I should have guessed so...
Rob ;-)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
FulgOre you should post as a comment.
Rob ;-)
Rob ;-)
ASKER
I gave you only Barely Acceptable because I have seen that RBertora and ptmcomp tried to help me by directly involving into the job.
Tks a lot to all of you!
QAZY
Tks a lot to all of you!
QAZY
just a quick idea:
The easiest way is to drop a Timage object on your form, set the visible property to false and set your image property to the pic you want.
Then in the appropriate events/ onstartdrag etc.. you make the image
visible at or near the mouse coordinates
you can use getcursorpos to get your mouse coordinates.. I think most components have onmousemove event, there
you set the top,left x,y coordinates of your image in the dragdroprelease event hide the image. (use all the other drag drop functionality you would normally use)
also you might want to set your cursor to invisible or none and then reset it to default after you have finished the drop...
you will probably have to use small delta values to adjust your top,left drag values like top+10,left-10 or something similar..
Oh if you don't have onmouse move events on all the components then drop a timer and set timer.enabled to true at start drag, and handle your image positioning in the timer event, use getcursorpos.
Cheers,
Rob ;-)