Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 357
CodeMouse92 Avatar asked Apr 17 '11 00:04

CodeMouse92


People also ask

How do you get the mouse position on canvas?

The dimension of the canvas is found using the getBoundingClientRect() function. This method returns the size of an element and its position relative to the viewport. The position of x-coordinate of the mouse click is found by subtracting the event's x position with the bounding rectangle's x position.

How can I see my mouse position on my screen?

In Mouse Properties, on the Pointer Options tab, at the bottom, select Show location of pointer when I press the CTRL key, and then select OK.

What method do you use to get the mouse point position for a mouse event?

getPointerInfo(). getLocation() might be helpful. It returns a Point object corresponding to current mouse position. getPointerInfo().

Which method is used to get the Y coordinate of the point where user have clicked using mouse button?

The moveTo() function will move the mouse cursor to the X and Y integer coordinates you pass it.


2 Answers

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,

like image 106
H.B. Avatar answered Sep 30 '22 05:09

H.B.


Hi the important thing is the

NOT on the Window

the canvas is part of the window as well. one example:

  • the Window.AllowsTransparency state is on true
  • the Window.Background is #00000000 (completely transparent)
  • the Window.Style is None
  • the Window.State is Maximized and
  • there are NO controls or elements on the window!

... so if you start the application you will see Nothing now tell me how to get the mouseposition on the screen in pixel

!Warning! if you juse Mouse.GetPosition(this); it will return x0 y0 every time

like image 38
Phillip Avatar answered Sep 30 '22 05:09

Phillip