Пример #1
0
 protected override void OnMouseDown(MouseEventArgs e)
 {
     // listen for mouse click or starting to drag
     base.OnMouseDown(e);
     if (AllowDragDrop)
     {
         int pickedImageIndex = GetImageIndex(e.Location);
         // check with -1 to ignore when the user clicking to blank zone
         if (pickedImageIndex > -1)
         {
             _dragItem       = new DraggingItem(_imgs[pickedImageIndex], e.Location);
             _dragItem.Index = pickedImageIndex;
         }
     }
 }
Пример #2
0
        protected override void OnMouseUp(MouseEventArgs e)
        {
            base.OnMouseUp(e);
            // checkin if swap is needed
            if (_dragItem != null && e.Location != _dragItem.PickedLocation)
            {
                int droppedIndex = GetImageIndex(e.Location);
                // check if the user drag an image then drop into blank zone in grid view
                // then move the dragged item into the end of image list
                if (droppedIndex == -1 && this.Bounds.Contains(e.Location))
                {
                    droppedIndex = _imgs.Count - 1;
                }

                // and only swap if the user doesn't click to another non-client rectangle
                if (droppedIndex != -1)
                {
                    // swap if picked != dropped
                    if (droppedIndex != _dragItem.Index)
                    {
                        _imgs[_dragItem.Index] = _imgs[droppedIndex];
                        _imgs[droppedIndex]    = _dragItem.ItemRef;
                    }
                }

                ReDraw();
            }
            else // click
            {
                this.SelectedIndex = GetImageIndex(e.Location);
                // in case click, change selected index to invoke index changed event
                if (e.Button == System.Windows.Forms.MouseButtons.Left)
                {
                    // perform click
                    if (OnImageClicked != null)
                    {
                        OnImageClicked(this, new ImageGridItemClickedEventArgs()
                        {
                            Index = this.SelectedIndex,
                            Image = _imgs[this.SelectedIndex].Original
                        });
                    }
                }
            }

            // then un-link drag item
            _dragItem = null;
        }