2010-07-30 106 views
2

假设我有一个按钮,我想要以下行为:几秒钟后如何获取MouseDown?

当我点击按钮时,它激发了一个事件 - 好的,这很容易。

现在,如果我点击并等待,几秒钟后它会启动另一个事件。例如弹出菜单...

该怎么办?

+0

“click and wait”是什么意思?你的意思是你按住鼠标按钮,还是哟意味着你点击一个按钮,它做了什么,然后在几秒钟后发生了其他事情?那么“弹出菜单”是什么意思?上下文菜单? – 2010-07-30 15:26:55

回答

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

你在检查MouseUp事件吗?

如果用户按住鼠标按钮2秒钟以显示弹出式菜单,您说的是什么?

我会做的是在MouseDown事件上创建一个单独的线程,等待2秒钟。如果MouseUp事件在到期前触发,则不执行任何操作,否则请执行事件。

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

很好的解决方法,谢谢 – 2010-07-31 07:42:43

0

查一查定时器()和DispatcherTimer()

0

我会使用线程这样

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

它会检查按下鼠标键,然后在2秒后再次检查,而不会停止主要的应用程序线程。我wou't检查按钮被按下的整个时间所以这可能是也可能不是你

重要巴蒂尔

0

您可以在MouseDown事件和计时器滴答事件运行2秒定时检查的内容需要。你可以停止你的计时器。