Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detecting a single mouse click in MFC

In MFC a double-mouse click event triggers the following sequence of messages

  • WM_LBUTTONDOWN
  • WM_LBUTTONUP
  • WM_LBUTTONDBCLK
  • WM_LBUTTONUP

So responding to the WM_LBUTTONDBCLK message allows you to detect a double-click. But if I just want to detect a single-click how to I distinguish it?

But just looking at the WM_LBUTTONUP message isn't enough as it could be a single-click or it could be the first click of a double-click.

How can I successfully identify just a single-click?

like image 383
Matt Warren Avatar asked Feb 03 '09 16:02

Matt Warren


1 Answers

(Please allow me to call these events Mouse Up and Mouse Down. My MFC is a little rusty. And there's this stuff called .NET who's been messing up my terminology lately ;-)

Short story: You don't simply want to know about Mouse Click. You need more.

Long story:

Although this is counter-intuitive, it appears that simply wanting a mouse-click is fairly uncommon. Most often, you'll want to perform some processing on Mouse Down and do some further processing on Mouse Up. The trick is that simply tracking Mouse Up messages is not enough: Mouse Down may not have happened in your window. Do you consider it a valid click then? Especially considering that the Mouse Down processing (such as selecting an item) did not occur.

Going further up the reasoning, you should not rely on receiving a Mouse Up after you processed Mouse Down: User may have moved the mouse and released the button somewhere else (think drag'n'drop), in which case, you don't receive the MouseUp event... unless you capture the mouse on MouseDown to make sure you get mouse event up to Mouse Up even if the mouse left your window.

All in all, you end up tracking Mouse Down, capture the mouse and when you receive Mouse Up, just check if you own the capture. If not, the mouse was either double-clicked (no 2nd mouse down) or Mouse Down happened somewhere else hence you most likely don't care about this Mouse Up.

In conclusion: There's no MouseClick message simply because you wouldn't go very far with it: You need to handle more messages and implement more mechanics anyway.

Oh! And if your dealing with an existing control which already handles all this items and selection stuff, such as a listview, chances are it provides with a similar custom notification such as Item Activate or Item Selection Changed.

like image 141
Serge Wautier Avatar answered Nov 15 '22 12:11

Serge Wautier