CHoverButton - Nice Hover Button with One Bitmap and Tooltip.

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!

Downloads

Download demo project with source - 34 Kb


Comments

  • Thanx very much

    Posted by Richard on 06/16/2014 03: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

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

    Posted by demeter2 on 09/15/2005 10:32pm

    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.

    Reply
  • Dynamic button

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

    Originally posted by: james

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

    Reply
  • ToolTip does NOT appear with MODELESS dialog

    Posted by Legacy on 06/11/2003 12: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,
    Don

    Reply
  • Fix LoadBitmap() function for using on child dialog

    Posted by Legacy on 10/09/2002 12: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));
    //mybitmap.LoadBitmap(bitmapid);

    BITMAP bitmapbits;
    mybitmap.GetBitmap(&bitmapbits);
    m_ButtonSize.cy=bitmapbits.bmHeight;
    m_ButtonSize.cx=bitmapbits.bmWidth/3;
    SetWindowPos( NULL, 0,0, m_ButtonSize.cx,m_ButtonSize.cy,SWP_NOMOVE | SWP_NOOWNERZORDER );
    return TRUE;

    }

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

    Posted by Legacy on 10/03/2002 12: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.

    Reply
  • Bug Fix - Ignore double-click

    Posted by Legacy on 08/13/2002 12: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)
    {
    InitToolTip();
    m_ToolTip.RelayEvent(pMsg);
    // Add this source code!!
    if( pMsg->message == WM_LBUTTONDBLCLK )
    {
    pMsg->message = WM_LBUTTONDOWN;
    }
    // Add this source code!!
    return CButton::PreTranslateMessage(pMsg);
    }

    Reply
  • Yeah !

    Posted by Legacy on 07/31/2002 12: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!

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

    Posted by Legacy on 06/17/2002 12: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.
    
    

    Thanks...

    Reply
  • Its a great help!

    Posted by Legacy on 06/12/2002 12: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,
    Anand

    Reply
  • Loading, Please Wait ...

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

Top White Papers and Webcasts

  • Live Event Date: December 11, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT Market pressures to move more quickly and develop innovative applications are forcing organizations to rethink how they develop and release applications. The combination of public clouds and physical back-end infrastructures are a means to get applications out faster. However, these hybrid solutions complicate DevOps adoption, with application delivery pipelines that span across complex hybrid cloud and non-cloud environments. Check out this …

  • Hundreds of millions of users have adopted public cloud storage solutions to satisfy their Private Online File Sharing and Collaboration (OFS) needs. With new headlines on cloud privacy issues appearing almost daily, the need to explore private alternatives has never been stronger. Join ESG Senior Analyst Terri McClure and Connected Data in this on-demand webinar to take a look at the business drivers behind OFS adoption, how organizations can benefit from on-premise deployments, and emerging private OFS …

Most Popular Programming Stories

More for Developers

RSS Feeds