1. Technology
Send to a Friend via Email

Understanding Drag and Drop Operations

With Source Code Examples...

By

Rearrange Multiple Items in ListView

Rearrange Multiple Items in ListView

Moving an object with the mouse button pressed is usually called dragging, and what happens when we end dragging by releasing the mouse button is called dropping.
Delphi makes it easy to program dragging & dropping into our applications. We can even drag and drop from one form to another or from Windows Explorer to our application, or from-to what ever we want, as you will see.

Dragging and Dropping Example

Start up a new project and put one image control on a form. Use Object Inspector to load some picture (Picture property). Set the DragMode property to dmManual.
We'll create a program that will allow moving a TImage control runtime using the drag and drop technique.

DragMode
Components permit two types of dragging: automatic and manual. Delphi uses DragMode property to control when the user is able to drag the control. The default value of a DragMode property is dmManual which means that dragging components around our application is not allowed, except under special circumstances, for which we have to write some appropriate code. Regardless of the setting for the DragMode property, the component will move only if we write the code to reposition it.

OnDragDrop

The event that recognizes dragging and dropping is called the OnDragDrop event, we use it to specify what we want to happen when the user drops an object. Therefore, if we want to move a component (image) to a new location on a form, we have to write some code for the form's OnDragDrop event handler.
 procedure TForm1.FormDragDrop(Sender, Source: TObject; X, Y: Integer) ;
 begin
    if Source is TImage then
    begin
      TImage(Source).Left := X;
      TImage(Source).Top := Y;
    end;
 end; 
The Source parameter of the OnDragDrop event is the object being dropped. The type of the source parameter is TObject, to access its properties we have to cast it to the correct component type. In this example, it is TImage.

Accept

We have to use form's OnDragOver event to signal that the form can accept the TImage control we want to drop on it. Although Accept parameter defaults to True, if an OnDragOver event handler is not supplied, the control rejects the dragged object, as if the Accept parameter was changed to False.
 procedure TForm1.FormDragOver(Sender, Source: TObject; X, Y: Integer; State: TDragState; var Accept: Boolean) ;
 begin
    Accept := (Source is TImage) ;
 end; 
Run your project, and try dragging and dropping your image. Notice that the image remains visible in its original location while the drag mouse pointer moves. We cannot use the OnDragDrop procedure to make the component invisible while the dragging takes place. This is because this procedure is called only after the user drops the object (if at all).

Drag Cursor
If you want to change the cursor image presented when the control is being dragged, use the DragCursor property. The possible values for the DragCursor property are the same as those for the Cursor property. You can even use an animated cursors or whatever you like (a BMP as a CUR for example).

BeginDrag

If DragMode is dmAutomatic, dragging begins automatically when we press a mouse button with the cursor on the control. If you have left the value of TImage's DragMode property at its default dmManual, you have to use BeginDrag/EndDrag methods to allow dragging of the component. A more common way to drag and drop is to set DragMode to dmManual and start the dragging by handling mouse-down events.

Now, we'll use Ctrl+MouseDown combination to allow dragging to take place. Set TImage's DragMode back to dmManual and write MouseDown event handler like this:

 procedure TForm1.Image1MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer) ;
 begin
    if ssCtrl in Shift then Image1.BeginDrag(True) ;
 end; 
BeginDrag takes a Boolean parameter. If we pass True (like in this code), dragging begins immediately, if we pass False, dragging doesn't begin until we move the mouse a short distance. Dont forget to hold down the Ctrl key.

Drag and Drop Examples

More Delphi dragging and dropping examples:

©2014 About.com. All rights reserved.