Пример #1
0
 private void DataGridViewItens_MouseMove(object sender, MouseEventArgs e)
 {
     if ((e.Button & MouseButtons.Left) != MouseButtons.Left)
     {
         return;
     }
     // If the mouse moves outside the rectangle, start the drag.
     if (_dragBoxFromMouseDown != Rectangle.Empty && !_dragBoxFromMouseDown.Contains(e.X, e.Y))
     {
         // Proceed with the drag and drop, passing in the list item.
         var dropEffect = DataGridViewItens.DoDragDrop(_itemFromMouseDown, DragDropEffects.Move);
     }
 }
Пример #2
0
        private void DataGridViewItens_MouseDown(object sender, MouseEventArgs e)
        {
            // Get the index of the item the mouse is below.
            var hittestInfo = DataGridViewItens.HitTest(e.X, e.Y);

            if (hittestInfo.RowIndex != -1 && hittestInfo.ColumnIndex != -1)
            {
                _itemFromMouseDown = _itens[hittestInfo.RowIndex];
                // 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.
                var 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;
            }
        }