RTF-Based Tooltips | CodeGuru

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 […]

Written By
CodeGuru Staff
CodeGuru Staff
Apr 22, 1999
1 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

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

  • CodeGuru Logo

    CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

    Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

    Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.