private void btnMove_MouseMove(object sender, MouseEventArgs e) { if (_mouseDown && !Moving) { this.BringToFront(); _relativeToScreen = this.Top - this.PointToScreen(new Point(e.X, e.Y)).Y; Moving = true; _moveStarted = true; } if (Moving) { Point newLoc = new Point(this.Left, this.PointToScreen(new Point(e.X, e.Y)).Y + _relativeToScreen); EventHandler <LocationChangingEventArgs> locationChanging = LocationChanging; LocationChangingEventArgs locationArgs = new LocationChangingEventArgs(newLoc); if (locationChanging != null) { locationChanging(this, locationArgs); } this.Top = locationArgs.NewLocation.Y; } }
private void ControlLocationChanging(object sender, LocationChangingEventArgs e) { Control movingControl = sender as Control; if (movingControl == null) { return; } if (e.NewLocation.Y - movingControl.Margin.Top < 0) { e.NewLocation = new Point(e.NewLocation.X, movingControl.Margin.Top); } else if (e.NewLocation.Y + movingControl.Height + movingControl.Margin.Bottom > btnAdd.Top - btnAdd.Margin.Top) { e.NewLocation = new Point(e.NewLocation.X, btnAdd.Top - btnAdd.Margin.Top - movingControl.Height - movingControl.Margin.Bottom); } if (Panel.Height <= this.Height) { return; } int viewDelta = -Panel.Location.Y - e.NewLocation.Y; if (e.NewLocation.Y < -Panel.Location.Y) { AutoScrollPosition = new Point(_autoScrollPosition.X, e.NewLocation.Y + viewDelta); } }