6

I have a FrameworkElement and I want to perform action A when the user single clicks, and action B when the user double clicks.

Due to the way events are delivered, I always get a single click event which begins action A. After looking around, I found an interesting technique here using a timer to delay the handling of the clicks. However, this example hardcodes the timer to 300 milliseconds, but I would prefer to use the user's "Double-click speed" setting Control Panel's Mouse Properties dialog.

What's the wpf/C# API for getting that value from the system?

Community
  • 1
  • 1
Shezan Baig
  • 1,474
  • 1
  • 14
  • 16

4 Answers4

10

You can find the time here: System.Windows.Forms.SystemInformation.DoubleClickTime

You can actually see a full implementation of what you are trying to achieve here:

WPF: Button single click + double click issue

Community
  • 1
  • 1
ColinE
  • 68,894
  • 15
  • 164
  • 232
  • good, but better to use SystemInformation.DoubleClickTime and avoid the winforms reference in WPF – Muad'Dib Jan 27 '11 at 15:59
  • Which SystemInformation class are you referring to? what is its namespace? – ColinE Jan 27 '11 at 16:00
  • @Muad'Dib it looks like SystemInformation is part of winforms – Shezan Baig Jan 27 '11 at 16:18
  • @ColinE Thanks! This gives exactly what I want (not concerned about the winforms reference :).. – Shezan Baig Jan 27 '11 at 16:19
  • 1
    @Shezan Baig I was thinking of the SystemParamaters class, which is NOT part of winforms. Turns out, there is not double-click info in there. I've used it for things like mouse move threshold for drag n drop. – Muad'Dib Jan 27 '11 at 18:16
  • 4
    To avoid the reference to Winforms, you could just do the same thing it does behind the scenes and import the [GetDoubleClickTime](http://msdn.microsoft.com/en-us/library/windows/desktop/ms646258(v=vs.85).aspx) function in user32.dll: `[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]public static extern int GetDoubleClickTime();` – Kent Boogaart Nov 26 '13 at 05:47
  • @KentBoogaart You should post that as answer, because it doesn't use Winforms (the author requested WPF) unlike the other answers. – Lennart Jan 12 '15 at 08:49
6

If you don't want reference System.Windows.Forms assembly, you can try this:

    [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
    public static extern int GetDoubleClickTime();
dexiang
  • 1,345
  • 16
  • 23
4

This should work SystemInformation.DoubleClickTime

bartosz.lipinski
  • 2,627
  • 2
  • 21
  • 34
0

I have a FrameworkElement and I want to perform action A when the user single clicks, and action B when the user double clicks.

If you're handling an event that provides a MouseButtonEventArgs, such as UIElement.MouseLeftButtonDown, you can access MouseButtonEventArgs.ClickCount to determine if multiple clicks have happened within the double-click time of each other. Since FrameworkElement derives from UIElement this should be possible in your case.

No need to use DllImport or WinForms, as you don't really need to know the double-click time to accomplish your main goal.

Edit add: I discovered that this only really works for the ButtonDown events (left or right). I'm not sure why they didn't implement this properly for the ButtonUp events, but it should suffice to catch both events, and keep a member variable of the ClickCount from the ButtonDown that you can check when receiving ButtonUp.

John Thoits
  • 355
  • 1
  • 12