c# - How do I capture the mouse move event -


i capture mouse move event in main form. although able wire mouseeventhandler main form, event no longer fires when cursor on usercontrol or other control. how ensure have mouse position.

you use low level mouse hook. see this example , check wm_mousemove mesage in hookcallback.

you use imessagefilter class catch mouse events , trigger event position (note: position on window, not outside of it):

using system; using system.collections.generic; using system.componentmodel; using system.data; using system.drawing; using system.text; using system.windows.forms;  namespace globalmouseevents {    public partial class form1 : form    {       public form1()       {          globalmousehandler gmh = new globalmousehandler();          gmh.themousemoved += new mousemovedevent(gmh_themousemoved);          application.addmessagefilter(gmh);           initializecomponent();       }        void gmh_themousemoved()       {          point cur_pos = system.windows.forms.cursor.position;          system.console.writeline(cur_pos);       }    }     public delegate void mousemovedevent();     public class globalmousehandler : imessagefilter    {       private const int wm_mousemove = 0x0200;        public event mousemovedevent themousemoved;        #region imessagefilter members        public bool prefiltermessage(ref message m)       {          if (m.msg == wm_mousemove)          {             if (themousemoved != null)             {                themousemoved();             }          }          // allow message continue next filter control          return false;       }        #endregion    } } 

Comments

Popular posts from this blog

unicode - Are email addresses allowed to contain non-alphanumeric characters? -

c++ - Convert big endian to little endian when reading from a binary file -

C#: Application without a window or taskbar item (background app) that can still use Console.WriteLine() -