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

本文关键字:时刻 位置 WPF 获取 光标 | 更新日期: 2023-09-27 18:18:38

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

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

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

试试这个:

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

这有帮助吗?我重命名了参数,因为你要重新定义它们,我之前在这个例子中遇到过红色的弯弯曲曲的线…