Nineth Chapter-delphi Drag-and-drop programming (1)

Source: Internet
Author: User
Tags include integer

Drag-and-drop (DragDrop) is a quick way to operate with Windows. As a windows-based development tool, Delphi also supports drag-and-drop operations, and the development of application system drag-and-drop function is very convenient, truly embodies the powerful function of Delphi and convenience.

All controls provided by Delphi (Control, which is the part that gets the input focus) support drag-and-drop operations with corresponding drag-and-drop properties, drag-and-drop events, and drag-and-drop methods. Let's first introduce the control's drag-and-drop support, and then give a general step and application example to develop the drag-and-drop operation.

9.1 Control Drag-and-drop support

In a drag-and-drop operation, a control can be divided into two categories: source control and Target control. Most controls can be either a source control or a target control. But there are also a few controls that support only one of these.

9.1.1 drag-and-drop properties

There are two main drag-and-drop properties:

DragMode: Drag mode

Dragcursor: Dragging the cursor

They are all set in a drag-and-drop source control. DragMode controls how the control reacts when the mouse is pressed over the control during run time. If the DragMode is set to Dmautomatic, drag starts automatically when the user presses the mouse on the control, and if DragMode is dmmanual (which is the default), the mouse event is used to determine whether a drag can start.

Dragcursor is used to select the cursor to display when dragging, the default value is Crdrag, and generally do not modify it. The common interface specification should be respected by developers in the process of programming. But sometimes for specific purposes, developers can also assign their own design of the cursor to dragcursor.

9.1.2 drag-and-drop events

There are three main drag-and-drop events:

OnDragOver: Fires when dragging through

Ondragdrop: Fires when dragging down

Onenddrop: Fired at end of drag

The first two events are responded by the target control, and the latter event is responded by the source control.

The primary function of the OnDragOver event is to determine whether the control is acceptable when the user drops the drag in place. Its parameters include the following:

Source:tobject; {Source Control}

X,y:integer; {Cursor Position}

state:tdragstate {Drag state}

var Accept:boolean {can accept}

Tdragstate is an enumeration type that represents the relationship between a drag-and-drop project and a target control.

Type

Tdragstate = (Dsdragenter, dsdragleave, Dsdragmove);

The meanings of different values are as follows:

Value and significance of Table 9.1 dragstate

━━━━━━━━━━━━━━━━━━━━━━━━━━━

Value-taking significance

───────────────────────────

Dsdragenter Drag object into a allow drag object to drop

In the control. As the default state.

Dsdragleave Drag object to leave a allow drag object to drop

The control.

Dsdragmove drag the object in an object that is allowed to be dragged down

Controls to move inside the control.

━━━━━━━━━━━━━━━━━━━━━━━━━━━

The user can use the supplied parameters to determine whether the dropped drag can be accepted, such as:

To determine the source control type:

Accept: = Source is Tlabel;

To determine the source control object:

Accept: = (Source = TabSet1);

To determine the cursor position:

See the routines in (9.2), (9.3).

To determine the drag state:

If (Source is Tlabel) and (state = Dsdragmove) Then

Begin

Source. DragIcon: = ' New.ico ';

Accept: = True;

End

Else

Accept: = False;

When Accept=true, the target control can respond to the Ondragdrop event to determine how the program handles when the drag is dropped.

Parameters for the Ondragdrop event processing include source control and cursor position. This information can be used to determine how the processing is handled.

The Onenddrag event is responded to by the source control at the end of the drag operation and is used for the source control to handle it accordingly. The drag operation ends with the drag drop being accepted, including the user releasing the mouse on a control that cannot accept the drop. The parameters of the event handling process include the target control (targets) and the coordinates of the drop position. If Target=nil, the drag item is not accepted by any control.

In section 3rd, the File list box should update the display if the operation is successful in the drag-and-drop move, drag-and-drop copy operation that is described in. The following procedure is used to implement this functionality.

Procedure Tfmform.filelistenddrag (Sender, Target:tobject; X, Y:integer);

Begin

If Target <> Nil then filelist.update;

End

In addition to the three events described above, an event onmousedown is also commonly used for the response of a drag-and-drop operation. OnMouseDown is not a special drag-and-drop event, but the start of dragging in manual mode is achieved during the processing of this event.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.