简体   繁体   English

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

[英]getting the moment of unchanging cursor position X and Y in C# WPF

I want to get the moment of the unchanging cursor position. 我想获得光标位置不变的时刻。 I mean when mouse stops, I want to do something. 我的意思是当鼠标停下时,我想做点什么。 But I will do this many times.I used dispatcher timer. 但是我会做很多次,我使用了调度计时器。 But It doesnt allow me to do same thing inside of that. 但这不允许我在其中做同样的事情。 Example: 例:

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

Try this : 尝试这个 :

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

Here's what you can try to get X and Y mouse position after the mouse has stopped moving for 1 sec. 这是在鼠标停止移动1秒钟后可以尝试获得X和Y鼠标位置的方法。 No need for double timers etc. Just register the MouseMove event for your window and reset the timer everytime it moves. 不需要双重计时器等。只需为窗口注册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();
    } 

Red squiggly lines you say? 你说的是红色的波浪线?

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

Does this help? 这有帮助吗? I renamed the arguments, since you're redefining them and I have run into red squiggly lines in this case before... 我重命名了参数,因为您正在重新定义它们,并且在这种情况下,在此之前,我遇到了弯曲的红色线条...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM