As someone pointed out here , you can use SetWindowsHookEx () or simply hook the MouseMove event to all the controls on the form. The latter works fine for me. The only drawback is adding or removing controls at runtime; you may need a different solution.
using System;
using System.Drawing;
using System.Windows.Forms;
namespace WindowsForms_MouseEvents
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
MouseMove += OnMouseMove;
MouseLeave += OnMouseLeave;
HookMouseMove(this.Controls);
}
private void HookMouseMove(Control.ControlCollection ctls)
{
foreach (Control ctl in ctls)
{
ctl.MouseMove += OnMouseMove;
HookMouseMove(ctl.Controls);
}
}
private void OnMouseMove(object sender, MouseEventArgs e)
{
BackColor = Color.Plum;
Control ctl = sender as Control;
if (ctl != null)
{
Point loc = this.PointToClient(ctl.PointToScreen(e.Location));
Console.WriteLine("Mouse at {0},{1}", loc.X, loc.Y);
}
}
private void OnMouseLeave(object sender, EventArgs e)
{
BackColor = Color.Gray;
}
}
}
source
share