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

  • 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
  • Thank you.

    Posted by Legacy on 03/03/2002 12:00am

    Originally posted by: mutter21

    Good!! It's very simple code!!

    Thank you.. *^^*

    Reply
  • Loading, Please Wait ...

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

Top White Papers and Webcasts

  • It's time high-level executives and IT compliance officers recognize and acknowledge the danger of malicious insiders, an increased attack surface and the potential for breaches caused by employee error or negligence. See why there is extra emphasis on insider threats.

  • On-demand Event Event Date: March 27, 2014 Teams need to deliver quality software faster and need integrated agile planning, task tracking, source control, auto deploy with continuous builds and a configurable process to adapt to the way you work. Rational Team Concert and DevOps Services (JazzHub) have everything you need to build great software, integrated seamlessly together right out of the box or available immediately in the cloud. And with the Rational Team Concert Client, you can connect your …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds