Click here to Skip to main content
15,896,269 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I want to retrieve the mouse pointer's x , y cordinates when my mouse clicks anywhere in the screen , it should be independent of windows form .
ex. if i click mouse in notepad it should give me cordinates , i tried following but it gives cordinated only when i clicked in myform.

please suggest me way out of this issue.
thanks.

What I have tried:

C#
private void Form1_MouseClick(object sender, MouseEventArgs e)
{
string x_cord = Cursor.Position.X.ToString();
string y_cord = Cursor.Position.Y.ToString();
MessageBox.Show("cordinates are " + x_cord +","+ y_cord);
}
Posted
Updated 11-Jul-16 0:57am

That's not nice, not at all. You are talking about intercepting mouse actions that aren;t intended for your form, and that's not the way it's all supposed to work.
As a result, it's difficult. Very difficult. And also dangerous...
You can do it, but you need a Global Hook: Global Windows Hooks[^]
Be aware: if you get it wrong - even slightly - you can render your whole system unstable and may "cause data loss".
Think twice before you do this: you will probably have to reboot your PC quite a few times in development, and I'd recommend an even more rigorous routine of backups than I normally would (and this from someone whose system does incrementals every hour...
 
Share this answer
 
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900