The definition in the class library is:
Copy Code code as follows:
[Uipermissionattribute (SecurityAction.Demand, Clipboard = Uipermissionclipboard.ownclipboard)]
Public DragDropEffects DoDragDrop (
Object data,
DragDropEffects allowedeffects
)
Where the data parameter is to be dragged and dropped, if the drag operation needs to interoperate with the application of another process, data represents the base managed class (String,bitmap, or metafile), or the implementation of the ISerializable or a IDataObject object. The allowedeffects parameter represents the effect of drag-and-drop, an enumeration value (DragDropEffects). The return value is also the DragDropEffects enumeration value.
When you start to call the DoDragDrop method to drag a data object, Dodragdrops detects whether the control under the current cursor position is a valid drop target during the drag-and-drop process. If the control under the current cursor is a valid drop target, the GiveFeedback event is raised with the specified drag-and-drop effect. When detecting whether the current position cursor is a valid drop target, the Dodragdrops method tracks changes to the cursor position, keyboard state, and mouse state.
(1) If used to remove a window, the DragLeave event is raised.
(2) If another control is moved, the DragEnter event for the control is raised.
(3) If the mouse moves, but stays in a control, the DragOver event is raised.
If a change to the keyboard or mouse state is detected, the QueryContinueDrag event for the drag-and-drop source is raised, and the QueryContinueDragEventArgs Action property value of the event determines whether to continue dragging, place data, or cancel the operation.
(1) If the Action property is specified as continue, the DragOver event is raised.
(2) If the Action property is specified as drop, the drop effect is returned to the source so that the application can manipulate the data appropriately; For example, if it is a move, cut the data.
(3) If the value of Dragaction is cancel, the DragLeave event is raised
Excerpt a sample code from the CSDN:
The following code example shows a drag-and-drop operation between two ListBox controls. When the drag action starts, the example calls the DoDragDrop method. During the MouseDown event, the mouse movement from the mouse position is larger than the SystemInformation..::. Dragsize, the drag action is started. The Indexfrompoint method is used to determine the index of the item to drag during the MouseDown event.
The example also shows how to use a custom cursor for a drag-and-drop operation. The example requires that there are two cursor files in the application directory: 3dwarro.cur and 3dwno.cur, respectively, for the custom drag cursor and no docking of cursors. If Usecustomcursorscheckcheckbox is selected, a custom cursor is used. The custom cursor is set in the GiveFeedback event handler.
The keyboard state is computed in the DragOver event handler of the right ListBox to determine which drag operation will occur based on the state of the shift, Ctrl, ALT, or Ctrl+alt key. Where the placement action takes place in the ListBox is also determined during the DragOver event. If the data to be placed is not a String, the Drageventargs.seffect is set to None in DragDropEffects. Finally, the docking status is displayed in the Droplocationlabellabel.
The data to be placed for the right ListBox is determined in the DragDrop event handler, and the String value is added to the appropriate location in the ListBox. If the drag operation moves outside the form border, the drag-and-drop operation is canceled in the QueryContinueDrag event handler
Copy Code code as follows:
Using System;
Using System.Drawing;
Using System.Windows.Forms;
Namespace Snip_dragndrop
{
public class Form1:System.Windows.Forms.Form
{
Private System.Windows.Forms.ListBox Listdragsource;
Private System.Windows.Forms.ListBox Listdragtarget;
Private System.Windows.Forms.CheckBox Usecustomcursorscheck;
Private System.Windows.Forms.Label Droplocationlabel;
private int Indexofitemundermousetodrag;
private int indexofitemundermousetodrop;
Private Rectangle Dragboxfrommousedown;
Private point Screenoffset;
Private Cursor mynodropcursor;
Private Cursor mynormalcursor;
The main entry point is for the application.
[STAThread]
static void Main ()
{
Application.Run (New Form1 ());
}
Public Form1 ()
{
This. Listdragsource = new System.Windows.Forms.ListBox ();
This. Listdragtarget = new System.Windows.Forms.ListBox ();
This. Usecustomcursorscheck = new System.Windows.Forms.CheckBox ();
This. Droplocationlabel = new System.Windows.Forms.Label ();
This. SuspendLayout ();
Listdragsource
This. ListDragSource.Items.AddRange (new object[] {"One", "two", "three", "four",
"Five", "six", "seven", "eight",
"Nine", "Ten"});
This. Listdragsource.location = new System.Drawing.Point (10, 17);
This. Listdragsource.size = new System.Drawing.Size (120, 225);
This. Listdragsource.mousedown + = new System.Windows.Forms.MouseEventHandler (this. Listdragsource_mousedown);
This. Listdragsource.querycontinuedrag + = new System.Windows.Forms.QueryContinueDragEventHandler (this. Listdragsource_querycontinuedrag);
This. Listdragsource.mouseup + = new System.Windows.Forms.MouseEventHandler (this. Listdragsource_mouseup);
This. Listdragsource.mousemove + = new System.Windows.Forms.MouseEventHandler (this. Listdragsource_mousemove);
This. Listdragsource.givefeedback + = new System.Windows.Forms.GiveFeedbackEventHandler (this. Listdragsource_givefeedback);
Listdragtarget
This. Listdragtarget.allowdrop = true;
This. Listdragtarget.location = new System.Drawing.Point (154, 17);
This. Listdragtarget.size = new System.Drawing.Size (120, 225);
This. Listdragtarget.dragover + = new System.Windows.Forms.DragEventHandler (this. Listdragtarget_dragover);
This. Listdragtarget.dragdrop + = new System.Windows.Forms.DragEventHandler (this. Listdragtarget_dragdrop);
This. Listdragtarget.dragenter + = new System.Windows.Forms.DragEventHandler (this. Listdragtarget_dragenter);
This. Listdragtarget.dragleave + = new System.EventHandler (this. Listdragtarget_dragleave);
Usecustomcursorscheck
This. Usecustomcursorscheck.location = new System.Drawing.Point (10, 243);
This. Usecustomcursorscheck.size = new System.Drawing.Size (137, 24);
This. Usecustomcursorscheck.text = "Use Custom Cursors";
Droplocationlabel
This. Droplocationlabel.location = new System.Drawing.Point (154, 245);
This. Droplocationlabel.size = new System.Drawing.Size (137, 24);
This. Droplocationlabel.text = "None";
Form1
This. ClientSize = new System.Drawing.Size (292, 270);
This. Controls.AddRange (New system.windows.forms.control[] {this. Listdragsource,
This. Listdragtarget, this. Usecustomcursorscheck,
This. Droplocationlabel});
This. Text = "Drag-and-drop Example";
This. ResumeLayout (FALSE);
}
private void Listdragsource_mousedown (object sender, System.Windows.Forms.MouseEventArgs e)
{
Get the index of the item the mouse is below.
Indexofitemundermousetodrag = Listdragsource.indexfrompoint (e.x, e.y);
if (Indexofitemundermousetodrag!= listbox.nomatches) {
Remember the point where the mouse down occurred. The dragsize indicates
The size that's mouse can move before a drag event should to be started.
Size dragsize = systeminformation.dragsize;
Create a rectangle using the dragsize, with the mouse position being
At the center of the rectangle.
Dragboxfrommousedown = new Rectangle (e.x-(DRAGSIZE.WIDTH/2),
E.y-(DRAGSIZE.HEIGHT/2)), dragsize);
} else
Reset the rectangle if the mouse isn't over a item in the ListBox.
Dragboxfrommousedown = Rectangle.empty;
}
private void Listdragsource_mouseup (object sender, System.Windows.Forms.MouseEventArgs e) {
Reset the drag rectangle the mouse button is raised.
Dragboxfrommousedown = Rectangle.empty;
}
private void Listdragsource_mousemove (object sender, System.Windows.Forms.MouseEventArgs e)
{
if ((E.button & mousebuttons.left) = = MouseButtons.Left) {
If the mouse moves outside the rectangle, start the drag.
if (Dragboxfrommousedown!= rectangle.empty &&
!dragboxfrommousedown.contains (e.x, e.y)) {
Create custom cursors for the Drag-and-drop operation.
try {
Mynormalcursor = new Cursor ("3dwarro.cur");
Mynodropcursor = new Cursor ("3dwno.cur");
catch {
An error occurred while attempting to load the cursors
Standard cursors.
usecustomcursorscheck.checked = false;
}finally {
The screenoffset is used to account for any desktop bands
That May is at the "top" or left side of the
Determining when to cancel the drag drop operation.
Screenoffset = SystemInformation.WorkingArea.Location;
Proceed with the Drag-and-drop and passing in the list item.
DragDropEffects dropeffect = Listdragsource.dodragdrop (Listdragsource.items[indexofitemundermousetodrag), DragDropEffects.All | Dragdropeffects.link);
If The drag operation is a move then remove the item.
if (DropEffect = = DragDropEffects.Move) {
ListDragSource.Items.RemoveAt (Indexofitemundermousetodrag);
Selects the previous item in the list as long as the "the" list has an item.
if (Indexofitemundermousetodrag > 0)
Listdragsource.selectedindex = indexOfItemUnderMouseToDrag-1;
else if (ListDragSource.Items.Count > 0)
Selects the "the".
Listdragsource.selectedindex = 0;
}
Dispose of the cursors since they are no longer needed.
if (mynormalcursor!= null)
Mynormalcursor.dispose ();
if (mynodropcursor!= null)
Mynodropcursor.dispose ();
}
}
}
}
private void Listdragsource_givefeedback (object sender, System.Windows.Forms.GiveFeedbackEventArgs e)
{
Use custom Cursors If the check box is checked.
if (usecustomcursorscheck.checked) {
Sets the custom cursor based upon the effect.
E.usedefaultcursors = false;
if ((E.effect & dragdropeffects.move) = = DragDropEffects.Move)
Cursor.current = Mynormalcursor;
Else
Cursor.current = Mynodropcursor;
}
}
private void Listdragtarget_dragover (object sender, System.Windows.Forms.DragEventArgs e)
{
Determine whether string data exists in the drop data. If not, then
The drop effect reflects that the drop cannot occur.
if (!e.data.getdatapresent (typeof (System.String)) {
E.effect = DragDropEffects.None;
Droplocationlabel.text = "none-no string data."
Return
}
Set the effect based upon the keystate.
if ((E.keystate & (8+32)) = = (8+32) &&
(E.allowedeffect & dragdropeffects.link) = = Dragdropeffects.link) {
Keystate 8 + = CTL + ALT
Link Drag-and-drop effect.
E.effect = Dragdropeffects.link;
else if ((e.keystate &) = = &&
(E.allowedeffect & dragdropeffects.link) = = Dragdropeffects.link) {
ALT keystate for link.
E.effect = Dragdropeffects.link;
else if ((E.keystate & 4) = = 4 &&
(E.allowedeffect & dragdropeffects.move) = = DragDropEffects.Move) {
SHIFT keystate for move.
E.effect = DragDropEffects.Move;
else if ((e.keystate & 8) = = 8 &&
(E.allowedeffect & dragdropeffects.copy) = = dragdropeffects.copy) {
CTL keystate for copy.
E.effect = dragdropeffects.copy;
else if ((E.allowedeffect & dragdropeffects.move) = = DragDropEffects.Move) {
By default, the drop action should is move, if allowed.
E.effect = DragDropEffects.Move;
} else
E.effect = DragDropEffects.None;
Get the index of the item the mouse is below.
The mouse locations are relative to the screens, so they must is
Converted to client coordinates.
Indexofitemundermousetodrop =
Listdragtarget.indexfrompoint (listdragtarget.pointtoclient) (New Point (e.x, e.y));
Updates the label text.
if (Indexofitemundermousetodrop!= listbox.nomatches) {
Droplocationlabel.text = "Drops before Item #" + (Indexofitemundermousetodrop + 1);
} else
Droplocationlabel.text = "Drops at" ";
}
private void Listdragtarget_dragdrop (object sender, System.Windows.Forms.DragEventArgs e)
{
Ensure that's the list item index is contained in the data.
if (E.data.getdatapresent (typeof (System.String)) {
Object item = (object) e.data.getdata (typeof (System.String));
Perform Drag-and-drop, depending upon the effect.
if (e.effect = = Dragdropeffects.copy | |
E.effect = = DragDropEffects.Move) {
Insert the item.
if (Indexofitemundermousetodrop!= listbox.nomatches)
ListDragTarget.Items.Insert (Indexofitemundermousetodrop, item);
Else
LISTDRAGTARGET.ITEMS.ADD (item);
}
}
Reset the label text.
Droplocationlabel.text = "None";
}
private void Listdragsource_querycontinuedrag (object sender, System.Windows.Forms.QueryContinueDragEventArgs e) {
Cancel the drag if the mouse moves off the form.
ListBox lb = sender as ListBox;
if (lb!= null) {
Form f = lb. Findform ();
Cancel the drag if the mouse moves off the form. The Screenoffset
Takes into account any desktop bands it is at the top or left
Side of the screen.
if ((Control.mouseposition.x-screenoffset.x) < F.desktopbounds.left) | |
((control.mouseposition.x-screenoffset.x) > F.desktopbounds.right) | |
((CONTROL.MOUSEPOSITION.Y-SCREENOFFSET.Y) < f.desktopbounds.top) | |
((CONTROL.MOUSEPOSITION.Y-SCREENOFFSET.Y) > F.desktopbounds.bottom)) {
E.action = Dragaction.cancel;
}
}
}
private void Listdragtarget_dragenter (object sender, System.Windows.Forms.DragEventArgs e) {
Reset the label text.
Droplocationlabel.text = "None";
}
private void Listdragtarget_dragleave (object sender, System.EventArgs e) {
Reset the label text.
Droplocationlabel.text = "None";
}
}
}
For the use of this drag-and-drop operation and Microsoft's service, container mode relationship, stay in the future to learn.