This article demonstrate a double click event handling on
CButton more effectively. Last one week iam continuously searching how to handle double click operation on a
CButton. I found a solution i.e., add
BS_NOTIFY style for that button. ya, its okay, but i also want some customization on that button. So at last found way, ie, use
PreSubclassWindow function of that button.
Normally single click of a button is performed with message map entry
ON_BN_CLICKKED: This notification code is sent when single click operation is performed on a button.
also there is another notification code which is sent when double click operation is performed on them. ie
ON_BN_DOUBLECLICKED: so we need to write handle for handle corresponding events by identifying corresponding notification code. in normal scenario a button double click operation cannot handle. so here iam explained it.
Using the code
The code section is very simple. Just create a MFC dialog based application. Place a button on that dialog.
First we need to create a class that is derived from
In this application iam created ButtonMore.cpp, i.e., derived from
class ButtonMore : public CButton
so next step is to handle
PreSubclassWindow function of button. This function is great one.
One of the lesser-understood handlers is the
<span class="search-highlight">PreSubclassWindow</span> handler. It turns out this is a very nice handler to use for certain effects in controlls or dialogs.
In normal window handling, you will have an
OnCreate handler that is invoked just as the window is created. In normal MFC window handling, you have have the
members of your subclass can be invoked only when the window is mapped into MFC, that is, its handler has been changed to be the
AfxWnd handler. But dialog controls are created long before the subclassing, which takes place on the first
DoDataExchange handler, which happens during the
OnInitDialog processing. This is far too late. I.e.,
PreSubClassWindow is an ideal place to make certain changeous.
This applies only to those styles which have an effect after the window has been created (many styles cannot be changed once the window is created. You can change the style bits, but the window itself is oblivious to these changes). This is useful when the styles you want to change are not part of the styles presented by the dialog editor..So we can add any style using this function. so customizing the control in any way.
DWORD dwExtendedStyle = ::GetClassLong( m_hWnd, GCL_STYLE );
dwExtendedStyle |= ( CS_DBLCLKS );
::SetClassLong( m_hWnd, GCL_STYLE, dwExtendedStyle );
So we acn add any style in
Next step is to create an object of
in the Dialog class. In the
function modify the style of the buttone .
M_Button.ModifyStyle( 0, BS_NOTIFY );