2

I want to create a windows form that when i click on a button it show mouse position and type of left click or right click in a label.I use this code:

this.Cursor = new Cursor(Cursor.Current.Handle);
label1.Text = Cursor.Position.X;
label1.Text+= Cursor.Position.Y;

but this show mouse position only from application form not anywhere,how to change it that return mouse position from desktop no a form?

thank you in advance.

user3651326
  • 122
  • 9
  • 3
    [This is not true.](https://msdn.microsoft.com/en-us/library/system.windows.forms.cursor.position%28v=vs.110%29.aspx) : _A Point that represents the cursor's position in screen coordinates._ You can calculate to relative coodinates using PointToClient and back with PointToScreen. – TaW May 10 '16 at 12:41
  • The only chance I see is making your form full screen and transparent. Otherwise, you'll only have the coordinates when your app has the focus. – Oscar May 10 '16 at 12:41
  • 1
    You answer is here http://www.codeproject.com/Articles/7294/Processing-Global-Mouse-and-Keyboard-Hooks-in-C – mohsen May 10 '16 at 12:54
  • 1
    Possible duplicate of [C# Mouse Movement Outside Form](http://stackoverflow.com/questions/20386876/c-sharp-mouse-movement-outside-form) – mohsen May 10 '16 at 12:57

2 Answers2

2

You can add to the cursor position in the form, the position of the form in the screen.

Label1.Text = Form1.Location.X + Cursor.Position.X;
Label1.Text += Form1.Location.Y + Cursor.Position.Y;
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
Paulo Prestes
  • 484
  • 2
  • 8
2

Here is a good article how help you to getting Position of mouse cursor when clicked out side the form's : Get Cursor Position outside form

Beldi Anouar
  • 2,170
  • 1
  • 12
  • 17