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:
- Devise my own markup, and parse it.
- Accept and parse RTF or HTML.
- 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