Click here to Skip to main content
15,886,518 members
Articles / Desktop Programming / MFC

CHoverButton - A Simple hoverbutton with One Bitmap and a tooltip

Rate me:
Please Sign up or sign in to vote.
4.81/5 (25 votes)
12 Oct 2000 419.9K   6.2K   103   60
A simple drop-in class that provides a 'hot' look button using the _TrackMouseEvent function

Sample Image - hoverbutton.gif

Introduction

Looking around for some nice bitmap hover buttons, 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 gave me some problems. I can't remember why, but it did to me. So I thought why do it so difficult? MS has a nice API to handle hover states: _TrackMouseEvent().

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

How to Use It?

  1. In your 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 its own size = (the width of the bitmap)/3.
    You can use true color bitmaps here. See below for an example image.

  6. In the OnInitDialog(), load the bitmap with mybutton.LoadBitmap(IDC_MYBUTTON);
  7. Set a tooltip with mybutton.SetToolTipText();
  8. Compile and enjoy.

Have fun!

License

This article has no explicit license attached to it, but may contain usage terms in the article text or the download files themselves. If in doubt, please contact the author via the discussion board below.

A list of licenses authors might use can be found here.


Written By
Web Developer
Netherlands Netherlands
Niek is the founder and programmer of DaanSystems.com and is working on many projects all the time. He makes a living by doing contractwork for others.

Comments and Discussions

 
QuestionEnable/Disable Issue with IE9 Pin
HydGuy31-Jan-12 3:48
HydGuy31-Jan-12 3:48 
GeneralFIX: m_ToolTip.AddTool() with correct button size [modified] Pin
Christoph Conrad26-Jul-10 0:57
Christoph Conrad26-Jul-10 0:57 
GeneralText "Click me" not shown Pin
the Jimmy21-Feb-07 20:10
the Jimmy21-Feb-07 20:10 
GeneralRe: Text "Click me" not shown Pin
Duhasty5-Apr-09 5:36
Duhasty5-Apr-09 5:36 
Generaltext over bitmap Pin
tyounsi2-Jan-07 14:07
tyounsi2-Jan-07 14:07 
Generalload image from file [modified] Pin
a.damm7-Aug-06 21:51
a.damm7-Aug-06 21:51 
GeneralTransparent bitmaps Pin
Gismow26-Jul-06 22:10
Gismow26-Jul-06 22:10 
Generalplz help me to solve tis problem with getting Front Page 98’s hover buttons to function. Pin
ac sakura7-Jul-06 1:59
ac sakura7-Jul-06 1:59 
Generalu r experiencing problem with getting Front Page 98's Hover buttons to fuction Pin
ac sakura7-Jul-06 1:53
ac sakura7-Jul-06 1:53 
GeneralSlightly Off Topic Pin
69 Bay4-Jul-06 6:26
69 Bay4-Jul-06 6:26 
Generaltooltips Pin
ramyasangeet8-Mar-06 21:52
ramyasangeet8-Mar-06 21:52 
Generaldinamic creation Pin
Max Santos8-Feb-06 14:09
Max Santos8-Feb-06 14:09 
GeneralButton Not Showing up Pin
tweetpiyo6-Feb-06 7:15
tweetpiyo6-Feb-06 7:15 
GeneralMinor improvement - transparent background Pin
Hans Dietrich5-Sep-04 9:11
mentorHans Dietrich5-Sep-04 9:11 
GeneralRe: Minor improvement - transparent background Pin
Anne Jan Beeks25-Jun-05 3:32
Anne Jan Beeks25-Jun-05 3:32 
GeneralRe: Minor improvement - transparent background Pin
Sims11-Aug-05 0:36
Sims11-Aug-05 0:36 
GeneralRe: Minor improvement - transparent background Pin
newofilms7-Dec-05 15:04
newofilms7-Dec-05 15:04 
GeneralButton as Hyperlink in MFC Pin
Member 82043224-Mar-04 1:59
Member 82043224-Mar-04 1:59 
GeneralSomething maybe better! Pin
Tailor28-Feb-04 21:06
Tailor28-Feb-04 21:06 
GeneralProblem compiling example in VC++ 7 Pin
Sick@work4-Nov-03 2:34
Sick@work4-Nov-03 2:34 
GeneralRe: Problem compiling example in VC++ 7 Pin
HJ Jo12-Dec-03 1:57
HJ Jo12-Dec-03 1:57 
GeneralRe: Problem compiling example in VC++ 7 Pin
Just_a_girl15-May-04 22:58
Just_a_girl15-May-04 22:58 
GeneralRe: Problem compiling example in VC++ 7 Pin
ihawley19-Jul-04 22:58
ihawley19-Jul-04 22:58 
All older App Wizard generated projects include the CWinApp::Enable3dControls() call to well, enable 3d controls. However with the latest version of .net and VC++'s associated libraries this appears to be unnecessary and so use of this function genrates a warning, although in truth I presume there is no harm in it's inclusion. As such, however, all projects containing the call that are migrated to .net (2003 at least) will produce this warning.

Other than that, it appears that .net 2003 is more rigorous with it's ON_MESSAGE() macro and consequently the return type for the CHoverButton::OnMouseHover() should be an LRESULT and not a void. Changing this fixes the problems and it compiles without any trouble. The above issues are issues to do with migration of the project/code to .net.

As already stated the slight flicker on the control can be avoided by handling WM_ERASEBKGND message, although you may still get a little "tearing", more noticeable on much larger buttons I imagine.

To eliminate all such artifcats, the only real way to handle it is ti blit the image to the screen during the vertical blank. This is problemtic at best and so handling the WM_ERASEBKGND is the cheaper and more acceptable solution.

Generally speaking, painting each pixel only once will provide the least flicker!

Smile | :)

IMH
GeneralTrackMouseEvent Pin
shinay19-Jul-03 11:57
shinay19-Jul-03 11:57 
GeneralRe: TrackMouseEvent Pin
Anonymous25-Aug-03 22:04
Anonymous25-Aug-03 22:04 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.