获取C#WPF中光标位置X和Y不变的时刻

获取C#WPF中光标位置X和Y不变的时刻,c#,wpf,mouse,C#,Wpf,Mouse,我想得到光标位置不变的时刻。我的意思是当鼠标停下来时,我想做点什么。但我会这么做很多次。我用了调度计时器。但它不允许我在里面做同样的事情。例如: timer.Interval = new TimeSpan(0, 0, 0, 1); timer.Tick += (sd, args) => // this is triggered when mouse stops. { if (a== b) {

我想得到光标位置不变的时刻。我的意思是当鼠标停下来时,我想做点什么。但我会这么做很多次。我用了调度计时器。但它不允许我在里面做同样的事情。例如:

        timer.Interval = new TimeSpan(0, 0, 0, 1);
        timer.Tick += (sd, args) => // this is triggered when mouse stops.
        {

            if (a== b)
            {
               I do something here }; // it works until here.

            }


         timer2.Tick += (sd, args ) => // // It doesnt allow me to put this timer here.

         {
               I will do something here when mouse stops.
         }
         };
试试这个:

        DispatcherTimer timer = new DispatcherTimer();
        timer.Interval = new TimeSpan(0, 0, 0, 1); /* Try to use larger value suitable for you. */
        timer.Tick += (sd, args) => // This is triggered every 1sec.
        {
            Point currentpoint = /* Define current position of mouse here */ null;
            if (lastpoint == currentpoint)
            {
                /* Do work if mouse stays at same */

                /* { //EDIT*/

                /* I haven't tried this, but it might work */
                /* I'm assuming that you will always do new work when mouse stays */
                DispatcherTimer timer2 = new System.Windows.Threading.DispatcherTimer(
                    TimeSpan.FromSeconds(1), /* Tick interval */
                    System.Windows.Threading.DispatcherPriority.Normal, /* Dispatcher priority */ 
                    (o, a) => /* This is called on every tick */
                    {
                        // Your logic goes here 
                        // Also terminate timer2 after work is done.
                        timer2.Stop();
                        timer2 = null;
                    },
                    Application.Current.Dispatcher /* Current dispatcher to run timer on */
                    );
                timer2.Start(); /* Start Timer */

                /* } //EDIT */

            }
            lastpoint = currentpoint;
        };
        timer.Start();

以下是鼠标停止移动1秒后,您可以尝试获取X和Y鼠标位置的信息。不需要双重定时器等。只需为您的窗口注册MouseMove事件,并在每次移动时重置定时器

    private DispatcherTimer timer;

    public MainWindow()
    {
        InitializeComponent();

        timer = new DispatcherTimer();
        timer.Interval = TimeSpan.FromSeconds(1);
        timer.Tick += new EventHandler(timer_Tick);
        timer.Start();
    }

    void timer_Tick(object sender, EventArgs e)
    {
        var position = Mouse.GetPosition(this);
        // position.X
        // position.Y

        timer.Stop(); // you don't want any more ticking. timer will start again when mouse moves.
    }

    private void Window_MouseMove(object sender, MouseEventArgs e)
    {
        // restart timer. will not cause ticks.
        timer.Stop();
        timer.Start();
    } 

你说的红色曲线

    timer.Interval = new TimeSpan(0, 0, 0, 1);
    timer.Tick += (sd, args) => // this is triggered when mouse stops.
    {

        if (a== b)
        {
           I do something here }; // it works until here.

        }


         timer2.Tick += (sd1, args1 ) => // // It doesnt allow me to put this timer here.
         {
               I will do something here when mouse stops.
         }
     };

这有用吗?我重新命名了参数,因为你正在重新定义它们,我以前在这个例子中遇到过红色的曲线

你在哪里申报时间。。?这是你能理解的代码吗。。请将代码的格式也设置得更好.“它不允许我把这个计时器放在这里”的错误是什么?另外,是否需要像对timer那样用分号关闭timer2 lambda表达式?当然,这不是全部代码。我只是想表达我的想法。我宣布了所有的事情。这段代码我的意思是,如果第一个计时器,整个代码都在工作。勾号是唯一的计时器。当我添加第二个计时器时,它不起作用。它只是在第二个计时器的(sd,args)=>部分加上红色下划线。你的格式设置让我大吃一惊。你能再看一遍吗?鼠标停下来时你在做什么???鼠标需要在同一坐标上停留多长时间?这个解决方案更接近我的问题。非常感谢。但是,你能试着用两个计时器吗。这才是我真正需要的。因为,我在你的代码中做了一些其他的事情。在IF部分,我需要使用第二个计时器,或者我需要捕捉另一个鼠标停止。我的意思是,例如:假设我在timer.Tick发生后调用LeftClick()函数。然后我进入一个IF条件。由于我处于IF状态,计时器。滴答不起作用。所以,我必须使用另一个计时器2。勾选开始计数器。你能模拟一下在IF条件下如何使用另一个计时器吗?我还没有想过重命名:)我是从其他地方得到参数的。我复制了一个名为sd1,args1的计时器和一个名为sd1,args1的计时器在IF条件下工作。谢谢..有时候小事情看不到你所看到的:)