Wednesday, March 11, 2020

Understanding Drag and Drop Operations in Delphi

Understanding Drag and Drop Operations in Delphi To drag and drop is to hold down the computer 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. Well 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 forms OnDragDrop event handler. 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 forms 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). 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). DragCursor 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 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 youve left the value of TImages 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, well use the CtrlMouseDown keyboard combination to allow dragging to take place. Set TImages DragMode back to dmManual and write the MouseDown event handler like this: BeginDrag takes a Boolean parameter. If we pass True (like in this code), dragging begins immediately; if False, it doesnt start until we move the mouse a short distance. Remember that it requires the Ctrl key.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.