Click here to Skip to main content
13,735,244 members
Click here to Skip to main content
Add your own
alternative version

Tagged as


8 bookmarked
Posted 24 Oct 2012
Licenced CPOL

Double click handling on a CButton (using PreSubclassWindow)

Rate this:
Please Sign up or sign in to vote.
Easly handle double click on a CButton by adding an extera style in PreSubclassWindow.


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 CButton

In this application iam created ButtonMore.cpp, i.e., derived from CButton.

class ButtonMore : public CButton
    // Construction
    // Attributes
    // Operations
    // Overrides
    // ClassWizard generated virtual function overrides
    // Implementation
    virtual ~ButtonMore();
    // Generated message map functions
    // NOTE - the ClassWizard will add and remove member functions here.
    void PreSubclassWindow();

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 PreCreateWindow function.

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.

void ButtonMore::PreSubclassWindow()
    DWORD dwExtendedStyle = ::GetClassLong( m_hWnd, GCL_STYLE );
    dwExtendedStyle |= ( CS_DBLCLKS );
    ::SetClassLong( m_hWnd, GCL_STYLE, dwExtendedStyle );
}<pre><pre />
So we acn add any style in PreSubClassWindow().  Next step is to create an object of ButtonMore in the Dialog class. In the OnInitDialog() function modify the style of the buttone .
M_Button.ModifyStyle( 0, BS_NOTIFY );  


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Authors

You may also be interested in...


Comments and Discussions

-- There are no messages in this forum --
Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web05-2016 | 2.8.180920.1 | Last Updated 24 Oct 2012
Article Copyright 2012 by Retired Author, Retired Author
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid