Virtual Developer Workshop: Containerized Development with Docker

Looking around for some nice bitmap hoverbuttons, I just couldn't find what I was looking for. In this case there are a couple of things you can do:

  1. Leave it.
  2. Buy it.
  3. Steal it.
  4. Get it for free.
  5. Start crying
  6. Do it yourself (and possibly share it.)
I'll share!

I found another hoverbutton class that uses SetCapture() and ReleaseCapture(). Using this method gives earlier or later some problems. I can't remember why, but it did to me. So I thought why do it so diffifcult? MS has a nice API to handle hoverstates: _TrackMouseEvent().

So here it, the class. After a long thought i decided to call it: 'CHoverButton'. I used some classes and information found elsewhere on CodeGuru (thanks to the people for that).

How to use it?

  1. In you resource editor create a button and set the 'owner draw' property.
  2. Use classwizard to create a CButton object in your dialog.
  3. Include the "hoverbutton.h" file in your dialog class file.
  4. Rename the 'CButton' object type to 'CHoverButton'.
  5. Create a bitmap with three buttonstates: Up/Down/Hover. The pictures need to be right next to each other.
    The button will calculate it's own size (the width of the bitmap)/3.
    You can use true color bimaps here.
  6. In the OnInitDialog() load the bitmap with mybutton.LoadBitmap(IDC_MYBUTTON);
  7. Set a tooltip with mybutton.SetToolTipText();
  8. Compile and watch.

Have fun!


Download demo project with source - 34 Kb


  • Thanx very much

    Posted by Richard on 06/16/2014 10:29pm

    I got it working in my app just like it works in the downloaded app only I had to change a 'void' in 'LRESULT' , but that's it great I was looking for a while for this , I just began programming c++ for about 2 months now. Now its a little late (about 12 years) but thanx anyway. Greetz Richard

  • When mouse cursor's shape is changed suddenly..in WinXP

    Posted by demeter2 on 09/16/2005 05:32am

    I think that it happens in WindowXP only because many CHoverButtons in Windows 2K there were no problem. When mouse over the button my cursor's shape is changed to the Information graphic suddenly. The solution is like below. In your CHoverButton::CHoverButton() function add next initialization code. m_CursorHand = NULL; It will work well. And thanks to the author for great work.

  • Dynamic button

    Posted by Legacy on 06/19/2003 07:00am

    Originally posted by: james

    How do you create this button dynamically and still have the tooltip work?

  • ToolTip does NOT appear with MODELESS dialog

    Posted by Legacy on 06/11/2003 07:00am

    Originally posted by: Don

    Using a modeless dialog, the tool tip does not appear to work because the PreTranslateMessage() does not get called.

    How can I get around this???

    Thanks in advance,

  • Fix LoadBitmap() function for using on child dialog

    Posted by Legacy on 10/09/2002 07:00am

    Originally posted by: Kanghee Im

    you can find some error if you use the class on child dialog.
    from the second view, the image does not correctly initialized. so you only see the hover image.

    fixed source is below.

    BOOL CHoverButton::LoadBitmap(UINT bitmapid)

    m_bHover = FALSE; // add this code
    mybitmap.Detach(); // thanks C

    //original code-----------------------------------
    mybitmap.Attach(::LoadImage(::AfxGetInstanceHandle(),MAKEINTRESOURCE(bitmapid), IMAGE_BITMAP,0,0,LR_LOADMAP3DCOLORS));

    BITMAP bitmapbits;
    SetWindowPos( NULL, 0,0, m_ButtonSize.cx,m_ButtonSize.cy,SWP_NOMOVE | SWP_NOOWNERZORDER );
    return TRUE;


  • A FIX for the blinking when entering and leaving the button

    Posted by Legacy on 10/03/2002 07:00am

    Originally posted by: Jaisor

    First of all, thanks for the excelent code, saved me days of looking up.

    I noticed an annoying bliking when the mouse enters and leaves the button, it is caused by the erasing on every Invalidate() so I used Invalidate(FALSE) which prevents erasing and it worked like a charm.

  • Bug Fix - Ignore double-click

    Posted by Legacy on 08/13/2002 07:00am

    Originally posted by: Cho Yong Kil

    generally, standard buttons are dont use WM_LBUTTONDBLCLK message. cause we shown effect of button when click fast.

    but CHoverButton is different.

    BOOL CHoverButton::PreTranslateMessage(MSG* pMsg)
    // Add this source code!!
    if( pMsg->message == WM_LBUTTONDBLCLK )
    pMsg->message = WM_LBUTTONDOWN;
    // Add this source code!!
    return CButton::PreTranslateMessage(pMsg);

  • Yeah !

    Posted by Legacy on 07/31/2002 07:00am

    Originally posted by: TheFlashback

    You have no idea how many troubles Set/Release Capture() brings. This code is neat and awesome. _TrackMouseEvent is just freaking splendid!

  • ActiveX Control with "hover" button - help with tooltip test not being displayed...

    Posted by Legacy on 06/17/2002 07:00am

    Originally posted by: Henry Park

    Having trouble displaying tooltip text using "hover" class in an ActiveX control. The "hover" part works fine, but the tooltip text does not appear. If someone can help me, I'll send some code to look at.


  • Its a great help!

    Posted by Legacy on 06/12/2002 07:00am

    Originally posted by: Anand Raval

    Hi Niek,

    Thanks a lot. You've saved my lots of research time! It is really great help to me.

    Thanks again,

  • Loading, Please Wait ...

  • You must have javascript enabled in order to post comments.

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

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date