I’m creating a simple drawing application using WPF. I’ve run into some strange behaviour when using the mouse_up event on a canvas control.
The effect I want is that when the user clicks on the canvas and then drags the mouse, the chosen shape grows. When the user releases the mouse button then the shape stays as it is. I have it working fine with an ellipse, as such:
//Calculate width and height based on difference from start and current mouse positions
int width = currentXPosition < StartXPosition ? StartXPosition - currentXPosition : currentXPosition - StartXPosition;
int height = currentYPosition < StartYPosition ? StartYPosition - currentYPosition : currentYPosition - StartYPosition;
StandardEllipse ellipse = new StandardEllipse
{
Fill = new SolidColorBrush(Colors.Blue),
Stroke = new SolidColorBrush(Colors.Blue),
StrokeThickness = 2,
Width = width,
Height = height
};
//Calculate and set the starting positions of the ellipse for the drawing canvas
int left = currentXPosition < StartXPosition ? currentXPosition : StartXPosition;
int top = currentYPosition < StartYPosition ? currentYPosition : StartYPosition;
Canvas.SetLeft(ellipse, left);
Canvas.SetTop(ellipse, top);
return ellipse;
However, when I try with a rectangle, using exactly the same code above, apart from setting the shape to rectangle, the mouse up event fires sporadically, I have to click the mouse button a number of times until it fires the event.
I’m not sure if there is a conflict between the mouse_move and mouse_up events somehow, but I can’t see how as all other code is exactly the same apart from returning a rectangle instead of an ellipse Anybody any ideas?
Edit: I thought it may be the fact that when I am drawing the rectangle, the mouse is strictly over the very edge of the shape, so I tried setting the width and height to 99% and it seems to work okay now! If anybody has any insight, then I would be grateful.
On an elipse, the drag corner is outside the bounds of the shape, on a rectangle the drag corner is within the bounds of the shape. It may be that the mouse event is not being routed correctly to the canvas.