private void ListDragSource_MouseMove(object sender, MouseEventArgs e) { //注意这里的严密性, 最好不使用e.Button == MouseButtons.Left, 因为e.Button还可能具有其他的属性 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, 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_MouseMove(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Left) { if (dragBoxFromMouseDown != Rectangle.Empty && !dragBoxFromMouseDown.Contains(e.X, e.Y)) { screenOffset = SystemInformation.WorkingArea.Location; DragDropEffects dropEffect = ListDragSource.DoDragDrop(ListDragSource.Items[indexOfItemUnderMouseToDrag], DragDropEffects.All | DragDropEffects.Link); } } }
private void ListDragSource_MouseDown(object sender, MouseEventArgs e) { 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 should 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; } }