Click here to Skip to main content
15,885,366 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

 
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 
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 
GeneralRe: TrackMouseEvent Pin
Anonymous25-Aug-03 22:04
Anonymous25-Aug-03 22:04 
Generalmybitmap.detach() missing in CHoverButton::LoadImage() Pin
RhettLiu12-Jul-03 21:35
RhettLiu12-Jul-03 21:35 
GeneralRe: mybitmap.detach() missing in CHoverButton::LoadImage() Pin
Anonymous4-Jan-04 8:13
Anonymous4-Jan-04 8:13 
GeneralProblem Pin
Wu Mintao14-Jun-03 8:43
Wu Mintao14-Jun-03 8:43 
GeneralRe: Problem Pin
mach7725-Jun-03 9:45
mach7725-Jun-03 9:45 
GeneralRe: Problem Pin
Wu Mintao26-Jun-03 3:02
Wu Mintao26-Jun-03 3:02 
GeneralThanks for the _TrackMouseEvent ... Pin
Maximilien6-Dec-02 4:54
Maximilien6-Dec-02 4:54 
GeneralPROBLEM!! Pin
Anonymous10-Oct-02 4:04
Anonymous10-Oct-02 4:04 
GeneralHover Button problem Pin
11-Feb-02 23:12
suss11-Feb-02 23:12 
GeneralRe: Hover Button problem Pin
thanh200222-Oct-02 7:02
thanh200222-Oct-02 7:02 
GeneralRe: Hover Button problem Pin
ac sakura7-Jul-06 2:04
ac sakura7-Jul-06 2:04 
Generalinformation Pin
23-Dec-01 9:34
suss23-Dec-01 9:34 
GeneralProblem with CHoverButton::LoadBitmap in Netscape Plugin (DLL) Pin
5-Nov-01 17:21
suss5-Nov-01 17:21 
GeneralRe: Problem with CHoverButton::LoadBitmap in Netscape Plugin (DLL) Pin
16-Nov-01 9:49
suss16-Nov-01 9:49 
GeneralRe: Problem with CHoverButton::LoadBitmap in Netscape Plugin (DLL) Pin
Anonymous10-Oct-02 3:54
Anonymous10-Oct-02 3:54 
Generalflicker Pin
12-Jul-01 11:27
suss12-Jul-01 11:27 
GeneralRe: flicker Pin
20-Aug-01 3:04
suss20-Aug-01 3:04 
GeneralRe: flicker Pin
seikou12-Aug-04 23:38
seikou12-Aug-04 23:38 
GeneralHoverButton on FormView Pin
Tim Matthews15-Jun-01 5:15
Tim Matthews15-Jun-01 5:15 
GeneralHover Button Pin
31-May-01 12:30
suss31-May-01 12:30 

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.