2012-04-11 3 views
-1

私のアプリケーションにはいくつかのウィンドウがあります。マウスが特定の時間(例えば半秒)の間にすべて私の窓の外になると、特定の動作を実行したい。私のアプリケーションには複数のウィンドウがあり、マウスが指定時間外にあるときに何かしたいのですが

単一のウィンドウでは、MouseLeaveイベントでタイマーを開始し、その時間をMouseEnterイベントで削除しますが、複数のウィンドウでこれを実装するにはどうすればいいですか?

答えて

0

すべてのWindowsとページには、アプリケーションへのアクセス権があります。遅れが組み込まれているBackGroundWorkerを開始してキャンセルするだけです。作業者が完了したら、あなたは何をしますか?私はこれを2ページでテストしました。

は、すべてのページにMouseEnterイベントとMouseLeaveを登録

private void MainWindowsMouseLeave(object sender, MouseEventArgs e) 
    { 
     // MessageBox.Show("MouseLeave"); 
     tbMouseEnterLeave.Text = "MouseLeave"; 
     if (App.BackgroundWorkerApp.IsBusy) App.BackgroundWorkerApp.CancelAsync(); 
     else 
     { 
      Thread.Sleep(10); 
      if (App.BackgroundWorkerApp.IsBusy)App.BackgroundWorkerApp.CancelAsync(); 
     } 
     if (!App.BackgroundWorkerApp.IsBusy) App.BackgroundWorkerApp.RunWorkerAsync(); 
    } 

    private void MainWindowsMouseEnter(object sender, MouseEventArgs e) 
    { 
     tbMouseEnterLeave.Text = "MouseEnter"; 
     App.BackgroundWorkerApp.CancelAsync(); 
    } 


public partial class App : Application 
{ 
    private static System.ComponentModel.BackgroundWorker backgroundWorkerApp = new BackgroundWorker(); 

    public App() 
    { 
     backgroundWorkerApp.WorkerSupportsCancellation = true; 
     backgroundWorkerApp.DoWork += 
      new DoWorkEventHandler(backgroundWorkerApp_DoWork); 
     backgroundWorkerApp.RunWorkerCompleted += 
      new RunWorkerCompletedEventHandler(
     backgroundWorkerApp_RunWorkerCompleted); 
    } 

    public static System.ComponentModel.BackgroundWorker BackgroundWorkerApp { get { return backgroundWorkerApp; } } 

    private void backgroundWorkerApp_DoWork(object sender, 
     DoWorkEventArgs e) 
    { 
     // Get the BackgroundWorker that raised this event. 
     BackgroundWorker worker = sender as BackgroundWorker; 
     e.Result = ComputeApp(worker, e); 
    } 

    // This event handler deals with the results of the 
    // background operation. 
    private void backgroundWorkerApp_RunWorkerCompleted(
     object sender, RunWorkerCompletedEventArgs e) 
    { 
     // First, handle the case where an exception was thrown. 
     if (e.Error != null) 
     { 
      MessageBox.Show(e.Error.Message); 
     } 
     else if (e.Cancelled) 
     { 
      // Next, handle the case where the user canceled 
      // the operation. 
      // Note that due to a race condition in 
      // the DoWork event handler, the Cancelled 
      // flag may not have been set, even though 
      // CancelAsync was called. 
      // MessageBox.Show("Cancel"); 
     } 
     else 
     { 
      // Finally, handle the case where the operation 
      // succeeded. 
      // this where you do that thing you want to do 
      MessageBox.Show("Complete");    
     } 
    } 

    string ComputeApp(BackgroundWorker worker, DoWorkEventArgs e) 
    { 

     // Abort the operation if the user has canceled. 
     // Note that a call to CancelAsync may have set 
     // CancellationPending to true just after the 
     // last invocation of this method exits, so this 
     // code will not have the opportunity to set the 
     // DoWorkEventArgs.Cancel flag to true. This means 
     // that RunWorkerCompletedEventArgs.Cancelled will 
     // not be set to true in your RunWorkerCompleted 
     // event handler. This is a race condition. 

     if (worker.CancellationPending) 
     { 
      e.Cancel = true; 
      return "cancelled"; 
     } 
     for (int i=0; i < 10; i++) 
     { 
      Thread.Sleep(100); 
      if (worker.CancellationPending) 
      { 
       e.Cancel = true; 
       return "cancelled"; 
      } 
     } 
     return "complete"; 
    } 

} 
関連する問題