Wpf 几秒钟后如何将鼠标向下移动?

Wpf 几秒钟后如何将鼠标向下移动?,wpf,Wpf,假设我有一个按钮,我想要以下行为: 当我点击按钮时,它会触发一个事件-好的,这很简单 现在,如果我点击并等待,几秒钟后它会触发另一个事件。e、 弹出菜单 怎么做 您正在检查MouseUp事件吗 如果用户按住鼠标按钮2秒钟以显示弹出菜单,您的意思是什么 我要做的是在MouseDown事件上创建一个单独的线程,等待2秒钟。如果MouseUp事件在到期之前触发,则不执行任何操作,否则执行该事件 // This event will be used for tracking if the MouseUp

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

当我点击按钮时,它会触发一个事件-好的,这很简单

现在,如果我点击并等待,几秒钟后它会触发另一个事件。e、 弹出菜单


怎么做

您正在检查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
    }
}

查找Timer()和dispatchermer()

我会像这样使用线程

    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秒钟后再次检查,而不会暂停主应用程序线程。我不会检查按钮是否一直被按下,所以这对你来说可能重要,也可能不重要


Shane

您可以在MouseDown事件下运行计时器2秒钟,并在timers tick事件上检查所需内容。之后,您可以停止计时器。

单击并等待”是什么意思?你是说你按住鼠标键,还是说你点击一个按钮,它会做一些事情,几秒钟后,其他事情就会发生?“弹出菜单”是什么意思?上下文菜单?
    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
        }
    }