开发者

How to get MouseDown after a few seconds?

开发者 https://www.devze.com 2023-01-10 02:15 出处:网络
Assume I have a button and I want the following behavior: when I click on the button, it fires up an event - ok, that\'s easy.

Assume I have a button and I want the following behavior:

when I click on the button, it fires up an event - ok, that's easy.

Now, if I click and wait, after a few seconds it suppose to fire up another event. e.g. popup a menu...

开发者_StackOverflow中文版how to do that?


Are you checking the MouseUp event?

Is what you are saying if the user holds down the mouse button for 2 seconds to display a popup menu?

What I would do is on the MouseDown event create a separate thread waiting for the 2 seconds. If the MouseUp event is triggered before it expires then do nothing, else do the event.

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


Look up Timer() and DispatcherTimer()


I would use threading like this

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

it will check a mouse button is pressed and then check again after 2 seconds without stalling the main app thread. I wou't check if the button was pressed the entire time so this may or may not be important to you

Shane


You can run timer for 2 seconds under MouseDown event and on timers tick event check what you need. Afer that you can stop your timer.


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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号