开发者

How can I get repeated double-clicks without WPF changing some to triple-clicks?

开发者 https://www.devze.com 2023-02-16 01:56 出处:网络
I\'ve got a listbox in my WPF app, and when the user double-clicks on an item, I change the contents of the list (something like directory navigation). In some cases, it makes sense to double-click, a

I've got a listbox in my WPF app, and when the user double-clicks on an item, I change the contents of the list (something like directory navigation). In some cases, it makes sense to double-click, and then immediately double-click again, because you know what's going to appear in that same spot. You would expect that you could do that by just clicking four times in rapid succession (which ought to mean "two double-clicks").

But that doesn't work. If you click rapid-fire enough, it's not until the fifth or sixth click before WPF will send another double-click event!

It looks like WPF has built-in support for triple-clicks, and sometimes even more, if you click fast enough. I ran a quick example to confirm this:

<ListBox PreviewMouseLeftButtonDown="ListBox_PreviewMouseLeftButtonDown"/>

private void ListBox_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
    Trace.WriteLine("ClickCount = " + e.ClickCount);
}

When I ran it, and clicked rapid-fire, I got output like this:

ClickCount = 1
ClickCount = 2
ClickCount = 3
ClickCount = 1
ClickCount = 2
ClickCount = 3
ClickCount = 4
ClickCount = 1
ClickCount = 2
ClickCount = 3

Sometimes it counted up to 3 and then reset; sometimes it counted up to 4 before it reset. I couldn't get it to go up to 5.

But then I went into Control Panel > Mouse, and changed my double-click speed to the slowest possible setting. Then I was reliably able to get it to count up to 10 before it reset, and up to 11 or 12 if I was really booking it. So apparently it's all about how fast you can click before your system-configured "double-click" interval. If you click more times before that interval has elapsed, you get a "triple-click" or a "quadruple-click" or more.

But I don't want triple-clicks or quadruple-clicks -- especially not if they're impossible to get with certain double-click configurations. I want every other click to be a double-click event (as long as they're in rapid succession, the mouse hasn't开发者_运维知识库 moved more than the threshold amount, etc.)

Is there any way I can get WPF to give me normal double-click events (an event for every other click) when I'm clicking rapid-fire?


Use a couple of Instance Variables: _LastMouseDownTime and _LastMouseDownWasDoubleClick:

    private int _LastMouseDownTime;
    private bool _LastMouseDownWasDoubleClick;

    private void ListBox_PreviewMouseDown(object sender, MouseButtonEventArgs e) {
        if(e.Timestamp - _LastMouseDownTime <= System.Windows.Forms.SystemInformation.DoubleClickTime && !_LastMouseDownWasDoubleClick) {
            Debug.Print("DoubleClick - StupidWorthlessClickCount:{0}", e.ClickCount);
            _LastMouseDownWasDoubleClick = true;

            //Do some double clicking goodness
        } else {
            Debug.Print("FirstClick - StupidWorthlessClickCount:{0}", e.ClickCount);
            _LastMouseDownWasDoubleClick = false;

            //Do some single clicking goodness
        }
        _LastMouseDownTime = e.Timestamp;
        base.OnMouseDown(e);
    }

I know this is super hacky and not very flexible, but it should get you what you need. I am trying to get WPF RichTextBox to respond to those 4th, 5th, etc. clicks like they were the 1st, 2nd, etc. to make selection work more naturally, but this may prove to be impossible! :)

Hope this helps.


ClickCount shows how many times user had clicked within the system double click time. So if you need to respond only on double clicks:

private void ListBox_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
    if (e.ClickCount % 2 == 1)
        return;
    Trace.WriteLine(e.ClickCount + " " + DateTime.Now);
}

Output:

//2 clicks:
2 10.03.2011 16:05:17
//4 clicks:
2 10.03.2011 16:05:20
4 10.03.2011 16:05:20
//3 clicks:
2 10.03.2011 16:05:26
0

精彩评论

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