Click here to Skip to main content
15,880,651 members
Articles / Programming Languages / C++

Double click handling on a CButton (using PreSubclassWindow)

Rate me:
Please Sign up or sign in to vote.
4.86/5 (5 votes)
24 Oct 2012CPOL2 min read 18.5K   394   8  
Easly handle double click on a CButton by adding an extera style in PreSubclassWindow.

Introduction

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.

Background

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.

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

so next step is to handle PreSubclassWindow function of button. This function is great one.

One of the lesser-understood handlers is the PreSubclassWindow 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.

C++
void ButtonMore::PreSubclassWindow()
{
    CButton::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 .
C++
M_Button.ModifyStyle( 0, BS_NOTIFY );  

License

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



Comments and Discussions

 
-- There are no messages in this forum --