// Handle mouse up; perform a local move protected override void OnMouseUp(MouseEventArgs mouseEventArgs) { this.Cursor = Cursors.Arrow; // Continue? if (mouseEventArgs.Button != MouseButtons.Left) { return; } // Unregister the mouse move event this.MouseMove -= new MouseEventHandler(DragItem_MouseMove); // New location this.Left += mouseEventArgs.X - startPoint.X; this.Top += mouseEventArgs.Y - startPoint.Y; if (!this.IsOutSide) { // Snap to parent grid? this.Location = (Parent as DragContainer).SnapToGrid(this.Location); // Here we might use an interface instead of casting... this.OrignalLocation = this.Location; } else { this.Location = this.OrignalLocation; } // Hide the outline object ShaDowControl.Hide(); this.onSelectDragEvent(this.Name); base.OnMouseUp(mouseEventArgs); }
// Handle mouse-move; If we are out of parent container then perform global drag protected void DragItem_MouseMove(object sender, MouseEventArgs mouseEventArgs) { if (this.IsFirstClickDrag) { this.Cursor = Cursors.NoMove2D; } // Get the current mouse coordinates as screen coordinates Point pMin = new Point((mouseEventArgs.X - startPoint.X), (mouseEventArgs.Y - startPoint.Y)); Point pMax = new Point(mouseEventArgs.X + this.Width, mouseEventArgs.Y + this.Height); Point minPoint = this.PointToScreen(pMin); Point maxPoint = new Point(minPoint.X + this.Width, minPoint.Y + this.Height); this.IsOutSide = (Parent as DragContainer).IsCursorOutside(minPoint, maxPoint); // Have we left the parent container? if (this.IsOutSide == true) { // Unregister this event this.MouseMove -= new MouseEventHandler(DragItem_MouseMove); ShaDowControl.Hide(); // Hide the outline object // ShaDowControl.SetBackColor(Color.Red); // ShaDowControl.Move(this.PointToScreen(new Point(mouseEventArgs.X - startPoint.X, mouseEventArgs.Y - startPoint.Y))); // Do start the drag-drop action this.DoDragDrop(this, DragDropEffects.Move); } else { ShaDowControl.SetBackColor(Color.Black); // Move the outline object ShaDowControl.Move(this.PointToScreen(new Point(mouseEventArgs.X - startPoint.X, mouseEventArgs.Y - startPoint.Y))); } }