Get Mouse Position on Canvas (But NOT on window)?

CodeMouse92 picture CodeMouse92 · Apr 17, 2011 · Viewed 42.3k times · Source

I have a project in WPF 4 and vb.net 2010.

I have a canvas inside a window. The window is full screen, but the canvas is set to a solid 640x480 in the center of the window. I need to get the mouse position inside of the canvas, but NOT inside of the window. How do I do this?

Answer

H.B. picture H.B. · Apr 17, 2011

Doesn't this work?

Point p = Mouse.GetPosition(canvas);

The position of the mouse pointer is calculated relative to the specified element with the upper-left corner of element being the point of origin,