Click here to Skip to main content
Click here to Skip to main content

Moveable \ Resizable Runtime Hover Buttons with ToolTips

, 28 Jul 2001
Rate this:
Please Sign up or sign in to vote.
An article on creating changable buttons
<!-- Download Links --> <!-- Article image -->

Sample Image - hoverbuttonex.jpg

<!-- Main HTML starts here -->

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

Share

About the Author

Fred Ackers
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 Pinmembermla1541-Jul-09 11:10 
GeneralBitmap PinmemberAmit kumhare24-Apr-07 23:21 
GeneralRe: Bitmap PinmemberFred Ackers25-Apr-07 14:52 
GeneralGood Article.. some suggestion.. [modified] Pinmemberbootflag12-Jan-07 16:00 
GeneralRe: Good Article.. some suggestion.. PinmemberMohammadj7-Apr-09 1:38 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.141223.1 | Last Updated 29 Jul 2001
Article Copyright 2001 by Fred Ackers
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid