MFC: CButton and how ON_BN_DOUBLECLICKED message map works

Microsoft Foundation Classes (MFC) library provides CButton which, according to MSDN, can be used for creating a check box, a radio button, and a pushbutton.

There are two events CButton supports: (i) ON_BN_CLICKED (single click), (ii) ON_BN_DOUBLECLICKED (double click). Let’s assume you provide a meaningful implementation for a single click and do nothing in the event handler for the double click (empty method’s body). The result if a single click is obvious, but what will happen if user double clicks the check box (or the other CButton representation)? There are two possible answers (assuming the check box was unchecked initially):

  1. Check box will be checked, and then unchecked – a simulation of a double (single) click
  2. Check box will be checked only – one action takes place now

The correct answer is 2. When you analyze the behavior in details (using a debugger or logging) you will learn that when you perform a double click two event handlers are executed in the following order: ON_BN_CLICKED, and only then ON_BN_DOUBLECLICKED. So the former makes the check box checked while the latter does nothing (because of no implementation provided).

Let’s assume you really want a double click to be handled as if user clicked the check box twice (two single clicks). The easiest way to achieve that is to supply both event handlers with the same implementation. Perhaps this is not the best solution – what is supposed to be done is done, but user is getting an impression the check box works slightly different that they are used to. However, all in all it works icon_smile-7286641

Any better workaround?

Previous Post
Next Post