Responsive CBitmapButton Class

I used the MFC CBitmapButton class in a project and was annoyed of the fact that it wasn't as responsive as normal text buttons. It seems to eat double clicks if you click too fast on it. I tried Micrsofts samples of bitmap buttons and experienced the same problem. I think the problem appears for all buttons that have the attribute "owner draw" set but I haven't confirmed this.

I don't know if this is the smartest way of solving the problem but it was quite easy once I figured it out. I could not find any solution to this problem on codeguru and not in MS' knowledgebase.

The following instructions assumes you have basic knowledge on how to use the ClassWizard in DevStudio.

Using the CFixedButton Class

  1. Add the files FixedButton.cpp and FixedButton.h to your project (Project/Add To Project/Files).

  2. Copy the bitmap resources to your project. They are "MOVEUPD", "MOVEUPF", and "MOVEUPU".

  3. Note that these are string resource IDs and not numeric identifiers.

  4. Create a new dialog resource.

  5. Give it the ID IDD_STEPBYSTEP.

  6. Press CTRL+W to bring up the dialog box "Adding a class".

  7. Select "Create a new class" and then click on OK.

  8. Enter CStepByStep in the Name edit box.

  9. Click on OK.

  10. Click on OK to close the "MFC Class Wizard" dialog box.

  11. Use the Controls toolbar in DevStudio to add a normal pushbutton to the newly created dialog box.

  12. Right-click on the button and give it the ID IDC_STEPBUTTON.

  13. Check the check box Owner draw on the Styles tab.

  14. Close the properties dialog.

  15. Open the file StepByStep.h.

  16. Locate the following line
      CStepByStep(CWnd* pParent = NULL);   // standard constructor
    
    Now insert this line after it.
      CFixedButton  m_btnStep;
    

  17. Open the file StepByStep.cpp.

  18. Below the line...
      #include "stdafx.h"
    
    ...add the following two lines:
      #include "resource.h"
      #include "FixedButton.h"
    

  19. Still in StepByStep.cpp, add the following in CStepByStep::CStepByStep():
    if (FALSE == m_btnStep.LoadBitmaps(_T("MOVEUPU"), 
                                       _T("MOVEUPD"), 
                                       _T("MOVEUPF") ) )
    {
      TRACE0("Failed to load bitmaps for buttons\n");
      AfxThrowResourceException();
    }
    

    Use ClassWizard to add the function BOOL CStepByStep::OnInitDialog() (CTRL+W, select CStepByStep in the drop down list Class name, browse to WM_INITDIALOG in the Messages list, click on Add Function).

  20. In CStepByStep::OnInitDialog(), after the base class call, add the following:
    VERIFY( m_btnStep.SubclassDlgItem( IDC_STEPBUTTON, this ) );
    m_btnStep.SizeToContent();
    

  21. To use it in, for example, FixedButtonDlg.cpp, add the include:
      #include "StepByStep.h"
    

  22. Use the ClassWizard to override, let's say the WM_LBUTTONDOWN message.

  23. Then modify the message handler to look like this:
    void CFixedButtonDlg::OnLButtonDown( UINT nFlags, CPoint point )
    {
      CStepByStep dlg;
      dlg.DoModal();
      CDialog::OnLButtonDown( nFlags, point );
    }
    

  24. Compile and test. That's it!
Please post any comments and/or improvements you might have.

Downloads

Download demo project - 11 Kb


Comments

  • A better way (?)

    Posted by Legacy on 02/18/2002 12:00am

    Originally posted by: phil

    Hi!
    I used this - and I think it works better, if I understand what your problem is...

    I've added a PreTranslateMessage handler to my owner-draw button class, and it looks like this:

    <code>
    BOOL CMyButton::PreTranslateMessage(MSG* pMsg) {
    if ( pMsg->message==WM_LBUTTONDBLCLK )
    pMsg->message=WM_LBUTTONDOWN;
    if ( pMsg->message==WM_MBUTTONDBLCLK )
    pMsg->message=WM_MBUTTONDOWN;
    if ( pMsg->message==WM_RBUTTONDBLCLK )
    pMsg->message=WM_RBUTTONDOWN;
    return CButton::PreTranslateMessage(pMsg);
    }
    </code>

    Now all doubleclicks are translated to 2 clicks by the button.

    Do you like it? :-)

    Reply
  • excellent

    Posted by Legacy on 11/16/2001 12:00am

    Originally posted by: charlie

    This is excellent. But just some suggestion, why not use Autoload function.

    • I don't know

      Posted by galter on 01/08/2008 05:15am

      I don't know

      Reply
    Reply
  • re:Responsive CBitmapButton Class

    Posted by Legacy on 11/14/2001 12:00am

    Originally posted by: Wes Bishop

    Robert,

    Thanks so much for your input of this. It was exactly what I was looking for.

    Wes Bishop
    Manager of Advanced Technical Projects
    CNN
    Atlanta, GA

    Reply
  • responsive button

    Posted by Legacy on 07/26/2001 12:00am

    Originally posted by: Amitabh Deshpande

    Robert,
    Thats a neat trick that works quite well for me. I modified my code to use that idea but your class is also very convenient.

    Thanks for posting.

    Amitabh

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: October 29, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you interested in building a cognitive application using the power of IBM Watson? Need a platform that provides speed and ease for rapidly deploying this application? Join Chris Madison, Watson Solution Architect, as he walks through the process of building a Watson powered application on IBM Bluemix. Chris will talk about the new Watson Services just released on IBM bluemix, but more importantly he will do a step by step cognitive …

  • Protecting business operations means shifting the priorities around availability from disaster recovery to business continuity. Enterprises are shifting their focus from recovery from a disaster to preventing the disaster in the first place. With this change in mindset, disaster recovery is no longer the first line of defense; the organizations with a smarter business continuity practice are less impacted when disasters strike. This SmartSelect will provide insight to help guide your enterprise toward better …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds