// See if we're over a polygon or corner point. private void picCanvas_MouseMove_NotDrawing(object sender, MouseEventArgs e) { Cursor new_cursor = Cursors.Cross; // See what we're over. Point mouse_pt = SnapToGrid(e.Location); if (MouseUtils.MouseIsOverCornerPoint(mouse_pt, Polygons)) { new_cursor = Cursors.Arrow; } else if (MouseUtils.MouseIsOverEdge(mouse_pt, Polygons)) { new_cursor = AddPointCursor; } else if (MouseUtils.MouseIsOverPolygon(mouse_pt, Polygons)) { new_cursor = Cursors.Hand; } // Set the new cursor. if (picCanvas.Cursor != new_cursor) { picCanvas.Cursor = new_cursor; } }
// Start or continue drawing a new polygon, // or start moving a corner or polygon. private void picCanvas_MouseDown(object sender, MouseEventArgs e) { // See what we're over. Point mouse_pt = SnapToGrid(e.Location); Polygon hit_polygon; int hit_point, hit_point2; Point closest_point; if (NewPolygon != null) { // We are already drawing a polygon. // If it's the right mouse button, finish this polygon. if (e.Button == MouseButtons.Right) { // Finish this polygon. if (NewPolygon.Count > 2) { Polygons.Add(NewPolygon); } NewPolygon = null; // We no longer are drawing. picCanvas.MouseMove += picCanvas_MouseMove_NotDrawing; picCanvas.MouseMove -= picCanvas_MouseMove_Drawing; } else { // Add a point to this polygon. if (NewPolygon[NewPolygon.Count - 1] != mouse_pt) { NewPolygon.Add(mouse_pt); } } } else if (MouseUtils.MouseIsOverCornerPoint(mouse_pt, Polygons, out hit_polygon, out hit_point)) { // Start dragging this corner. picCanvas_MouseDown_MoveCorner(hit_polygon, hit_point, e); } else if (MouseUtils.MouseIsOverEdge(mouse_pt, Polygons, out hit_polygon, out hit_point, out hit_point2, out closest_point)) { // Add a point. hit_polygon.Insert(hit_point + 1, closest_point); // Start dragging this corner. picCanvas_MouseDown_MoveCorner(hit_polygon, hit_point + 1, e); } else if (MouseUtils.MouseIsOverPolygon(mouse_pt, Polygons, out hit_polygon)) { // Start moving this polygon. picCanvas.MouseMove -= picCanvas_MouseMove_NotDrawing; picCanvas.MouseMove += picCanvas_MouseMove_MovingPolygon; picCanvas.MouseUp += picCanvas_MouseUp_MovingPolygon; // Remember the polygon. MovingPolygon = hit_polygon; // Remember the offset from the mouse to the segment's first point. OffsetX = hit_polygon[0].X - e.X; OffsetY = hit_polygon[0].Y - e.Y; } else { // Start a new polygon. NewPolygon = new Polygon(); NewPoint = mouse_pt; NewPolygon.Add(mouse_pt); // Get ready to work on the new polygon. picCanvas.MouseMove -= picCanvas_MouseMove_NotDrawing; picCanvas.MouseMove += picCanvas_MouseMove_Drawing; } // Redraw. picCanvas.Invalidate(); }