

Introduction
Microsoft Word displays a tooltip when you drag the vertical scrollbar in a document.
The tooltip shows the number of the page that will be displayed when you stop dragging,
and sometimes other information as well.
This article shows how this can be done.
Background
I've been developing an MFC application to play Midi files and show music scores.
The score window takes too long to redraw as the scrollbar is being dragged, so the program
waits until dragging has stopped before redrawing the window. I wanted some feedback
on exactly where the scrollbar was as I was dragging it, so that I could stop dragging
at the required bar number. What was needed was a tooltip window, very much as
Microsoft Word displays the page number in a tooltip as the vertical scrollbar is being dragged.
MFC has a class called CToolTipCtrl
, so that seemed the obvious place to start. But
it seems to have a built-in delay, and
I couldn't work out how the make the tooltip window appear without delay
when I clicked the mouse on the scrollbar.
Fortunately, I then found
this article on CodeProject
(thanks Zarembo!), which displays a tooltip window without any delay.
I converted the code to C++, wrapped it in a class, tweaked it bit, and it works fine.
Adding a tooltip to a scrollbar
You can add a tooltip to the vertical or the horizontal scrollbar in a window, or both.
The following describes how to add a tooltip to the vertical scrollbar:
That's all you have to do. The rest of the work is done by the
CRHTipWnd
class.
If you build and run your program you should see some sort of tooltip window appearing when
you drag the vertical scrollbar in the window whose class you've modified.
If you want the
tooltip to apply to the horizontal scrollbar instead of the vertical scrollbar then add a
handler for WM_HSCROLL
instead of WM_VSCROLL
.
If you want tooltips for both scrollbars then you'll need two CRHTipWnd
objects in the class,
you must add handlers for WM_VSCROLL
and WM_HSCROLL
, and the two handlers must refer to the
two different CRHTipWnd
objects.
The demo program
This is the MULTIPAD MFC sample program, with the following changes to demonstrate a tooltip
window appearing when the vertical scrollbar is dragged:
Using the CRHTipWnd
class
CRHTipWnd::CRHTipWnd()
This constructs a
CRHTipWnd
object. The Windows tooltip window is not created
and attached until
CRHTipWnd::CRHInit()
is called.
CRHTipWnd::CRHInit(const char *BigString)
This creates
a Windows tooltip window and attaches it to the
CRHTipWnd
object.
It does no harm to call
CRHTipWnd::CRHInit()
more than once. However, only the first call
to
CRHTipWnd::CRHInit()
after
CRHTipWnd::CRHTipWnd()
or
CRHTipWnd::CRHClear()
is called has any effect.
This provides better encapsulation of
CRHTipWnd
by freeing
the code calling
CRHTipWnd::CRHInit()
from having to keep track of whether it's
called it already.
The const char *BigString
parameter tells CRHTipWnd
how big to make the tooltip window.
The tooltip window is a fixed size, determined by BigString
.
This gets round a minor problem I hit when developing this code, which is
described in "Points of Interest" below.
void CRHTipWnd::CRHSetText(const char *fmt, ...)
This sets the text in the tooltip window.
It takes a variable number of parameters in the same sort of way as void
CString::Format()
.
CRHTipWnd::CRHClear()
This destroys the Windows tooltip window attached to the
CRHTipWnd
object.
After
CRHTipWnd::CRHClear()
has been called,
CRHTipWnd::CRHInit()
may be called again,
to create another Windows tooltip window, possibly in a different place and with a different size.
Typically, you would call CRHTipWnd::CRHInit()
and CRHTipWnd::CRHSetText()
to handle an
SB_THUMBTRACK
event in your OnHScroll()
or OnVScroll()
routine (or both),
and CRHTipWnd::CRHClear()
to handle an SB_THUMBPOSITION
event.
How the CRHTipWnd
class works
You can just leave the
CRHTipWnd
class to do its job, or you can take a peek inside.
The key to creating a tooltip window is to call CWnd::CreateEx()
using the registered class name
TOOLTIPS_CLASS
(which is #define
d in COMMCTRL.H to be "tooltips_class32"
).
I'm not sure which of the other parameters are critical, but the following seems to work:
CreateEx(WS_EX_TOPMOST,
TOOLTIPS_CLASS,
NULL,
TTS_NOPREFIX | TTS_ALWAYSTIP,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
NULL);
This is done in
CRHTipWnd::CRHInit()
. Once the tooltip window has been created,
CRHTipWnd
sends various
TTM_
messages to it to make it appear, display the right text, and disappear as required.
Points of Interest
History
- 5th July 2004 - first submitted.