0

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

In my Form I have a Panel 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 MouseMoveEvent 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 MouseMoveEvent:

protected override void OnMouseMove(MouseEventArgs e)
{
    MessageBox.Show("Moved!");
}

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?

Spontifixus
  • 6,570
  • 9
  • 45
  • 63
Shift 'n Tab
  • 8,808
  • 12
  • 73
  • 117
  • So you want to trigger the `OnMouseMove` of Form control(say `FrmMain_OnMouseMove`) from the mouse move of the child panels. isn't it? – sujith karivelil Aug 16 '16 at 07:27
  • yes sir, the function will serve to hide the current `form` and show another `form` this is something like a screen saver when the mouse moved the desktop UI appears. – Shift 'n Tab Aug 16 '16 at 07:29
  • You could use a low level mouse hook. [Please check this topic](http://stackoverflow.com/questions/2063974/how-do-i-capture-the-mouse-move-event) – drak1988 Aug 16 '16 at 08:19
  • @drak1988 i followed the implementation but the function was called even though the mouse did not moved. – Shift 'n Tab Aug 16 '16 at 08:28

3 Answers3

1

This example works correct for me, program call TheMouseMoved() method only if I move mouse.

public partial class Form1 : Form
{
    int counter = 0;
    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);
        System.Console.WriteLine("{0}. [ {1},{2} ]", counter++, (cur_pos.X - this.Location.X), (cur_pos.Y - this.Location.Y));
    }
}

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();
            }
        }
        // Always allow message to continue to the next filter control
        return false;
    }

    #endregion
}
drak1988
  • 41
  • 5
0

Try the "MouseMove" Event on the panel. If you disable the docked panel, the "MouseMove" Event of the Forms will be triggered.

Hannes
  • 31
  • 2
  • 5
  • 1
    the mouse move event of a panel still wont work because there are many panels inside it is not a good approach if i will write down each mousemove event it each panel inside. – Shift 'n Tab Aug 16 '16 at 07:42
  • The mouse move event always triggers for the top most control. So the implemented mouse move event of your underlying form won't be triggered. – Hannes Aug 16 '16 at 08:18
0

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

        #endregion
    }
}
Community
  • 1
  • 1
Shift 'n Tab
  • 8,808
  • 12
  • 73
  • 117