Pieter888
Pieter888

Reputation: 5012

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.

Upvotes: 3

Views: 4634

Answers (2)

Konamiman
Konamiman

Reputation: 50313

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

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

Upvotes: 5

Galwegian
Galwegian

Reputation: 42257

Check out the Cursor.Position property

Upvotes: 1

Related Questions