Click here to Skip to main content
15,440,991 members
Articles / Desktop Programming / MFC
Posted 13 Mar 2005


91 bookmarked

RichText Tool-tip Control

Rate me:
Please Sign up or sign in to vote.
4.80/5 (35 votes)
6 Oct 2008CPOL4 min read
A tool-tip control with support for rich-text formatting
RichToolTipCtrl Demo


Recently, I wanted to show a tool-tip to display some information about an object in a diagram. I wanted the tool-tip to contain the name of the object, followed by any comment text about that object. That was fine, except it looked very plain. What I really wanted was to show the object's name in a bold font, and the rest in the normal font, so I came up with the control presented here.

This control is a drop-in replacement for a CToolTipCtrl control, with the added benefit of being able to specify rich-text as the tool-tip text. Information on the rich text format can be found on MSDN.

Using the Demo Application

The demo application demonstrates the rich-tip control using a simple edit control, whose text is added to its tool-tip, and also a rich-edit control, with a few formatting buttons. Text in the rich-edit control appears in the tip for the rich-edit control. Rich-text may be pasted into the rich-edit control from other applications, or alternatively, if there is a file named test.rtf in the EXE's directory, then it is streamed into the control on running the application.

Using the Control

This control should be used in exactly the same way as CToolTipCtrl. While the demo application demonstrates how to add tool-tips to various controls, the basic steps for simple usage are as follows:

Add a CRichToolTipCtrl member variable to the window class which is to have the tool tips, thus:

CRichToolTipCtrl m_tip;

Next, add a call to create the control in your OnInitDialog, if it's a dialog, or your OnInitialUpdate, if it's a view, or in OnCreate for a control or other window:


You should then add the following line to your PreTranslateMessage override:


Once that's done, you can start adding tools to the control. So, to add a tool tip to the OK button of a dialog, you would then call, in your OnInitDialog, after creating the tip control:

m_tip.AddTool(GetDlgItem(IDOK), _T("{\\rtf This is my {\\b OK} button}"));

As with the standard CToolTipCtrl, if you don't specify any text for the tool, then the tool-tip control sends the dialog a TTN_GETDISPINFO/TTN_NEEDTEXT notification. See the code of the demo application for an example of how to handle this.

Implementation Notes

Once I had realised that I was going to write my own control, I had to come up with a way of entering text such that it could be parsed, and displayed. I had a choice:

  1. Devise my own markup, and parse it.
  2. Accept and parse RTF or HTML.
  3. Use a control which parses RTF/HTML, and can output to a given device context.

I didn't really want to do 1 or 2, and I favoured RTF over HTML, as images can be embedded into RTF, so I had a look at the WordPad sources, which uses CRichEditView, to see how it rendered its output to the printer, and found that it uses CRichEditCtrl::FormatRange(), which draws the output to a specified device context.

Having found out how to render the RTF, I added a handler for the NM_CUSTOMDRAW notifications to draw to the tool-tip's window. To prevent the default rendering appearing, I set the text colour to the back colour.

This was fine except that the tool-tip window was sized to fit the original mark-up text, which is often very big, so it needed resizing. After some searching, I found the article Calculating a Rich Edit Control Minimum Size by Thales P. Carvalho, which allowed me to calculate the correct size for the tip window. I then re-positioned the window to a sensible place based on the cursor position.

This was enough to display basic formatted text in a tip window, but I wanted to be able to support embedded images. To implement this in a rich edit control, it is necessary to give the control an OLE callback handler (a pointer to an IRichEditOleCallback COM object). This is required to allow the control to allocate memory for the storage of embedded items. I added the interface code from the MFC's CRichEditView to my control. The only method that needed implementing was GetNewStorage(), so all other methods simply return S_OK.


The CRichToolTipCtrl class has just one public function, in addition to the default constructor and virtual destructor:

static CString MakeTextRTFSafe(LPCTSTR lpszText);

This static function takes a text string, and escapes special characters to make the text acceptable for RTF.

Acknowledgements and References


Version 1.2 - 06 Oct 2008

  • Updated to work correctly on Vista and XP
  • Updated project to use Visual Studio 2008
  • Added project for Visual C 6

Version 1.1 - 22 Mar 2005

  • Added static method for escaping plain text to make it RTF safe
  • Modified the positioning to work on multi-monitor systems
  • Removed dependency on RichToolTipCtrlDemo.h

Version 1.0 - 14 Mar 2005

  • First version


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Written By
Software Developer (Senior)
United Kingdom United Kingdom
Originally from an electronics background, I moved into software in 1996, partly as a result of being made redundant, and partly because I was very much enjoying the small amount of coding (in-at-the-deep-end-C) that I had been doing!

