如何在.Net Framework 3.5上的c#中区分按钮按下和按钮长按 [英] How to differentiate between button press and button long press in c# on .Net Framework 3.5

查看:279
本文介绍了如何在.Net Framework 3.5上的c#中区分按钮按下和按钮长按的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Visual Studio 2008中用C#创建一个应用程序,在一个按钮中有2个用途,这取决于用户是按下按钮还是长按按钮。



i想加入一个计时器,但这是一个非常笨拙的方式,我在想是否有一个内置的方式为此

I am creating an application in C# in visual Studio 2008 where in a single button will have 2 uses and it will depend on whether the user just presses the button or long presses the button.

i thought of adding a timer but it is a very clumsy way and i was thinking if there was an inbuilt way for this

推荐答案

简单。除了按下按钮,还有较低级别的事件,按下按钮然后按下按钮。您可以检测这些事件之间的时间。更多细节取决于您要使用的UI库和应用程序类型,但代码非常相似。







在大多数应用程序中,我不建议您在向下和向上事件之间进行操作,因为用户不习惯它,这不是常见的UI风格。但有时我们想要退出常见的UI风格,那么你的想法可能适合你。但即便如此,如果你不关心以某种方式解释这个不寻常的功能,也不要认为用户会得到它。



-SA
Easy. Except for button press, there are lower-level events, button down and then button up. You can detect the time between these events. Further detail depends on UI library you want to use and application type, but the code would be very similar.



In most applications, I would not recommend you to operate on timing between down and up events, as the users are not used to it, this is not a common UI style. But sometimes we want to go out of common UI styles, then your idea might work for you. But even then, don't assume the user will get it if you don't care about explaining this unusual feature somehow.

—SA


您可以将事件mouseLeftButtonDown添加到此按钮。并使用此方法检测长按事件



You can add event mouseLeftButtonDown to this button. And detect the long click event using this method

/// <summary>
/// Detect Mouse Long Click
/// </summary>
/// <param name="element">Framework</param>
/// <param name="duration">Set duration of long click</param>
/// <returns>true: Long Click false: Not Long Click</returns>
public static Task<bool> MouseDown(this FrameworkElement element, TimeSpan duration)
{
    DispatcherTimer timer = new DispatcherTimer();
    TaskCompletionSource<bool> task = new TaskCompletionSource<bool>();
    timer.Interval = duration;

    MouseButtonEventHandler touchUpHandler = delegate
    {
        timer.Stop();
        if (task.Task.Status == TaskStatus.Running)
        {
            task.SetResult(false);
        }
    };

    element.PreviewMouseUp += touchUpHandler;

    timer.Tick += delegate
    {
        element.PreviewMouseUp -= touchUpHandler;
        timer.Stop();
        task.SetResult(true);
    };

    timer.Start();
    return task.Task;
}





在你的活动中调用方法btn_MouseLeftButtonDown

例如< br $> b $ b



Call the method in your event btn_MouseLeftButtonDown
For example

private async void btn_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {

        boolean isLongPress = await MouseDown(e.Source as Button,           TimeSpan.FromSeconds(0.4));
       if (isLongPress) {
          // long press
       } else {
        //short press
       }
    }


我这个问题的最短解决方案:



my shortest solution for this problem:

private bool mouseUp = false;
private const int holdButtonDuration = 2000; //milliseconds
private void btnTest_MouseDown(object sender, MouseEventArgs e)
{
    mouseUp = false;
    Stopwatch sw = new Stopwatch();
    sw.Start();
    while (e.Button == MouseButtons.Left && e.Clicks == 1 && (mouseUp == false && sw.ElapsedMilliseconds < holdButtonDuration))
        Application.DoEvents();
    if (sw.ElapsedMilliseconds < holdButtonDuration)
        btnTest_ShortClick(sender, e);
    else
        btnTest_LongClick(sender, e);
}
private void btnTest_MouseUp(object sender, MouseEventArgs e)
{
    mouseUp = true;
}


这篇关于如何在.Net Framework 3.5上的c#中区分按钮按下和按钮长按的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