如何在几秒钟后获得MouseDown?

时间:2010-07-30 15:23:07

标签: wpf

假设我有一个按钮,我想要以下行为:

当我点击按钮时,它会启动一个事件 - 好的,这很容易。

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

怎么做?

5 个答案:

答案 0 :(得分: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
    }
}

答案 1 :(得分:0)

查找Timer()和DispatcherTimer()

答案 2 :(得分: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秒后再次检查,而不会停止主应用程序线程。我不会检查按钮是否一直按下,所以这对你来说可能是重要的,也可能不重要

沙恩

答案 3 :(得分:0)

你可以在MouseDown事件下运行计时器2秒钟,并在计时器上勾选事件检查你需要什么。你可以停止计时器。

答案 4 :(得分: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
        }
    }