viksoe.dk

RTF ToolTip control


This article was submitted .


The RTF ToolTip control draws rich text formatted (RTF) text instead of the boring old system font plain text. With RTF formatting you can add funky colors, font changes, images, tables etc. etc. to your tooltips.

The WTL control is based on a regular tooltip control but uses the Custom Draw capabilities of this Windows Common Control.
To be honest, the Microsoft tooltip API is not particular great. Very strange handling of dialogs, custom draw notifications and not to mention the inability to determine the tip text length... but I didn't want to re-invent the wheel so the control simply takes over a standard tooltip control, keeps a hidden RTF control, and whenever the real tooltip wants to paint the text, the RTF control paints the text instead.

To create a new tooltip control, place an instance in your window...

CRtfToolTipCtrl m_tip
In the OnInitDialog() event handler, create the tooltip control and add the tools as with a regular tooltip.
  LRESULT OnInitDialog(UINT /*uMsg*/, 
                       WPARAM /*wParam*/, 
                       LPARAM /*lParam*/, 
                       BOOL& /*bHandled*/)
  {
    ...
    DWORD dwStyle = WS_POPUP | TTS_NOPREFIX | TTS_ALWAYSTIP;
    m_tip.Create(m_hWnd, rcDefault, NULL, dwStyle);

    CToolInfo ti(TTF_SUBCLASS|TTF_IDISHWND, m_hWnd, 
                (UINT)hwndTool, NULL, pstrRtfText);
    m_tip.AddTool(ti);
    m_tip.Activate(TRUE);
    ...
  }
Then add the following reflection macro to your message map:
  BEGIN_MSG_MAP(CMainDlg)
    ...
    REFLECT_NOTIFICATIONS()
  END_MSG_MAP()

The control resizes itself according to the RTF, but can be controlled with the TTM_SETMAXTIPWIDTH message. Use it in combination with the new SetWordWrap() method to automatically word wrap the tip text.

Because of the nasty 80 character tip limitation in Windows, you will probably want to support the TTN_GETDISPINFO by using the special LPSTR_TEXTCALLBACK text string instead of storing the RTF text inside the tool. RTF text can be pretty large.
You may also feed the control with plain text (no RTF codes). This allows you to have a multi-line tooltip control on all Windows platforms.

And do remember to initialise the RTF library properly in your application startup code.

  int WINAPI _tWinMain(HINSTANCE hInstance, HIN...)
  {
    ...
    ::InitCommonControls();
    HINSTANCE hInstRich = 
         ::LoadLibrary(CRichEditCtrl::GetLibraryName());
    ...
    int nRet = Run(lpstrCmdLine, nCmdShow);
    ...
    ::FreeLibrary(hInstRich);
    ...
    return 0;
  }
Also remember to set the following macro...
  #define _RICHEDIT_VER 0x0200
in stdafx.h because the Rich Text control version 2 does better resizing.

That should be enough to get started.

Source Code Dependencies

Microsoft Visual C++ 6.0
Microsoft WTL 3.0 Library

See Also

Sample WTL application with RTF tip

Download Files

DownloadSource Code (4 Kb)

To the top