2010-07-30 8 views
2

Supposons que j'ai un bouton et je veux le comportement suivant:Comment faire pour MouseDown après quelques secondes?

lorsque je clique sur le bouton, il déclenche un événement - ok, c'est facile. Maintenant, si je clique et attend, après quelques secondes, il suppose de déclencher un autre événement. par exemple. popup un menu ...

comment faire cela?

+0

Que signifie "cliquer et attendre"? Voulez-vous dire que vous maintenez le bouton de la souris, ou que vous voulez dire que vous cliquez sur un bouton, cela fait quelque chose, et après quelques secondes, il se passe quelque chose d'autre? Et que signifie "popup menu"? Un menu contextuel? –

Répondre

0
DispatcherTimer PopupTimer = new DispatcherTimer(); 

    PopupTimer.Tick += new EventHandler(PopupTimerTick); 
    PopupTimer.Interval = new TimeSpan(0, 0, 0, 0,5); 


    private void PopupTimerTick(object sender, EventArgs e) 
    { 
     if (Mouse.LeftButton == MouseButtonState.Pressed) 
     { 
      // If still pressed showing popup 
      ((Storyboard)Resources["ShowPopup"]).Begin(); 
      PopupTimer.Stop(); 
     } 
    } 

    private void ImageOnMouseDown(object sender, MouseButtonEventArgs e) 
    { 
     PopupTimer.Start(); 
     e.Handled = true; 
    } 

    private void ImageOnMouseUp(object sender, MouseButtonEventArgs e) 
    { 
     e.Handled = true; 

     if (Popup.IsOpen == false) 
     { 
      ((Storyboard)Resources["ShowPopup"]).Stop(); 
      // Here the operation that works on the click 
     } 
    } 
3

Vérifiez-vous l'événement MouseUp?

Est-ce que ce que vous dites si l'utilisateur appuie sur le bouton de la souris pendant 2 secondes pour afficher un menu contextuel? Ce que je ferais sur l'événement MouseDown créer un thread séparé en attente pour les 2 secondes

Si l'événement MouseUp est déclenché avant son expiration, ne faites rien, sinon faites l'événement.

// This event will be used for tracking if the MouseUp has been received 
private System.Threading.AutoResetEvent _stopTrigger; 

private void OnMouseDown(object sender, MouseButtonEventArgs e) 
{ 
    if (this._stopTrigger == null) 
    { 
     this._stopTrigger = new System.Threading.AutoResetEvent(false); 
    } 

    Action popupProcess = new Action(this.ShowPopupAfterTime); 

    // Make the Popup process on a separate thread 
    popupProcess.BeginInvoke(null, null); 

} 

private void OnMouseUp(object sender, MouseButtonEventArgs e) 
{ 
    if (this._stopTrigger != null) 
    { 
     // Sends the signal to the ShowPopupAfterTime that it should NOT display the pop up 
     // IIt will make WaitOne return true and not go into the if statement 
     this._stopTrigger.Set(); 
    } 
} 

private void ShowPopupAfterTime() 
{ 
    // Will enter the if after 2 seconds 
    if (!this._stopTrigger.WaitOne(2000)) 
    { 
     // This means it has NOT be trigged thus I can display the popup 



     // DISPLAY POPUP 
     // DON"T FORGET you are on a different thread here, NOT UI thread. You will have to use the Dispatcher to get back 
     // to the UI thread to display the popup 
    } 
} 
+0

Belle solution, merci –

0

J'utiliser le filetage comme celui-ci

private void mousedownEvent(object sender, MouseButtonEventArgs e) 
    { 
     //Fire off a thread which will do the waiting in the background 
     new Thread(delegate() 
      { 
       //Wait for 2 seconds 
       Thread.Sleep(2000); 

       //dump a dowork() method onto the main thread 
       this.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal, new Action(delegate() 
       { 
        doWork(sender); 
       })); 
       return; 
      }).Start(); 
    } 

    private void doWork(object sender) 
    { 
     //if the button is still pressed 
     if ((sender as UIElement).IsMouseOver && Mouse.LeftButton == MouseButtonState.Pressed) 
     { 
      //continue here 
     } 
    } 

il vérifiera un bouton de la souris enfoncé, puis vérifiez à nouveau au bout de 2 secondes sans décrocher le fil d'application principal. Je wou't vérifier si le bouton a été pressé tout le temps si cela peut ou peut ne pas être important pour vous

Shane

0

Vous pouvez exécuter la minuterie pendant 2 secondes en cas MouseDown et minuteries cocher événement vérifier ce que vous avoir besoin. Après que vous pouvez arrêter votre minuterie.

Questions connexes