Enabling Form.Capture
I'm sorry, I'm trying to find the screen coordinates of the mouse cursor (with
System.Windows.Forms.Cursor.Position) and I've been told several times that I need to enable Form.Capture to get mouse input from anywhere on the screen. I'm not sure how to do that; I've tried several ways and I can't get it to work:
| | protectedoverridevoid OnMouseMove(MouseEventArgs e) { this.Capture =true; textBox1.Text = System.Windows.Forms.Cursor.Position.X.ToString(); }
|
| | public Form1() { InitializeComponent(); this.Capture =true; }protectedoverridevoid OnMouseMove(MouseEventArgs e) { textBox1.Text = System.Windows.Forms.Cursor.Position.X.ToString(); }
|
| | Capture =true; protectedoverridevoid OnMouseMove(MouseEventArgs e) { textBox1.Text = System.Windows.Forms.Cursor.Position.X.ToString(); }
|
And none of them work

Help?
Getting the mouse co-ordinates is facilitated by .Net itself. Infact you have it right: System.Windows.Forms.Cursor.Position will give you the current mouse co-ordinates.
The problem is knowing when this has changed so you can call it. To do that you use Windows Hooks.
| | static int WH_MOUSE_LL = 14; [System.Runtime.InteropServices.DllImport("kernel32.dll")] static extern IntPtr GetModuleHandle(string moduleName); [System.Runtime.InteropServices.DllImport("user32.dll")] static extern IntPtr SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr hMod, uint dwThreadId); [System.Runtime.InteropServices.DllImport("user32.dll")] static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode, uint wParam, IntPtr lParam); delegate IntPtr HookProc(int nCode, uint wParam, IntPtr lParam); IntPtr LowLevelMouseProc(int nCode, uint wParam, IntPtr lParam) { textBox1.Text = Cursor.Position.ToString(); return CallNextHookEx(IntPtr.Zero, nCode, wParam, lParam); } |
And in your initializer add the following code
| | IntPtr hook = SetWindowsHookEx(WH_MOUSE_LL, new HookProc(LowLevelMouseProc), GetModuleHandle(null), 0);
|
Um... Can you elaborate on your question?
The code above would capture mouse cursor position anywhere in the screen.
You can get "x" and "y" from the System.Windows.Forms.Cursor.Position as such
System.Windows.Forms.Cursor.Position.X
System.Windows.Forms.Cursor.Position.Y