Search code examples
c#buttontimermouse

Check if Mouse isn't moved (stands still) c# WinForms


I want button5 to dissapear if the mouse isn't moved for 3 seconds. If the mouse moved, button1 should appear again. I used a timer with this code:

    private void timer2_Tick(object sender, EventArgs e)
    {
        if (button5.Visible == true)
        {

            timer2.Stop();
            button2.Visible = false;
            button3.Visible = false;
            aimen_IPTV2.Visible = false;
            button5.Visible = false;
            button6.Visible = false;
            aimen_IPTV1.Visible = false;

        }
    }


    private void transpCtrl1_MouseMove(object sender, MouseEventArgs e)
    {

            button5.Visible = true;
            button6.Visible = true;
            aimen_IPTV1.Visible = true;



        }`

It didn't work can somebody help me out ?


Solution

  • Your best bet is to implement IMessageFilter so you can trap mouse movement across your whole application before it gets dispatched to each individual control (where it will be a mess to intercept):

    public partial class Form1 : Form
    {
    
        public Form1()
        {
            InitializeComponent();
        }
    
        private MyFilter mf;
    
        private void Form1_Load(object sender, EventArgs e)
        {
            button5.Visible = false;
            mf = new MyFilter();
            mf.MouseMoved += Mf_MouseMoved;
            mf.ThreeSecondWithoutMouseMove += Mf_ThreeSecondWithoutMouseMove;
            Application.AddMessageFilter(mf);
        }
    
        private void Mf_MouseMoved()
        {
            button5.Visible = true;
        }
    
        private void Mf_ThreeSecondWithoutMouseMove()
        {
            button5.Visible = false;
        }
    
        private void button5_Click(object sender, EventArgs e)
        {
            MessageBox.Show("You clicked me!");
        }
    }
    
    public class MyFilter : IMessageFilter
    {
    
        public delegate void dlgMouseMoved();
        public delegate void dlgThreeSeconds();
        public event dlgMouseMoved MouseMoved;
        public event dlgThreeSeconds ThreeSecondWithoutMouseMove;
    
        private Point lastPoint;
        private const int WM_MOUSEMOVE = 0x200;
        private System.Windows.Forms.Timer tmr;
    
        public MyFilter()
        {
            tmr = new System.Windows.Forms.Timer();
            tmr.Enabled = false;
            tmr.Interval = (int)TimeSpan.FromSeconds(3).TotalMilliseconds;
            tmr.Tick += Tmr_Tick;
        }
    
        bool IMessageFilter.PreFilterMessage(ref Message m)
        {
            switch (m.Msg)
            {
                case WM_MOUSEMOVE: // you WILL get phantom WM_MOUSEMOVE messages, when the mouse has NOT moved!
                    Point curPoint = Cursor.Position;
                    if (!curPoint.Equals(lastPoint)) 
                    {
                        lastPoint = curPoint;
                        if (MouseMoved != null)
                        {
                            MouseMoved();
                        }
                        tmr.Stop();
                        tmr.Start();
                    }
                    break;
            }
            return false; // handle messages normally
        }
    
        private void Tmr_Tick(object sender, EventArgs e)
        {
            tmr.Stop();
            if (ThreeSecondWithoutMouseMove != null)
            {
                ThreeSecondWithoutMouseMove();
            }
        }
    
    }