Distinguish single click from double click C++

double-clickmfcmousevisual c++

I have an application in which double clicking over an image view area changes the layout of the image view. Also on single click a dot will be placed on the image.
My problem is, both functionality is working when double clicked.

Of course I know that, when a double click occurs the control first goes to LButtonDown. I don't want the dot functionality to work when double click occurs. I have been working around this for more than a week. Please help.

Best Answer

The easiest way to solve this is to build a finite-state machine for handling mouse clicks. Basically, this will be a singleton object, which takes input from the mouse click events you're currently using. It's output will be SingleClickDetected, DoubleClickDetected, .... Red arrows indicate events which you are reporting to the rest of your application. Parentheses indicate what event you are reporting.

FSM

Of course, this state machine will have to be modified if you have to deal directly with MouseDown and MouseUp events, instead of MouseClick events. It will be slightly larger, but the idea is basically the same.

EDIT: From the comments, it looks like Windows doesn't cleanly report single- vs double-clicks, and you need to separate them. A state-machine for this scenario: FSM2

This is probably overkill for what you're trying to do, especially since most, if not all GUI-based programs in the history of everything have never ever used a double-click drag. It does show the basic idea, and shows how you can extend your state machine to handle different types of button clicks. Furthermore, if you wanted to, you could handle double-right-clicks, a drag that involves both left and right buttons, or any other scenario you could think of and incorporate into your UI.

Related Topic