Click here to Skip to main content
15,884,064 members
Articles / Desktop Programming / MFC
Article

RichText Tool-tip Control

Rate me:
Please Sign up or sign in to vote.
4.80/5 (35 votes)
6 Oct 2008CPOL4 min read 137.1K   5.5K   91   28
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:

C++
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:

C++
m_tip.Create(this);

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

C++
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:

C++
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:

C++
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)


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

 
GeneralRe: Does NOT word on windows 98 Pin
Paul Vickery19-May-05 0:46
professionalPaul Vickery19-May-05 0:46 
GeneralRe: Does NOT word on windows 98 Pin
Paul Vickery19-May-05 22:45
professionalPaul Vickery19-May-05 22:45 
GeneralThis fix olso helps for NT Pin
Dieter Hammer11-Aug-05 4:55
Dieter Hammer11-Aug-05 4:55 

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.