2011-12-20 6 views
0

変更しないカーソル位置の瞬間を取得したい。私はマウスが止まると何かしたいと思っています。しかし、私はこれを何度もやります。私はディスパッチャータイマーを使いました。しかし、それは私の中で同じことをすることはできません。例:C#のカーソル位置XとYの変更の瞬間を得るWPF

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

ここで、あなたはtimer2 ..を宣言していますか? – MethodMan

+0

「このタイマーはここに入れることができません」というエラーは何ですか?いう? また、タイマのようにセミコロンでtimer2ラムダ式を閉じる必要がありますか? – M3NTA7

+0

もちろん、これはコード全体ではありません。私はちょうどその考えを伝えようとした。私はすべてのものを宣言した。このコードは、最初のtimer timer.Tickが唯一のタイマーであれば、コード全体が機能していることを意味します。私は2番目のタイマーを追加すると、それは動作しません。 2番目のタイマーの(sd、args)=>部分に赤い下線を付けるだけです。 – Samet

答えて

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

この解決策は私の問題に近いです。ありがとうございました。しかし、あなたは2タイマーを使用しようとすることはできますか?これは私が実際に必要なものです。なぜなら、私はあなたのコードでIF部分に何か他のことをするからです。 IFの部分では、私は2番目のタイマーを使用する必要がある、または私は別のマウスの停止をキャッチする必要があります。つまり、たとえば:timer.Tickの後にLeftClick()関数を呼び出すことを想像してみましょう。そして、私は1つのIF条件に入ります。 IF状態なので、timer.Tickは機能しません。だから、別のtimer2.Tickを使用してカウンタを開始する必要があります。 IF条件で別のタイマーを使用する方法をシミュレートできますか? – Samet

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

これは役に立ちますか?あなたがそれらを再定義しているので、私は議論の名前を変更しました。この前に赤い波打ち線に遭遇しました...

+0

私は名前を変更することを考えていませんでした:)私はどこか他の場所からargsを取得していました。私は名前sd1、args1とsd1を持つ新しいタイマーでそれを複製し、args1はIF条件で働いた。ありがとうございました。時には小さなものが見えないことがあります:) – Samet

関連する問題