Understanding Drag and Drop Operations

Including Source Code Examples

Format
mla apa chicago
Your Citation
Gajic, Zarko. "Understanding Drag and Drop Operations." ThoughtCo, Jul. 24, 2017, thoughtco.com/understanding-drag-and-drop-operations-1058386. Gajic, Zarko. (2017, July 24). Understanding Drag and Drop Operations. Retrieved from https://www.thoughtco.com/understanding-drag-and-drop-operations-1058386 Gajic, Zarko. "Understanding Drag and Drop Operations." ThoughtCo. https://www.thoughtco.com/understanding-drag-and-drop-operations-1058386 (accessed September 25, 2017).
Rearrange Multiple Items in ListView
Rearrange Multiple Items in ListView.

To "drag and drop" is to hold down the mouse button as the mouse is moved, and then release the button to drop the object. Delphi makes it easy to program dragging and dropping into applications.

You can really drag and drop from/to wherever you like, like from one form to another, or from Windows Explorer to your application.

Dragging and Dropping Example

Start up a new project and put one image control on a form.

Use Object Inspector to load a picture (Picture property) and then 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 the DragMode property to control when the user is able to drag the control.

The default value this property is dmManual, which means that dragging components around the application is not allowed, except under special circumstances, for which we have to write the appropriate code.

Regardless of the setting for the DragMode property, the component will move only if the correct code is written 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 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, which in this example is TImage.

Accept

We have to use the form's OnDragOver event to signal that the form can accept the TImage control we want to drop on it. Although the 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 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 use an animated cursors or whatever you like, like a BMP image file or a CUR cursor file.

BeginDrag

If DragMode is dmAutomatic, dragging begins automatically when we press a mouse button with the cursor on the control.

If you've left the value of TImage's DragMode property at its default of 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 the Ctrl+MouseDown keyboard combination to allow dragging to take place. Set TImage's DragMode back to dmManual and write the 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 False, it doesn't start until we move the mouse a short distance.

Remember that it requires the Ctrl key.

Drag and Drop Examples

Here are some additional Delphi dragging and dropping examples:

Format
mla apa chicago
Your Citation
Gajic, Zarko. "Understanding Drag and Drop Operations." ThoughtCo, Jul. 24, 2017, thoughtco.com/understanding-drag-and-drop-operations-1058386. Gajic, Zarko. (2017, July 24). Understanding Drag and Drop Operations. Retrieved from https://www.thoughtco.com/understanding-drag-and-drop-operations-1058386 Gajic, Zarko. "Understanding Drag and Drop Operations." ThoughtCo. https://www.thoughtco.com/understanding-drag-and-drop-operations-1058386 (accessed September 25, 2017).