getting position of rectangle in canvas

14.1k views Asked by At

I'm making a software that just displays a shape in a canvas and if the user clicks that shape , it disappears and another shape is shown and so on. My question is how can I check if the user did click on the shape ? My solution is to get the position of the object in the canvas and the mouse position relative to the canvas and check if the mouse cursor is in the range of the shape's area. but I couldn't find the way to get the position of the object in the canvas.

1

There are 1 answers

0
Thomas Levesque On

You can get the position on the canvas of a control by calling the Canvas.GetLeft and Canvas.GetTop methods:

double x = Canvas.GetLeft(rect);
double y = Canvas.GetTop(rect);

But I don't think you need to do that anyway... Just handle the MouseDown event on the shape itself, rather than on the Canvas.