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

发布于 2024-12-22 01:48:33 字数 546 浏览 2 评论 0原文

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

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

离笑几人歌 2024-12-29 01:48:33

试试这个:

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

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();
你没皮卡萌 2024-12-29 01:48:33

您可以尝试在鼠标停止移动 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();
    } 

Here's what you can try to get X and Y mouse position after the mouse has stopped moving for 1 sec. No need for double timers etc. Just register the MouseMove event for your window and reset the timer everytime it moves.

    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();
    } 
挽你眉间 2024-12-29 01:48:33

你说的是红色的波浪线吗?

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

这有帮助吗?我重命名了参数,因为您正在重新定义它们,并且我之前在这种情况下遇到过红色波浪线......

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文