Click here to Skip to main content
15,895,011 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 420.8K   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

 
GeneralPossible Bug. Pin
Julien9-May-01 14:57
Julien9-May-01 14:57 
GeneralRe: Possible Bug. Pin
Earl Rex Arao-arao10-May-01 22:10
Earl Rex Arao-arao10-May-01 22:10 
Generalhelp Pin
Member 65773-Apr-01 21:03
Member 65773-Apr-01 21:03 
GeneralRe: help Pin
3-Apr-01 21:57
suss3-Apr-01 21:57 
GeneralRe: help Pin
Earl Rex Arao-arao10-May-01 22:37
Earl Rex Arao-arao10-May-01 22:37 
GeneralWindows 95 Pin
Michael Groeger13-Dec-00 3:47
Michael Groeger13-Dec-00 3:47 
GeneralRe: Windows 95 Pin
19-Dec-00 2:53
suss19-Dec-00 2:53 
GeneralRe: Windows 95 Pin
Philippe Lhoste22-Oct-01 2:22
Philippe Lhoste22-Oct-01 2:22 
MSDN excerpt:
>>>
_TrackMouseEvent
The _TrackMouseEvent function posts messages when the mouse pointer leaves a window or hovers over a window for a specified amount of time. This function calls TrackMouseEvent if it exists, otherwise it emulates it.

Requirements
Windows NT/2000: Requires Windows 2000 (or Windows NT 4.0 with Internet Explorer 3.0 or later).
Windows 95/98: Requires Windows 98 (or Windows 95 with Internet Explorer 3.0 or later).
Header: Declared in Commctrl.h.
Library: Use Comctl32.lib.
<<<
Seems OK for me, I suppose there isn't much original Win95 around there (those shipped without mscrt.dll...).
Otherwise, you can't use improvements made since the original release, that can lead to quite poor interfaces.
Unless you make a program that must be used only in a corporate setting with old units (I wrote 16-bit programs for this reason Smile | :)

At worse, you can write code that handle this case, or warn the user to upgrade...

Regards.

--=#=--=#=--=#=--=#=--=#=--=#=--=#=--=#=--=#=--
Philippe Lhoste (Paris -- France)
Professional programmer and amateur artist
http://jove.prohosting.com/~philho/
--=#=--=#=--=#=--=#=--=#=--=#=--=#=--=#=--=#=--
GeneralBack ground Color of the button. Pin
1-Dec-00 21:39
suss1-Dec-00 21:39 
GeneralRe: Back ground Color of the button. Pin
30-May-01 7:45
suss30-May-01 7:45 
GeneralRe: Back ground Color of the button. Pin
Philippe Lhoste22-Oct-01 2:27
Philippe Lhoste22-Oct-01 2:27 
GeneralSeems pretty good. Pin
adamsd516-Oct-00 13:37
adamsd516-Oct-00 13:37 

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.