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);
m_hover.LoadBitmap(IDB_HOVER);
CString hover=_T("Hover Button");
m_hover.SetMoveable();
m_hover.SetToolTipText(hover);
The functions to handle these processess are pretty well documented in the code. Enjoy!
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.