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

时间:2011-12-20 20:28:02

标签: c# wpf mouse

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

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

3 个答案:

答案 0 :(得分:1)

试试这个:

        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 :(得分:0)

在鼠标停止移动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();
    } 

答案 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 += (sd1, args1 ) => // // It doesnt allow me to put this timer here.
         {
               I will do something here when mouse stops.
         }
     };

这有帮助吗?我重新命名了这些论点,因为你正在重新定义它们,在这种情况下我遇到了红色的波浪线...