I swiftly moved from C to C++, and learned MFC, and then went on to real-time C on Unix. After this I moved to the company for which I currently work, which specialises in Configuration Management software, and currently program mainly in C/C++, for Windows. I have been gradually moving their legacy C code over to use C++ (with STL, MFC, ATL, and WTL). I have pulled in other technologies (Java, C#, VB, COM, SOAP) where appropriate, especially when integrating with third-party products.

In addition to that, I have overseen the technical side of the company website (ASP, VBScript, JavaScript, HTML, CSS), and have also worked closely with colleagues working on other products (Web-based, C#, ASP.NET, SQL, etc).

For developing, I mainly use Visual Studio 2010, along with an in-house-designed editor based on Andrei Stcherbatchenko's syntax parsing classes, and various (mostly freeware) tools. For website design, I use Dreaweaver CS3.

When not developing software, I enjoy listening to and playing music, playing electric and acoustic guitars and mandolin.

Comments and Discussions

QuestionC# version Pin
Misiu28-Dec-15 4:00
MemberMisiu28-Dec-15 4:00 
GeneralMy vote of 5 Pin
Member 1107406624-Sep-14 22:45
MemberMember 1107406624-Sep-14 22:45 
QuestionBalloon tooltip Pin
Sharon Gottlieb27-Jan-14 1:50
MemberSharon Gottlieb27-Jan-14 1:50 
Question[SOLVED] Setting tab-stop or using rtf tables - not working?? Pin
Phil Outram24-Sep-13 1:04
MemberPhil Outram24-Sep-13 1:04 
GeneralMy vote of 5 Pin
Franc Morales23-May-13 0:13
MemberFranc Morales23-May-13 0:13 
QuestionWonderful application - if only I could stop the flickering Pin
Michael B Pliam22-Jul-11 15:52
MemberMichael B Pliam22-Jul-11 15:52 
GeneralBug correction Pin
Octopod21-Oct-09 5:08
MemberOctopod21-Oct-09 5:08 
GeneralBuffer overrun Pin
Octopod21-Oct-09 5:04
MemberOctopod21-Oct-09 5:04 
GeneralSome replacements for Test.rtf cause re-display Pin
l_d_allan8-May-09 5:11
Memberl_d_allan8-May-09 5:11 
AnswerRe: Some replacements for Test.rtf cause re-display Pin
Paul Vickery14-May-09 1:24
professionalPaul Vickery14-May-09 1:24 
GeneralRe: Some replacements for Test.rtf cause re-display Pin
l_d_allan14-May-09 2:05
Memberl_d_allan14-May-09 2:05 
GeneralBugfix for multi-monitor setup Pin
Christian Severin3-Sep-08 2:17
MemberChristian Severin3-Sep-08 2:17 
Question????????????????????? Pin
ariel_lerman_1235-Nov-07 9:20
Memberariel_lerman_1235-Nov-07 9:20 
AnswerRe: ????????????????????? Pin
Paul Vickery6-Nov-07 4:36
professionalPaul Vickery6-Nov-07 4:36 
QuestionUsing RichToolTipCtrl with a CComboBox doesn't work Pin
bholman28-Sep-07 10:28
Memberbholman28-Sep-07 10:28 
I'm using RichToolTipCtrl for a CComboBox. It is working fine for CButtons and CEdit controls, but I must be incorrectly implementing it for the combo box. Here is the way I am doing it:

In InitDialog:


m_tip.AddTool(GetDlgItem(IDC_COMBO_TRANS_X_TYPE), " ");

Then I override UpdateDate() and add the following:
CWnd* editXRate = this->GetDlgItem(IDC_COMBO_TRANS_X_TYPE);
CString strXrate = "";
if (editXRate)
m_tip.UpdateTipText(strXrate, GetDlgItem(IDC_COMBO_TRANS_X_TYPE));
This is exactly the way I coded for the CEdit contorls on the dialog, and it works fine for them, but no tooltips show up for the CComboBox.

Anyone have any ideas where I'm going wrong?

Thank you in advance for any help with this.

Bill Holman

Bill Holman
Sr. Systems Programmer

AnswerRe: Using RichToolTipCtrl with a CComboBox doesn't work Pin
Paul Vickery5-Oct-07 1:03
professionalPaul Vickery5-Oct-07 1:03 
GeneralRe: Using RichToolTipCtrl with a CComboBox doesn't work Pin
bholman5-Oct-07 4:55
Memberbholman5-Oct-07 4:55 
GeneralUpdateTipText while tooltip is visible causes flickering Pin
Heinz Drewer24-May-05 23:54
MemberHeinz Drewer24-May-05 23:54 
GeneralRe: UpdateTipText while tooltip is visible causes flickering Pin
nightstalker438436348t423-Jul-06 21:24
Membernightstalker438436348t423-Jul-06 21:24 
GeneralRe: UpdateTipText while tooltip is visible causes flickering Pin
Mike Marquet7-Sep-06 23:02
MemberMike Marquet7-Sep-06 23:02 
GeneralSeems like setting text with UpdateTipText doesn't work Pin
Heinz Drewer18-May-05 1:37
MemberHeinz Drewer18-May-05 1:37 
GeneralRe: Seems like setting text with UpdateTipText doesn't work Pin
Paul Vickery19-May-05 23:31
professionalPaul Vickery19-May-05 23:31 
GeneralRe: Seems like setting text with UpdateTipText doesn't work Pin
Heinz Drewer24-May-05 23:21
MemberHeinz Drewer24-May-05 23:21 
General[Message Deleted] Pin
Igor Mihailov17-May-05 0:12
MemberIgor Mihailov17-May-05 0:12 
GeneralRe: Does NOT word on windows 98 Pin
Paul Vickery17-May-05 23:50
professionalPaul Vickery17-May-05 23:50 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.