BLOG.CSHARPHELPER.COM: Let the user draw polygons and move them in C#
Let the user draw polygons and move them in C#
See the example Let the user draw polygons in C# for information about letting the user draw polygons. This program adds the ability to let the user drag polygons or their corner points.
The program uses MouseDown, MouseMove, and MouseUp event handlers to let the user draw and interact with the polygons. You could write three complex event handlers to handle every situation. For example, the MouseMove event handler would take different actions depending on whether the user was:
Drawing a new polygon
Moving a polygon
Moving a polygon's corner
Doing nothing
To make the event handlers easier to understand, this program uses separate, simpler event handlers to handle different tasks:
picCanvas_MouseDown - Handles all mouse down events. Depending on what is under the mouse, this will:
Add a new point to the new polygon.
Finish drawing the new polygon (right button).
Start moving a polygon's corner point.
Start moving a polygon.
Start drawing a new polygon.
picCanvas_MouseMove_Drawing - Moves the next point for a new polygon.
picCanvas_MouseMove_MovingCorner - Moves a polygon's corner.
picCanvas_MouseUp_MovingCorner - Stops moving a polygon's corner.
picCanvas_MouseMove_MovingPolygon - Moves a polygon.
picCanvas_MouseUp_MovingPolygon - Stops moving a polygon.
picCanvas_MouseMove_NotDrawing - Sees what the mouse is over and changes the cursor accordingly.
picCanvas_Paint - Redraws the polygons and any new polygon.
The various event handlers add and remove other event handlers as necessary. For example, the following code snippet shows how the program starts moving a polygon when you press the mouse down over it.
... else if (MouseIsOverPolygon(mouse_pt, 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; } ...
The MouseIsOverPolygon method returns true if the mouse is currently over a polygon. It returns that polygon through the output parameter hit_polygon.
If the mouse is over a polygon, the code removes the MouseMove event handler that is used while not drawing and installs the MouseMove and MouseUp event handlers used while moving a polygon. It saves the polygon it will move and saves the offset between the mouse and the polygon's first corner point.
The following code shows how the program moves a polygon.
// Move the selected polygon. private void picCanvas_MouseMove_MovingPolygon(object sender, MouseEventArgs e) { // See how far the first point will move. int new_x1 = e.X + OffsetX; int new_y1 = e.Y + OffsetY;
int dx = new_x1 - MovingPolygon[0].X; int dy = new_y1 - MovingPolygon[0].Y;
if (dx == 0 && dy == 0) return;
// Move the polygon. for (int i = 0; i < MovingPolygon.Count; i++) { MovingPolygon[i] = new Point( MovingPolygon[i].X + dx, MovingPolygon[i].Y + dy); }
// Redraw. picCanvas.Invalidate(); }
The code adds the original offset to the mouse's current position to determine where the polygon's first point should now be. It calculates the distance it must move that point from its current position and then adds that amount to each of the polygon's points. The code finishes by redrawing.
The following code shows how the program stops moving a polygon when you release the mouse.
This code simply restores the original event handlers that should be used while not drawing or moving anything.
Moving a corner point is similar to moving a polygon, with some changes in detail. Creating polygons is similar to the method used by the example Let the user draw polygons in C#. Download this example program for additional details.
Comments