For the VCL library, Borland has implemented its own version of the Drag&Drop interface (translated as "drag"). This interface is internal - you can send and receive any Delphi controls inside the form "(except for the form itself). It is implemented without using the corresponding Windows API functions - they must be used when organizing communication with other tasks by dragging and dropping.

Clicking left button mouse over the control, we can "drag" it to any other element. From the programmer's point of view, this means that at the moments of dragging and releasing the key, certain events are generated that transmit all the necessary information - a pointer to the dragged object, current cursor coordinates, etc. The event receiver is the element on which the this moment the cursor is located. The handler for such an event must tell the system whether the given control accepts the "sending" or not. When the button is released over the receiver control, one or two more events are fired, depending on the receiver's readiness.

CancelDrag Cancels the current drag-and-drop or drag-and-dock operation.

Function FindDragTarget (const Pos: TPoint ;AllowDisabled: Boolean ): TControl ;

The function returns an object of the base class TControl , which refers to the screen position with the coordinates specified by the Pos parameter. This function is used to determine the potential recipient of a drag-and-drop or drag-and-dock operation. If no window control exists for the specified position, then the function returns nil . The AllowDisabled parameter determines whether disabled objects will be taken into account.

Function IsDragObject(Sender: TObject ): Boolean ;

The function determines whether the object specified in the Sender parameter is a descendant of the class TDragObject . This function can be used as the Source parameter in the OnDragOver and OnDockOver event handlers to determine whether the dragged object will be accepted. Also function IsDragObjectcan be used as the Source parameter in the OnDragDrop and OnDockDrop event handlers in order to correctly interpret the dragged object.

DragMode, DragCursor properties, BeginDrag, OnDragOver, OnDragDrop, OnEndDrag, OnStartDrag methods, Accept parameter

The process of dragging information from one object to another with the mouse is widely used in Widows. You can move files between folders, move folders themselves, and more.

All properties, methods, and events associated with the drag and drop process are defined in the TControl class, which is the parent of all Delphi visual components. Therefore, they are common to all components.

The beginning of the drag is determined by the DragMode property, which can be set at design time or programmatically equal to dmManual or dmAutomatic. The value of dmAutomatic (automatic) determines the automatic start of the dragging process when the user clicks the mouse button over the component. However, in this case, the OnMouseDown event associated with the user pressing the mouse button does not occur for this component at all.

The interface for transferring and receiving components appeared a long time ago. It provides interaction between two controls during application execution. In this case, any necessary operations can be performed. Despite the simplicity of implementation and the age of development, many programmers (especially beginners) consider this mechanism obscure and exotic. However, using Drag-and-Drop can be very useful and easy to implement. Now we will verify this.

In order for the mechanism to work, two controls must be configured accordingly. One must be the source (Source), the second - the receiver (Target). In this case, the source does not move anywhere, but is only registered as such in the mechanism.

Believe me, it's easy enough to transform X,Y coordinates, passed in the parameters of the OnDragOver and OnDragDrop events, into form coordinates.

Work with the Left and Top properties of the component that the cursor is hovering over. I will give a simple example. Place a Memo component on the form and assign Align property alTop value. Place a panel on the form, also set the Align property to alTop and set the Height property to a small value, say 6 or 7 pixels. Set DragMode to dmAutomatica and DragCursor to crVSplit. Place another Memo component and set Align to alClient. Select both Memos at the same time, the panel, and create a common OnDragOver event handler as shown below:

The Drag and Drop feature can help boost your iPad's performance. Here's how you can use it.

So you can move a file from one cloud storage service to another, copy text from Safari into a text editing app to add a quote or create backup certain photos in the file storage application.

How to drag and drop photos, files and text

1. Touch and hold the photo, file, or highlighted text you want to drag to another app.

2. Drag the item to Right place in this application or another that you have open in Slide Over or Split View and release.

How to drag and drop multiple photos or files at once

1. Touch and hold one of the photos or files you want to drag.

2. While dragging the current item, tap another photo or file that you also want to drag. Do it again with as many elements as you want to move.

3. Drag all selected objects to the designated place in another application that you have open in Slide Over or Split View and release them.

How to drag text from one application to another

