5,661,954 members and growing! (13,721 online)
Email Password   helpLost your password?
Desktop Development » Miscellaneous » Tooltips     Intermediate License: The Code Project Open License (CPOL)

RichText Tool-tip Control

By Paul S. Vickery

A tool-tip control with support for rich-text formatting
VC6, C++Windows, NT4, Win2K, WinXP, Win2003, MFC, VS6, Visual Studio, Dev

Posted: 14 Mar 2005
Updated: 6 Oct 2008
Views: 39,847
Bookmarked: 51 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
23 votes for this Article.
Popularity: 6.02 Rating: 4.42 out of 5
2 votes, 8.7%
1
0 votes, 0.0%
2
1 vote, 4.3%
3
1 vote, 4.3%
4
19 votes, 82.6%
5
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 S. Vickery


Originally from an electronics background, Paul moved into software in 1996, partly as a result of being made redundant, and partly because he was very much enjoying the small amount of coding (in-at-the-deep-end-C) that he had been doing (the only programming he had done prior to that was a little Pascal, and ZX Spectrum Basic!).

Paul swiftly moved from C to C++, and learned MFC, and then went on to real-time C on Unix. After this he moved to the company for which he currently works, which specialises in Configuration Management, and programs in C/C++, for Windows. He has been gradually moving their legacy C code over to C++/MFC, pulling in other new technologies as he goes where appropriate.

Paul uses Visual Studio 2005, his own in-house-designed editor based on Andrei Stcherbatchenko's syntax parsing classes, and various other freeware tools.

Occupation: Software Developer (Senior)
Location: United Kingdom United Kingdom

Other popular Miscellaneous articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 17 of 17 (Total in Forum: 17) (Refresh)FirstPrevNext
GeneralBugfix for multi-monitor setupmemberChristian Severin3:17 3 Sep '08  
General?????????????????????memberariel_lerman_12310:20 5 Nov '07  
GeneralRe: ?????????????????????memberPaul S. Vickery5:36 6 Nov '07  
QuestionUsing RichToolTipCtrl with a CComboBox doesn't workmemberbholman11:28 28 Sep '07  
AnswerRe: Using RichToolTipCtrl with a CComboBox doesn't workmemberPaul S. Vickery2:03 5 Oct '07  
GeneralRe: Using RichToolTipCtrl with a CComboBox doesn't workmemberbholman5:55 5 Oct '07  
GeneralUpdateTipText while tooltip is visible causes flickeringsussHeinz Drewer0:54 25 May '05  
GeneralRe: UpdateTipText while tooltip is visible causes flickeringmembernightstalker438436348t422:24 23 Jul '06  
GeneralRe: UpdateTipText while tooltip is visible causes flickeringmemberMike Marquet0:02 8 Sep '06  
GeneralSeems like setting text with UpdateTipText doesn't worksussHeinz Drewer2:37 18 May '05  
GeneralRe: Seems like setting text with UpdateTipText doesn't workmemberPaul S. Vickery0:31 20 May '05  
GeneralRe: Seems like setting text with UpdateTipText doesn't worksussHeinz Drewer0:21 25 May '05  
GeneralDoes NOT word on windows 98memberXPiS1:12 17 May '05  
GeneralRe: Does NOT word on windows 98memberPaul S. Vickery0:50 18 May '05  
GeneralRe: Does NOT word on windows 98memberPaul S. Vickery1:46 19 May '05  
GeneralRe: Does NOT word on windows 98memberPaul S. Vickery23:45 19 May '05  
GeneralThis fix olso helps for NTmemberDieter Hammer5:55 11 Aug '05  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 6 Oct 2008
Editor: Deeksha Shenoy
Copyright 2005 by Paul S. Vickery
Everything else Copyright © CodeProject, 1999-2008
Web17 | Advertise on the Code Project