Capturing Mouse Movement

Many of you have probably seen "mouse meters" that track how many pixels the mouse has moved. I'm trying to do that but I can't figure out how to execute code every time there's a mousemove (on any control, let alone the entire screen). This is what I've gotten so far but it doesn't work. Any ideas?

namespace mmove
{
public partialclass Form1 : Form
{
int pxls = 0;
public Form1()
{
InitializeComponent();
}
privatevoid OnMouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
pxls++;
textBox1.Text = pxls.ToString();
}

}
}


[1051 byte] By [BrianGordon] at [2007-12-16]
# 1
If you want to capture mouse events when the mouse has moved away from your control, you'll need to set "capture" to on.

Try calling

this.Capture = true;

on your form's OnLoad override.

VijayeRaji at 2007-9-9 > top of Msdn Tech,Visual C#,Visual C# General...
# 3
OK, but that seems to freeze my computer :p
I rewrote it slightly and I got a working program but it only counts while the mouse is in the application window. I have this.capture set as i was told... here's my complete code

namespace mmove
{
public partial class Form1 : Form
{
int pxls = 0;
public Form1()
{
InitializeComponent();
}
protected override void OnMouseMove(MouseEventArgs e)
{
pxls++;
textBox1.Text = pxls.ToString();
}
private void Form1_Load(object sender, EventArgs e)
{
this.Capture = true;
}

}
}


BrianGordon at 2007-9-9 > top of Msdn Tech,Visual C#,Visual C# General...
# 4
also it takes a triple-click on the X to close the program... does anyone know how I can fix this?
BrianGordon at 2007-9-9 > top of Msdn Tech,Visual C#,Visual C# General...
# 5
I've rewritten my code but it still only works within the application window. Help?

namespace mmove
{
public partial class Form1 : Form
{
int pxls = 0;
int lx = 0;
int ly = 0;
bool started = false;
public Form1()
{
InitializeComponent();
}
protected override void OnMouseMove(MouseEventArgs e)
{
if (started == false)
{
lx = e.X;
ly = e.Y;
started = true;
}
if (e.Y - ly == 0)
pxls = pxls + (int)Math.Abs(e.X - lx);
else if (e.X - lx == 0)
pxls = pxls + (int)Math.Abs(e.Y - ly);
else
{
int slope = (int)Math.Abs((e.Y - ly) / (e.X - lx));
if (slope >= 1)
pxls = pxls + (int)Math.Abs(e.Y - ly);
else if (slope < 1)
pxls = pxls + (int)Math.Abs(e.X - lx);
}
lx = e.X;
ly = e.Y;
}
private void Form1_Load(object sender, EventArgs e)
{
this.Capture = true;
}
private void timer1_Tick(object sender, EventArgs e)
{
textBox1.Text = pxls.ToString();
}
}
}


BrianGordon at 2007-9-9 > top of Msdn Tech,Visual C#,Visual C# General...
# 6
Hi,
You could try implementing DirectX's Direct Input. It handles mouse moves all over the screen... And also you could capture keyboards, joystick and other input devices...
cheers,
Paul June A. Domag
PaulDomag at 2007-9-9 > top of Msdn Tech,Visual C#,Visual C# General...