Use of asp.net DoDragDrop Method

Source: Internet
Author: User
Tags custom cursors

The class library is defined:
Copy codeThe Code is as follows:
[UIPermissionAttribute (SecurityAction. Demand, Clipboard = UIPermissionClipboard. OwnClipboard)]
Public DragDropEffects DoDragDrop (
Object data,
DragDropEffects allowedEffects
)

The data parameter indicates the data to be dragged and dropped. If the drag operation requires the applications of another process to operate on each other, data indicates the data of the basic hosting class (String, BitMap, or MetaFile ), or the object that implements ISerializable or IDataObject. The allowedEffects parameter indicates the drag-and-drop effect, which is a DragDropEffects value. The returned value is also the DragDropEffects enumerated value.
When you start to call the DoDragDrop method to drag a data object, DoDragDrops checks whether the control at the current cursor position is a valid place target during the drag-and-drop process. If the control under the current cursor is a valid placement target, the GiveFeedBack event is triggered with the specified drag-and-drop effect. When detecting whether the cursor at the current position is a valid drag-and-drop target, the DoDragDrops method simultaneously tracks changes to the cursor position, keyboard status, and mouse status.
(1) If it is used to remove a window, the DragLeave event is triggered.
(2) If another control is moved in, the DragEnter event of the control is triggered.
(3) If the mouse moves but stays in a control, the DragOver event is triggered.
If the keyboard or mouse status is changed, the QueryContinueDrag event of the drag-and-drop source is triggered, and the drag-and-drop operation is performed based on the Action attribute value of QueryContinueDragEventArgs of the event.
(1) If the Action attribute is set to Continue, the DragOver event is triggered.
(2) If the Action attribute is set to Drop, the placement effect is returned to the source so that the application can perform proper operations on the data. For example, if the operation is a move operation, the data is cut.
(3) If the value of DragAction is Cancel, the DragLeave event is triggered.
Extract a sample code from csdn:
The following code example demonstrates the drag-and-drop operation between two ListBox controls. This example calls the DoDragDrop method when the drag action is started. During the MouseDown event, if the distance between the mouse and the mouse is greater than SystemInformation...:. DragSize, the drag action is started. The IndexFromPoint method is used to determine the index of the item to be dragged during the MouseDown event.
This example also demonstrates how to use a custom cursor for drag-and-drop operations. In this example, two cursor files are required in the application directory: 3dwarro. cur and 3dwno. cur, which are used to customize the drag cursor and disable the cursor parking. If UseCustomCursorsCheckCheckBox is selected, the custom cursor is used. The custom cursor is set in the GiveFeedback event handler.
The keyboard status is calculated in the DragOver event handler of the right ListBox to determine which type of drag operation will take place based on the Shift, Ctrl, Alt or Ctrl + Alt key state. The position where the placement action occurs in The ListBox is also determined during the DragOver event. If the data to be placed is not a String, DragDropEffects sets DragEventArgs. sEffect to None. Finally, the parking status is displayed in 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 position in The ListBox. If the drag operation moves outside the border of the form, the QueryContinueDrag event handler removes the drag and drop operation.
Copy codeThe Code is 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 usemcmcursorscheck;
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 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 the mouse can move before a drag event shocould 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 (new Point (e. X-(dragSize. Width/2 ),
E. Y-(dragSize. Height/2), dragSize );
} Else
// Reset the rectangle if the mouse is not over an item in the ListBox.
DragBoxFromMouseDown = Rectangle. Empty;
}
Private void ListDragSource_MouseUp (object sender, System. Windows. Forms. MouseEventArgs e ){
// Reset the drag rectangle when 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 M 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, so use
// Standard cursors.
UseCustomCursorsCheck. Checked = false;
} Finally {
// The screenOffset is used to account for any desktop bands
// That may be at the top or left side of the screen when
// Determining when to cancel the drag drop operation.
ScreenOffset = SystemInformation. WorkingArea. Location;
// Proceed with the drag-and-drop, passing in the list item.
DragDropEffects dropEffect = ListDragSource. DoDragDrop (ListDragSource. Items [indexOfItemUnderMouseToDrag], DragDropEffects. All | DragDropEffects. Link );
// If the drag operation was 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 list has an item.
If (indexOfItemUnderMouseToDrag> 0)
ListDragSource. SelectedIndex = indexOfItemUnderMouseToDrag-1;
Else if (ListDragSource. Items. Count> 0)
// Selects the first item.
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 M cursors if the check box is checked.
If (UseCustomCursorsCheck. Checked ){
// Sets the custom cursor based upon the effect.
E. usedefacurcursors = 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 + 32 = CTL + ALT
// Link drag-and-drop effect.
E. Effect = DragDropEffects. Link;
} Else if (e. KeyState & 32) = 32 &&
(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 shocould be 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 screen, so they must be
// 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 the end .";
}
Private void ListDragTarget_DragDrop (object sender, System. Windows. Forms. DragEventArgs e)
{
// Ensure that 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 that may be at the top or left
// Side of the screen.
If (Control. MousePosition. X-screenOffset. X) <f. Rows topbounds. Left) |
(Control. MousePosition. X-screenOffset. X)> f. Specify topbounds. Right) |
(Control. MousePosition. Y-screenOffset. Y) <f. Specify topbounds. Top) |
(Control. MousePosition. Y-screenOffset. Y)> f. Specify topbounds. 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 ";
}
}
}

The relationship between this drag-and-drop operation and Microsoft services and container mode will be studied later.

Related Article

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.