Balloon Tooltips

and enhanced
by Guy Brousseau and
Greg Gagnaux.

This class, CTooolTipWnd is useful if you want tooltips to
look like an ellipse rather than the standard rectangle as shown
in the image below.

Usage is somewhat similar to MFC CToolTipCtrl.

To use CTooolTipWnd in an MFC application, you have to perform
the following steps

1. Add ToolTipWnd.cpp to your
project.

2. #include "ToolTipWnd.h"
in the header file of the class, where you want tooltips.

3. Add a variable in the class.

CToolTipWnd m_BalloonToolTip;

3. In OnInitDialog (if it is a dialog) or OnInitialUpdate (if
it is a formview) add the following code

m_BalloonToolTip.Create(this);

m_BalloonToolTip.AddTool(GetDlgItem(<id>),
<text>, [text color]);

eg.

m_BalloonToolTip.AddTool(GetDlgItem(IDC_EDIT1),"Tooltip",
RGB(255,0,0));

Third parameter is optional. If it is not given the default
text color ( RGB(0,0,0) ) is used. The default text color can
also be set using the public function SetDefTextColor.

4. Override PreTranslateMessage and add the following code


if(m_BalloonToolTip)
m_BalloonToolTip.RelayEvent(pMsg);

Here is a list of all the public methods and variables.

  • void AddTool(CWnd *pWnd, CString
    strText, COLORREF clrTextColor=NULL);
  • void SetWidth(int iWidth)
    This function is used for setting the width of the
    tooltip window. Default is 100
  • void SetHeight(int iHeight)
    This function is used for setting the height of
    the tooltip window. Default is 60
  • void SetBkColor(COLORREF clrRef)
    To Set the background color
    of the tooltip window. Default is Light yellow (on a 256
    color display this will look dithered.
  • void SetFrameColor(COLORREF clrRef)
    To set the border color of
    the window. Default is Blue
  • void SetDefTextColor(COLORREF
    clrRef)
    The default textcolor is
    black, you can change it using this function.
  • void SetFontHeight(int iHeight) {
    m_iFontHeight = iHeight; }
    To set the font size.
    Default is 14.
  • void SetFontName(CString
    strFontName) { m_strFontName = strFontName; }
    To the font name. Default is
    Arial.

The tooltip text will be wrapped iniside the window if it
exceeds one line otherwise it will be centered horizontally and
vertically.

Enhancements

Enhancements by Guy Brousseau

Guy Brousseau has enchanced this control with the following corrections
and additions:

Here is the summary of changes (addressing two posted comments) extracted
from the code

  • Add TIMER support (0 if no timer wanted)
  • DON’T GET CLIPPED around screen corners (although incomplete).
  • Corrected a bug when m_stuck is not set and mouse moves over the tooltip, it moved
  • Corrected a bug when you jumped quickly into a control and land onto a tooltip, the mouse moves and click were not forwarded
  • Trap Non-client messages for mouse move and button click (cf. scrollbars in edit controls)
  • Replace tool in lookup table if inserted more than once
  • Explicitly display the tooltip by programmation request (rather than mouse move)
  • All revamped (simplified and commented) to be better understandable (sorry, but I must stick to local programming style…)

Guy thanks his boss, Jean Ctti, who joyfully granting him the 2 days necessary for this mission.

Enhancements by Greg Gagnaux

Greg Gagnaux has enchanced this control with the following additions:

Here is the summary of changes (one of the posted comments)

  • Added a second timer to allow a short delay before the tooltip is displayed. (Defaults to 1 second)

Download source (5.7 Kb)

Download demo (36 Kb)

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read