Download demo project - 23 Kb
Download source files - 5 Kb
<!-- Article Starts -->
A titletip is similar to a tooltip, but is used to
display data in a control that is otherwise not large enough to display the full
text. We are all used to seeing cell titletips that display contents that are hidden by
the width of the cell. These titletips typically use a single line. If the contents are
greater than the single line, they were truncated.
The code presents a titletip class that can show the
complete contents of a cell regardless of its size. The titletip window will
adjust itself up or down depending on the cell being displayed, and will take
into account the size of the parent's client area, and that of the screen. It will do its utmost to show you that data!
This article is based on code written by Zafir
Anjum, but has been extensively reworked to add a great deal of new
How to use CTitleTip
Using the class is extremely easy. Just declare an object of type
CTitleTip in your main windows' header, and then create the
title tip window in your dialog's
OnInitDialog, or your
class CTitleTipDemoDlg : public CDialog
return TRUE; }
You then need to add a mouse over handler so that when the mouse cursor
moves over controls that may contain large amounts of text, you can popup
a title tip to display that text. For instance, in our demo dialog we have
a static control (
m_staticText) that displays a lot of text.
Just add an
OnMouseMove handler and add the following code:
void CTitleTipDemoDlg::OnMouseMove(UINT nFlags, CPoint point)
// Get the dimensions of the static control
// If the mouse is over the control, display the titletip.
// Note: The title tip will only be displayed if the text
// in the static control is too large for the static control
m_TitleTip.Show(rect, str, 0, 80);
CTitleTip::Show function has the following syntax
void Show(CRect rectTitle, LPCTSTR lpszTitleText, int xoffset = 0, int nMaxChars = -1, LPRECT lpHoverRect = NULL, LPLOGFONT lpLogFont = NULL)
If nMaxChars is less than 0, then the text will only wrap where there is insufficient room
to display the entire line.
if lpHoverRect is NULL then it will be set as rectTitle.
Chris is the Co-founder, Administrator, Architect, Chief Editor and Shameless Hack who wrote and runs The Code Project. He's been programming since 1988 while pretending to be, in various guises, an astrophysicist, mathematician, physicist, hydrologist, geomorphologist, defence intelligence researcher and then, when all that got a bit rough on the nerves, a web developer. He is a Microsoft Visual C++ MVP both globally and for Canada locally.
His programming experience includes C/C++, C#, SQL, MFC, ASP, ASP.NET, and far, far too much FORTRAN. He has worked on PocketPCs, AIX mainframes, Sun workstations, and a CRAY YMP C90 behemoth but finds notebooks take up less desk space.
He dodges, he weaves, and he never gets enough sleep. He is kind to small animals.
Chris was born and bred in Australia but splits his time between Toronto and Melbourne, depending on the weather. For relaxation he is into road cycling, snowboarding, rock climbing, and storm chasing.