1. Tap and hold on the part of the text you want to drag to select it.

2. Use selection points to highlight the rest of the text you want to drag.

3. Press and hold the selected text.

4. Drag the text to the application where you want to place it and release it.

How to change the position of icons of several applications at once using "Drag and Drop"

While most iOS drag and drop functionality only works on the iPad, this trick actually works on both the iPhone and iPad. This allows you to organize the location of applications on the home screen using « Drag and Drop" instead of moving them one by one.

1. Touch and hold the icon for the app you want to reposition on the home screen.

2. Touch additional applications, which also needs to be moved.

3. Drag these apps to the page or folder where you want them to be and drop them.

For a long time now, there have been JavaScript functions that allow us to create drag & drop interfaces. But none of these implementations are native to the browser. HTML5 has its own way of creating drag & drop interfaces (with a little help from JavaScript). In this article, we will tell you how this can be achieved...

Browser support

HTML5 drag & drop is currently supported by all major desktop browsers (including IE (even IE 5.5 has partial support)), but is not supported by any of the popular mobile browsers.

Drag&Drop events

At each stage of the drag & drop, various events are fired to let the browser know which JavaScript code to execute. List of events:

  • dragStart: fired when the user starts dragging elements;
  • dragEnter: fired when the dragged element is first dragged over the target element;
  • dragOver: fired when the mouse moves over an element while a drag is in progress;
  • dragLeave: fired if the user's cursor leaves the element while dragging;
  • drag: fired every time we move the mouse while dragging our element;
  • drop: fired when the actual drop is executed;
  • dragEnd: fired when the user releases the mouse button while dragging an object.

With all these event listeners, you have a lot of control over how the interface will work.

dataTransfer object

This is where all the drag&drop magic happens. This object contains the data that was submitted by the drag operation. Data can be set and retrieved different ways, the most important of which are:

  • dataTransfer.effectAllowed=value: returns the allowed action types, possible values: none, copy, copyLink, copyMove, link, linkMove, move, all and uninitialized.
  • dataTransfer.setData(format, data): Adds specific data and format.
  • dataTransfer.clearData(format): Clears all data for a specific format.
  • dataTransfer.setDragImage(element, x, y): sets the image you want to drag, the x and y values ​​indicate where the mouse cursor should be (0, 0 will position it top left).
  • data = dataTransfer.getData(format) : As the name suggests, it returns data for a particular format.

Creating a drag&drop example

Now we will start creating a simple drag&drop example. As you can see we have two small divs and one big one, we can drag the small divs inside the big one and even move them back.

Dragging an object

The first thing we need to do is create the HTML. We make divs draggable with the draggable attribute:

Once that's done, we need to define a JavaScript function that will fire as soon as we start moving this element:

Function dragStart(ev) ( ev.dataTransfer.effectAllowed="move"; ev.dataTransfer.setData("Text", ev.target.getAttribute("id")); ev.dataTransfer.setDragImage(ev.target,100,100) ; return true; )

In this code, we first declare what type of effect we are allowing in the operation and set it to move. On the second line, we set the data to work with, where the text will be Text and the value will be the ID of the element we are dragging. After that, we use the setDragImage method, which will set what we will drag, and then where the cursor will be during the drag, and since the cubes are 200 by 200 pixels, we placed it in the very center. At the end, we return return true.

Drop object

In order for an element to accept a drop, it must listen for 3 different events: dragEnter, dragOver, and the drop event. So let's add this to our HTML5 div with ID big:

function dragEnter(ev) ( ev.preventDefault(); return true; ) function dragOver(ev) ( ev.preventDefault(); )

In the first function, we define what should happen when the element we are dragging reaches the desired element where the drop should occur, in this case we are only preventing the default behavior of the browser. Next, in the dragOver function, we just don't allow drop to happen by default.

In the next part, we define a function for when an element is "thrown" on the desired target:

Function dragDrop(ev) ( var data = ev.dataTransfer.getData("Text"); ev.target.appendChild(document.getElementById(data)); ev.stopPropagation(); return false; )

In this last part, we first set the data variable where we get all the data that is available to text format, and then we add data to the div where we want to drop the element.

Make the drop section a target

