检查鼠标是否没有移动(静止不动)c#WinForms [英] Check if Mouse isn't moved (stands still) c# WinForms

查看:603
本文介绍了检查鼠标是否没有移动(静止不动)c#WinForms的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果鼠标不移动3秒钟,我希望button5消失.如果鼠标移动,则应再次出现button1.我使用带有以下代码的计时器:

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 ?

推荐答案

您最好的选择是实现

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

}

这篇关于检查鼠标是否没有移动(静止不动)c#WinForms的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