C ++ builder implements drag-and-drop controls

Source: Internet
Author: User

5.1 drag and drop controls
Drag-and-drop is a common method for manipulating objects. You can drag the entire control, or drag an item in a control (such as a list box or Tree View) to another control.
· Start the drag operation.
· Accept the items to be dragged.
· Put down items.
· Terminate the drag operation.
· Customize drag-and-drop operations by dragging objects.
· Change the drag pointer.

5.1.1 start dragging
Each control has the dragmode attribute to determine how to initialize it. If the dragmode value is dmautomatic, drag the cursor when you place the cursor on the control and press the mouse key.
Start automatically. Because dmautomatic may affect the common mouse activity, you can set dragmode to dmmanual (default) and start by pressing the event with the mouse.
Drag operation.

To manually start the drag operation, you can call this control.
Begindrag method. Begindrag requires a Boolean parameter immediate. If the value is true, the drag operation starts immediately. If the value is false, the drag operation starts
The user will not start before dragging the mouse for a short distance. The begindrag (false) Call allows the control to accept mouse clicks without starting the drag operation.

You can start to drag controls when setting other statuses. For example, you can check the parameters of begindrag before calling the begindrag method to determine which mouse key the user presses. For exampleCodeYou can initialize the drag operation to process the mouse pressing event in the file list box only when the left mouse button is pressed.

5.1.2 accept drag items
When you drag an item over a control, the control receives the ondragover event, which must indicate whether the control can accept the drag item if you place the drag item here. Drag the cursor
Indicates whether the control can accept the drag. To accept the drag items above the control, write events for the ondragover event of the control.Program.
The drag-over event has an accept parameter. To accept a drag, you can set it to true in the event handler. If accept is true, the application sends
Drag-drop events. The drag-over event also has other parameters, including the source of the drag item and the current position of the mouse cursor. These parameters can also be used by the event handler to determine whether to accept the put-down operation.
. In the following example, a directory tree view only accepts items when they are dragged from the file list box.

5.1.3 put down items
If a control indicates that it can accept the drag item, it should also process the item to be put down. To process put-down items, you can write an event handler for the ondragdrop event of the control that accepts the put-down operation. And
The drag-over event is similar. The drag-drop event indicates the source of the drag item and the coordinates of the mouse cursor above the accept control. The latter allows the path to which the monitoring item is dragged. For example, you can use this message.
To change the color of components with other controls.

In the following example, the directory tree view accepts the drag items from the file list box, corresponding to moving the file to a directory and putting down the file.

5.1.4 terminate the drag operation
When the drag item is successfully put down or released above the control that cannot accept it, the drag operation ends. The End-drag event is sent to the source control of the drag item. To make the source control respond to the drag end, write an event handler for the onenddrag event of the control.
The most important parameter in the onenddrag event is the target, which specifies which control (if any) has been put down. If
If the target is null, no control accepts the drag. The onenddrag event also contains the coordinates of the accepted controls. In the following example, the file list box refreshes its file list to process the end-drag event.

5.1.5 custom drag-and-drop operations by dragging objects

Yes
Use the derived class of tdragobject to customize the drag-and-drop operation of the object. The standard drag-over and drag-drop events indicate the drag item's source and the cursor sitting over the control.
. To obtain the attached state information, you can derive a custom drag object from tdragobject and overwrite its virtual method. You can create this custom object in the onstartdrag event.
In general, the source parameters in the drag-over and drag-drop events are the controls starting with the drag operation. If different types of controls need to start dragging the same data, the source needs to support various controls
. When a derived object of tdragobject is used, the source is the drag object itself. If each control creates a drag object of the same type in its onstartdrag event, the target only needs
Process a type of object.

The drag-over and drag-drop events can call the isdragobject function to determine whether to drag an object or a control. You can drag an object between the form implemented by the main EXE file of the application and the form implemented by the DLL, or between the forms implemented by different DLL.

5.1.6 change the drag pointer

You can customize the appearance of the mouse pointer during the drag operation by setting the dragcursor attribute of the source component.

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.