The demo shows that two divs can be moved back into place. Luckily, adding another drop target is a lot easier than you might think. Because we already have these functions and all we need to do is add event listeners:

And that's all it takes to allow the div to be dragged back into place.

There are many drag&drop applications that are built with using JavaScript libraries, and are often easier to use. But we hope that in this HTML5 and JavaScript technique, you will see the future potential for solving your problems.

HTML Drag and Drop interfaces enable applications to use drag-and-drop features in browsers. The user may select draggable elements with a mouse, drag those elements to a droppable element, and drop them by releasing the mouse button. A translucent representation of the draggable elements follows the pointer during the drag operation.

For web sites, extensions, and XUL applications, you can customize which elements can become draggable, the type of feedback the draggable elements produce, and the droppable elements.

This overview of HTML Drag and Drop includes a description of the interfaces, basic steps to add drag-and-drop support to an application, and an interoperability summary of the interfaces.

Drag Events

event On Event Handler Fires when…
drag ondrag …a dragged item(element or text selection) is dragged.
dragend ondragend …a drag operation ends (such as releasing a mouse button or hitting the Esc key; see Finishing a Drag .)
dragenter ondragenter …a dragged item enters a valid drop target. (See Specifying Drop Targets .)
dragexit ondragexit …an element is no longer the drag operation's immediate selection target.
dragleave ondragleave …a dragged item leaves a valid drop target.
dragover ondragover …a dragged item is being dragged over a valid drop target, every few hundred milliseconds.
dragstart ondragstart …the user starts dragging an item. (See Starting a Drag Operation .)
drop drop …an item is dropped on a valid drop target. (See Performing a Drop.)

Note: Neither dragstart nor dragend events are fired when dragging a file into the browser from the OS.

Interfaces

The basics

This section is a summary of the basic steps to add drag-and-drop functionality to an application.

identify what is draggable

Making an element draggable requires adding the draggable attribute and the ondragstart global event handler, as shown in the following code sample:

This element is draggable.

For more information, see:

handle the drop effect

The handler for the drop event is free to process the drag data in an application-specific way.

Typically, an application uses the getData() method to retrieve drag items and then process them accordingly. Additionally, application semantics may differ depending on the value of the

Drag and drop techniques have evolved over many years. Not surprisingly, with the increase in the number of programmers developing plug-ins with open source code(e.g. for jQuery) the old methods are resurrected. The JavaScript library is highly adaptive and offers many improvements in this age of web technology.

In this tutorial, we'll make a script that we can use to create drag and drop dynamic rectangles on our website. The process is controlled by jQuery. Such scripts save time by providing ready-made functionality! And the drag and drop library can be used in other projects.

We prepare content

First of all, we will prepare a small website for the project. In the project folder, you need to create two directories with noteworthy names "js" and "css" and an empty file index.html . The code will be very simple so that there is a clear idea of ​​\u200b\u200bwork, and there is a point for further development.

Below is the code of our HTML file. In chapter head we include 3 scripts. The main jQuery script will be loaded from the Google Code server. our style.css file is also included, which contains the main properties for forming appearance our document.

drag me

Yes Yes. Exactly me.

I can be dragged too

( zIndex: 200, opacity: .9 )

P.S. You can drop me anywhere!

Within a section body only two blocks div, which contain both rectangles. The code is quite simple and understandable. Headers with classes are placed inside each rectangle. handler and handler2. This is important because each rectangle behaves differently when dragging.


Installing the CSS

The HTML code is very simple. If you understand the basic markup, then css styles also won't be difficult. It mainly defines margins, padding and colors.

