protected override void OnMouseLeave(EventArgs e) { /* Called when the mouse leaves the GridBox altogether. * We must reset the colour of the last active square * to its own BackColor. * * We don't need to handle a specific MouseEnter() event for the GridBox * because the MouseMove handler will already pick it up. */ if (ActiveSquare != null) { ActiveSquare.OnMouseLeave(); ActiveSquare = null; } }
protected override void OnMouseMove(MouseEventArgs e) { /* We have to check the position of the mouse on the grid (in pixels), * and obtain the Square object which resides in that position. * If that square is not also the currently set ActiveSquare, * it means that we've left the previously active square, and we * have to reset its colour to its own BackColor. */ Square squareObj = GetSquare(e.X, e.Y); if (squareObj == null) { // No such square. We must be out of bounds. Console.WriteLine("MouseMove error. No such square. Out of bounds."); return; } if (squareObj != ActiveSquare) { /* Either ActiveSquare is null, or there's a previously active square. * In the case of a previously active on, we must call OnMouseLeave() on it. * We also have to call OnMouseEnter() on the new square. */ if (ActiveSquare != null) { // There is a previously active square ActiveSquare.OnMouseLeave(); ActiveSquare = squareObj; } /* Call OnMouseEnter() on the newly entered square. * We do this here to avoid calling OnMouseEnter() * whenever the mouse moves inside the square * after having already entered its area. * * However, before calling OnMouseEnter(), we must check whether * one of the mouse buttons is currently pressed. * If it's pressed, it means that the square is caught in a mouse drag, * and therefore should be notified that this is happening. */ if (LeftMouseDown) { squareObj.MouseEvent = MouseEventType.LeftButtonDrag; } else if (RightMouseDown) { squareObj.MouseEvent = MouseEventType.RightButtonDrag; } squareObj.OnMouseEnter(); } // Set our current square object as the active square ActiveSquare = squareObj; }