CHoverButton – Nice Hover Button with One Bitmap and Tooltip.

CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.

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

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read