Body,html ( font-family:Calibri, sans-serif; background:#eaf3fb; font-size:12px; height:1000px; line-height:18px; ) p ( height:30px; )

Selectors body,html used only for the demo page. And all the content is placed in two draggable rectangles.

Dv1 ( width:200px; background-color:#eff7ff; border:1px solid #96c2f1; position:absolute; left:100px; top:100px; ) .dv1 h2 ( background-color:#b2d3f5; padding:5px; font- family:Georgia, "Times New Roman", Times, serif; font-size:1.0em; text-transform:uppercase; font-weight:bold; color:#3a424a; margin:1px; cursor:move; ) .dv1 div ( padding:5px; margin-bottom:10px; ) .dv2 ( background-color:#f6ebfb; border:1px solid #a36fde; width:550px; position:absolute; cursor:move; left:400px; top:230px; ) .dv2 h2 ( background-color:#eacfe9; letter-spacing:-0.09em; font-size:1.8em; font-weight: bold; padding:15px; margin:1px; color:#241f24; cursor:move; ) .dv2 .content2 ( padding:5px; margin-bottom:10px; )

For both .dv1 and .dv2 classes we use absolute positioning. This is not necessary and probably not the most The best way for positioning draggable rectangles. However, for our example, this positioning makes sense, since each time the page is refreshed, the rectangles are set to certain places.

Also fonts and colors are different for rectangles to make it easier to see the difference.

Otherwise, the headings and content of the blocks are almost identical. If you will be copying styles into your project, change the names before running. In some cases it will make more sense to use IDs instead of classes, such as when using a drag and drop technique for one specific block.

Parsing JavaScript

The two JavaScript files contain all the code needed to make it work. We'll skip the details of working with jQuery, as it's out of the scope of this tutorial. Let's pay attention to the jquery.dragndrop.js file.

Line 22 is the definition of the function Drags.

$.fn.Drags = function(opts) ( var ps = $.extend(( zIndex: 20, opacity: .7, handler: null, onMove: function() ( ), onDrop: function() ( ) ), opts );

This sets the return variable and initialization data for Drags. This method is very widely used when working with jQuery to pass options to other functions. Inside, we set variables for all available options for the draggable rectangles.


The following piece of code includes event handlers for the variable drag drop. Both events drag and drop call functions with passing event parameters to them. These events occur when you press the mouse button to drag an object and then release it.

Var dragndrop = ( drag: function(e) ( var dragData = e.data.dragData; dragData.target.css(( left: dragData.left + e.pageX - dragData.offLeft, top: dragData.top + e.pageY - dragData.offTop )); dragData.handler.css(( cursor: "move" )); dragData.target.css (( cursor: "move" )); dragData.onMove(e); ), drop: function( e) ( var dragData = e.data.dragData; dragData.target.css(dragData.oldCss); //.css(( "opacity": "" )); dragData.handler.css("cursor", dragData. oldCss.cursor); dragData.onDrop(e); $().unbind("mousemove", dragndrop.drag) .unbind("mouseup", dragndrop.drop); ) )

Our functions manipulate the CSS positioning of each object. If you change the absolute positioning of your objects, then this will not affect the code, since each JavaScript function changes any style that is defined for the object.

In the rest of the code, the handler is checked and other styles are cosmetically changed. Here you can add a change in transparency, font and color, or add new paragraphs.

Drag/Drop Functions

The second fn.js file contains very simple code. We wait for the document to fully load, after which we call our functions. Two function instances are defined Drags, which was dealt with earlier.

We have two floating blocks with classes .dv1 and .dv2 . If you need to leave one floating block, then you just need to remove the second part of the code. Adding another floating block is also easy. You only need to add new feature in this file.

The first step is to set the options when calling the function. Be sure to set the name of the handler. With it, we tell jQuery which handler to use when the mouse button is pressed in certain area document. The handler name can be a class or an ID attribute.

Our first function has two event handlers onMove and onDrop. Both call new functions that are passed to the current event as variables. This is where the HTML code in the rectangle is manipulated to update on every move. This is a great effect for demonstrating how you can control the process with simple jQuery events.

In the second function, we use the z-Index and opacity parameters. You can add others css properties? but this would require rewriting the JavaScript code to validate the installs. For example, you can pass a different font style or values ​​for the height and width of the floating rectangle - a very interesting trick!

Conclusion

As a result of a little work, we have at our disposal a wonderful interface with a drag and drop function. jQuery provides tremendous benefits for developers who are eager to use the old ways in their projects.

As a result, we not only got event handler functions, but we can also pass new variables to draggable blocks. This opens up new possibilities for creativity. The demo for the lesson contains only an outline of what can be done with the help of such code.

So check out the jQuery documentation for using library functions.