Click here to Skip to main content
15,878,959 members
Articles / Desktop Programming / MFC
Article

Moveable \ Resizable Runtime Hover Buttons with ToolTips

Rate me:
Please Sign up or sign in to vote.
4.62/5 (7 votes)
28 Jul 20012 min read 101.3K   3.3K   50   5
An article on creating changable buttons

Sample Image - hoverbuttonex.jpg

Introduction

I orginally found CHoverButton by Nick Albers and liked the HoverButton idea, however it did not have enough versatility for what I wanted. There was a good start with the MouseHover\Leave code though. I wanted to be able move and resize the buttons at runtime. I also wanted to be able to stretch the bitmaps as well as load the hover images from a horizontal or veritcal layout. I also needed the buttons to draw as regular buttons just in case no bitmaps where loaded. Thus CHoverButtonEx is created.

To make use of the CHoverButtonEx class simply create a button on your dialog and change it from CButton to CHoverButtonEx.

#include "hoverbutton.h"
...
CHoverButtonEx m_hoverbtn;

if no bitmaps or tooltips are needed, then you are done. If you need Bitmaps, then simply call LoadBitmap(IDB_Bitmap); or else

LoadBitmapFromFile("Bitmap.bmp");
. LoadBitmap takes an image that has 3 equal sized parts. The size of each bitmap should be width (or height) / 3 = image size.

Call SetHorizontal(TRUE); for horizontal images, SetHorizontal(FALSE); for vertical images before calling LoadBitmap. Images should be laid out as:

Next we add tooltips by calling SetToolTipText(UINT nResourceStringID, bActivate = TRUE) or else as SetToolTipText(CString spText, bActivate = TRUE). Activate is set to true to create the Tooltip and tell it to show if the mouse hovers. If Activate == FALSE, then the ToolTip will not show when the mouse hovers over the button. SetToolTipText() will create the ToolTip and set its text at the same time. Well what if I want to change the ToolTip text? Then you merely call

DeleteToolTip();
SetToolTipText("My string here");

This will delete the previous tooltip we created and create a new one with the proper text. Why don't we just reset the text you ask? Ideally that would work, however, when the button is resizeable, merely setting the text to a new string does not work. We have to delete the tooltip and recreate it with the proper dimensions and text.

To allow moving and resizing of the button at runtime, we merely call SetMoveable() and this will allow moving or resizing at runtime. Moving is done at runtime by Right Clicking and dragging the button then Left Clicking where you want to place the object. Resizing is done by holding down the Control Key and Right Clicking the Button, then LeftClicking when the button is resized to what you want. Thus our code to use this class looks like this in our header file:

#include "hoverbutton.h"
...
...
...
CHoverButtonEx m_hover;

And like this in our .cpp file:

m_hover.SetHorizontal(TRUE); // Images are laid out horizontally
m_hover.LoadBitmap(IDB_HOVER);//Load from resource
CString hover=_T("Hover Button");//ToolTip text
m_hover.SetMoveable();// Allow moving and resizing
m_hover.SetToolTipText(hover);//Create the ToolTip

The functions to handle these processess are pretty well documented in the code. Enjoy!

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
United States United States
Programming using MFC and ATL for almost 12 years now. Currently studying Operating System implementation as well as Image processing. Previously worked on DSP and the use of FFT for audio application. Programmed using ADO, ODBC, ATL, COM, MFC for shell interfacing, databasing tasks, Internet items, and customization programs.

Comments and Discussions

 
QuestionRe: License Pin
mla1541-Jul-09 10:10
mla1541-Jul-09 10:10 
GeneralBitmap Pin
Amit kumhare24-Apr-07 22:21
Amit kumhare24-Apr-07 22:21 
GeneralRe: Bitmap Pin
Fred Ackers25-Apr-07 13:52
Fred Ackers25-Apr-07 13:52 
GeneralGood Article.. some suggestion.. [modified] Pin
bootflag12-Jan-07 15:00
bootflag12-Jan-07 15:00 
GeneralRe: Good Article.. some suggestion.. Pin
Mohammadj7-Apr-09 0:38
Mohammadj7-Apr-09 0:38 

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.