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.
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
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
WM_HSCROLL instead of
If you want tooltips for both scrollbars then you'll need two
CRHTipWnd objects in the class,
you must add handlers for
WM_HSCROLL, and the two handlers must refer to the
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:
This constructs a
object. The Windows tooltip window is not created
and attached until
CRHTipWnd::CRHInit(const char *BigString)
a Windows tooltip window and attaches it to the
It does no harm to call
more than once. However, only the first call
is called has any effect.
This provides better encapsulation of
the code calling
from having to keep track of whether it's
called it already.
const char *BigString parameter tells
CRHTipWnd how big to make the tooltip window.
The tooltip window is a fixed size, determined by
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
This destroys the Windows tooltip window attached to the
has been called,
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::CRHSetText() to handle an
SB_THUMBTRACK event in your
OnVScroll() routine (or both),
CRHTipWnd::CRHClear() to handle an
CRHTipWnd class works
You can just leave the
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
#defined in COMMCTRL.H to be
I'm not sure which of the other parameters are critical, but the following seems to work:
TTS_NOPREFIX | TTS_ALWAYSTIP,
This is done in
. Once the tooltip window has been created,
messages to it to make it appear, display the right text, and disappear as required.
Points of Interest
- 5th July 2004 - first submitted.