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 workSad
Help?
[2033 byte] By [BrianGordon] at [2007-12-16]
# 1
Mouse Capture works only when either the left or the right or the middle button is depressed.
VijayeRaji at 2007-9-9 > top of Msdn Tech,Visual C#,Visual C# General...
# 2
Oh ok, well I read something on MSDN about an API called user32.dll or maybe win32.dll that somehow lets me get the mouse coords... I have no idea what to do (the only example was in VB) and don't know how to import an api anyway.
can you help?
BrianGordon at 2007-9-9 > top of Msdn Tech,Visual C#,Visual C# General...
# 3

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);

VijayeRaji at 2007-9-9 > top of Msdn Tech,Visual C#,Visual C# General...
# 4
And what exactly does that do? / How do I implement that so I get two realtime ints "x" and "y"?
BrianGordon at 2007-9-9 > top of Msdn Tech,Visual C#,Visual C# General...
# 5
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

VijayeRaji at 2007-9-9 > top of Msdn Tech,Visual C#,Visual C# General...