namespace mmove { public partial class Form1 : Form { int pxls = 0; public Form1() { InitializeComponent(); } private void OnMouseMove(object sender, System.Windows.Forms.MouseEventArgs e) { pxls++; textBox1.Text = pxls.ToString(); } } } |
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

Capturing Mouse Movement
Amit Bhave
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();
}
}
}
gabriewo
You could try using the mouse_event API...
http://msdn.microsoft.com/library/default.asp url=/library/en-us/winui/winui/windowsuserinterface/userinput/mouseinput/mouseinputreference/mouseinputfunctions/mouse_event.asp
cheers,
Paul June A. Domag
shhameed
Try calling
this.Capture = true;
on your form's OnLoad override.
dodo.net
Ken Wilson
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;
}
}
}
M.D.luffy
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