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?
QAZYAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

RBertoraCommented:


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 ;-)


0
ptmcompCommented:
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(Sender: 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(ImageList1.Handle, ImageIndex, HotSpotX, HotSpotY);
  p:=ClientToScreen(Point(x,y));
  ImageList_DragEnter(0, p.x, p.y);
end;

procedure TForm1.FormMouseMove(Sender: 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.
0
RBertoraCommented:
good one ptmcomp :-)
0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

ptmcompCommented:
To RBertora: Have you tried it?
0
RBertoraCommented:
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(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  xx := x;
  yy := y;
end;

procedure TForm1.Image1MouseMove(Sender: 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 ;-)
0
ptmcompCommented:
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.Handle, 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.
0
RBertoraCommented:
ImageList_DragEnter where is the code for this written? or any of your underscore procedurecalls?

Rob ;-)
0
RBertoraCommented:
ImageList_DragEnter where is the code for this written? or any of your underscore procedurecalls?

Rob ;-)
0
ptmcompCommented:
You have to add 'uses CommCtrl, ImgList;' in your unit.
ptm. #:-o
0
RBertoraCommented:
:-)

I should have guessed so...

Rob ;-)
0
fulg0reCommented:
hi there.

i guess you can save yourself tons of time if you d/l this fine drag'n'drop suite from www.melander.dk

it has ALL sorts of dragging you could think off... full source incl. ofcourse. enjoy :)
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
RBertoraCommented:
FulgOre you should post as a comment.
Rob ;-)
0
QAZYAuthor Commented:
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
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.