RTF-Based Tooltips

This code demonstrates using rich text in tooltips, as seen in Query Analyser utility of MS SQL Server 7.


It is more like a proof-of-concept then a finished code, and is heavily based on two classes from CodeGuru :
TFXDataTip by S. Wilson and CAutoRichEditCtrl by Jeremy Iverson

First class was modifyed to hold a hidden AutoRichEdit control as a static member and use it for rendering the
RTF text to the window. Function of AutoRichEdit are used to load text into the control.

Most of the magic behind the code is in the OnPaint handler of the tooltip window. The idea is to utilise
RichEdit printing functions to copy the picture to screen DC instead of printer.


void TFXDataTip::OnPaint()
{
CPaintDC dc(this); // device context for painting

CRect rect;
GetClientRect(&rect);

dc.SetBkColor(::GetSysColor(COLOR_INFOBK));
//dc.SetTextColor(::GetSysColor(COLOR_INFOTEXT));
//dc.SetMapMode(MM_TEXT);

if(_richCtrl)
{
_richCtrl->SetRTF(m_tip);

FORMATRANGE fr;
fr.hdc = dc;
fr.hdcTarget = dc;
fr.rc = CRect((rect.Width( ) / 2), (_border / 2) ,m_size.cx*15, m_size.cy*15);
fr.rcPage = CRect(0,0,0,0);
fr.chrg.cpMin = 0;
fr.chrg.cpMax = -1;

_richCtrl->FormatRange( &fr );
}

}

Using it from the dialog is also rather simple :


void CRich1Dlg::OnMouseMove(UINT nFlags, CPoint point)
{
CWnd *pwnd = ChildWindowFromPoint(point);
if(pwnd)
{
int nID = pwnd->GetDlgCtrlID();

switch(nID)
{
case IDC_RICH_PLACEHOLDER:
m_datatip.Set(point, m_tip1);
break;
case IDC_TEXT:
m_datatip.Set(point, m_tip2);
break;
case IDC_IMAGE_PLACEHOLDER:
m_datatip.Set(point, m_tip3);
break;
default:
break;
}
}
CDialog::OnMouseMove(nFlags, point);
}



Issues and possible enhancements :


  • More ‘standard’ tooltip behaviour, such as ability to assign text to control IDs etc.
  • Some framework for dynamically producing RTF based on a template could be nice
  • Images included in RTF files doesn’t show up.
  • Any explanation for ’15’ constant in rc member of a FORMATRANGE struct ? What units are used there ?

  • Possibly cache rendered text to avoid having to load it into RichEdit control every time.

    Download demo project – 10 KB

    Download source – 26 KB

  • More by Author

    Get the Free Newsletter!

    Subscribe to Developer Insider for top news, trends & analysis

    Must Read