Click here to Skip to main content
Click here to Skip to main content

RichText Tool-tip Control

, 6 Oct 2008
Rate this:
Please Sign up or sign in to vote.
A tool-tip control with support for rich-text formatting
RichToolTipCtrl Demo

Introduction

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:

m_tip.Create(this);

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

m_tip.RelayEvent(pMsg);

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.

Documentation

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

History

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

License

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

About the Author

Paul Vickery
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.
Follow on   Google+   LinkedIn

Comments and Discussions

 
QuestionBalloon tooltip PinmemberSharon Gottlieb27-Jan-14 1:50 
Question[SOLVED] Setting tab-stop or using rtf tables - not working?? [modified] PinmemberPhil Outram24-Sep-13 1:04 
GeneralMy vote of 5 PinmemberFranc Morales23-May-13 0:13 
QuestionWonderful application - if only I could stop the flickering PinmemberMichael B Pliam22-Jul-11 15:52 
GeneralBug correction PinmemberOctopod21-Oct-09 5:08 
GeneralBuffer overrun PinmemberOctopod21-Oct-09 5:04 
GeneralSome replacements for Test.rtf cause re-display Pinmemberl_d_allan8-May-09 5:11 
AnswerRe: Some replacements for Test.rtf cause re-display PinmemberPaul S. Vickery14-May-09 1:24 
GeneralRe: Some replacements for Test.rtf cause re-display Pinmemberl_d_allan14-May-09 2:05 
GeneralBugfix for multi-monitor setup PinmemberChristian Severin3-Sep-08 2:17 
Question????????????????????? Pinmemberariel_lerman_1235-Nov-07 9:20 
AnswerRe: ????????????????????? PinmemberPaul S. Vickery6-Nov-07 4:36 
QuestionUsing RichToolTipCtrl with a CComboBox doesn't work Pinmemberbholman28-Sep-07 10:28 
AnswerRe: Using RichToolTipCtrl with a CComboBox doesn't work PinmemberPaul S. Vickery5-Oct-07 1:03 
GeneralRe: Using RichToolTipCtrl with a CComboBox doesn't work Pinmemberbholman5-Oct-07 4:55 
GeneralUpdateTipText while tooltip is visible causes flickering PinsussHeinz Drewer24-May-05 23:54 
GeneralRe: UpdateTipText while tooltip is visible causes flickering Pinmembernightstalker438436348t423-Jul-06 21:24 
GeneralRe: UpdateTipText while tooltip is visible causes flickering PinmemberMike Marquet7-Sep-06 23:02 
GeneralSeems like setting text with UpdateTipText doesn't work PinsussHeinz Drewer18-May-05 1:37 
GeneralRe: Seems like setting text with UpdateTipText doesn't work PinmemberPaul S. Vickery19-May-05 23:31 
GeneralRe: Seems like setting text with UpdateTipText doesn't work PinsussHeinz Drewer24-May-05 23:21 
General[Message Deleted] PinmemberXPiS17-May-05 0:12 
GeneralRe: Does NOT word on windows 98 PinmemberPaul S. Vickery17-May-05 23:50 
GeneralRe: Does NOT word on windows 98 PinmemberPaul S. Vickery19-May-05 0:46 
GeneralRe: Does NOT word on windows 98 PinmemberPaul S. Vickery19-May-05 22:45 

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

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

| Advertise | Privacy | Mobile
Web03 | 2.8.140721.1 | Last Updated 7 Oct 2008
Article Copyright 2005 by Paul Vickery
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid