Shift N' Tab Shift N' Tab - 2 months ago 4x
C# Question

How to handle the MouseMoveEvent in the underlying Form instead of the Panels inside it?

I have an application written in C#, It is a directory system that will display information in a slideshow fashion.

In my

I have a
docked to fill the form's content. Inside that panel there are 9 panels where each one displays the information of a particular object.

Now what I want is that whenever I move the mouse I want to trigger the
of the form hosting the panel, instead of those of the big panel or the panels inside it.

Here is my code handling the form's

protected override void OnMouseMove(MouseEventArgs e)

I know that this will not fire because the mouse cursor is inside the panel but how to trigger the event on the form anyway?

The purpose of this is to hide the current form and show another form when mouse cursor inside the form moved. It is possible?


I solve the problem by modifying the answer from How do I capture the mouse move event because the accepted answer is continuously firing even though the mouse is not moving according to @Randy Gamage comment.

I solved it using this code.

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


      void gmh_TheMouseMoved()
         Point cur_pos = System.Windows.Forms.Cursor.Position;

   public delegate void MouseMovedEvent();

   public class GlobalMouseHandler : IMessageFilter
        private const int WM_MOUSEMOVE = 0x0200;
        private System.Drawing.Point previousMousePosition = new System.Drawing.Point();
        public static event EventHandler<MouseEventArgs> MouseMovedEvent = delegate { };

        #region IMessageFilter Members

        public bool PreFilterMessage(ref System.Windows.Forms.Message m)
            if (m.Msg == WM_MOUSEMOVE)
                System.Drawing.Point currentMousePoint = Control.MousePosition;

                // Prevent event from firing twice.
                if (previousMousePosition == new System.Drawing.Point(0, 0))
                { return; }

                if (previousMousePosition != currentMousePoint)
                    previousMousePosition = currentMousePoint;
                    MouseMovedEvent(this, new MouseEventArgs(MouseButtons.None, 0, currentMousePoint.X, currentMousePoint.Y, 0));
            // Always allow message to continue to the next filter control
            return false;