Search code examples
c#css-positioncursor-position

How to get/set the position of the mouse relative from the application window?


I want to be able to get the current location of the mouse pointer, relative from where my form window is. So if I would put my mouse on the top left of the form, it would give me the x and y values of x=0 and y=0, while the form itself might be in the middle of the screen.

Also I want to set the position of the mouse.

How can this be done in C#? I'm using a windows forms application.


Solution

  • Obtain the screen coordinates by using Cursor.Position, then convert to window coordinates invoking PointToClient on the window.

    Point p = this.PointToClient(Cursor.Position);