[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 210
  • Last Modified:

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?
0
QAZY
Asked:
QAZY
1 Solution
 
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
Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

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

Featured Post

Take Control of Web Hosting For Your Clients

As a web developer or IT admin, successfully managing multiple client accounts can be challenging. In this webinar we will look at the tools provided by Media Temple and Plesk to make managing your clients’ hosting easier.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now