Click here to Skip to main content
14,690,613 members
Articles » Desktop Development » List Controls » List Controls
Posted 7 Nov 2001


50 bookmarked

CToolTipListCtrl - A CListCtrl derived class providing per SubItem tooltips

Rate me:
Please Sign up or sign in to vote.
4.50/5 (17 votes)
12 Nov 2001
A CListCtrl derived class providing per SubItem tooltips, adapted from Nate Maynard's CToolTipTreeCtrl

Sample Image - Snapshot.jpg


Once upon a time, I felt the need to use CListCtrls to display (obviously multi-column-line) mathematical matrices, most of which turned out to have explicitly as individual cell elements entities with physical real-world significance. This either meant that I should scorch for extra-space in my already cluttered form view to accommodate the more explicit, although redundant, data display, or try to find an ingenious alternative.

How about commenting each cell/SubItem with tool/info tips? I immediately remembered Matt Weagle's great article “Using the ListCtrl” that referred to the matter of using info tips through LVS_EX_INFOTIP. Unfortunately, as stated by the MSDN documentation, there is no way of doing this method with more granularity than to comment Items with info tips. I then thought about searching CodeProject for tool tip implementations and prayed to find something done on ListCtrl. Well, although I didn't find exactly what I was looking for, I did discover a great article by Nate Maynard (thanks! I owe you one! :) ) about a CTreeCtrl derived class providing per item tool tip support and thought “why not make my first humble contribution to CodeProject?”.

Well, here it is. Most of my class uses the ideas from Nate Maynard's article, so as to avoid redundancy (ok: I confess! To avoid losing to much time… :) ) I will only address the CListCtrl specifics in this article. Please refer to Mr Maynard's article for <a href="http:"" useritems="" ctooltiptreectrl.asp"="">further insights on the general issue.

How it works (specifically for CListCtrls)

The two major problems I had to deal with when planning my CToolTipListCtrl were the CListCtrl specific issues:

1. There are no handles for

SubItems, so I couldn't use
to map the ToolTip strings.

2. The right CListCtrl member to use for hit-testing the SubItems would be SubItemHitTest, which, due to OnToolHitTest being const, would had to have its use be re-evaluated in that particular override…

As for the first problem, I addressed it using CMapStringToString with its CString keys being formed by a simple algorithm: (Item * 100 + SubItem). Not very elegant, but it does solve the problem for list controls with less than 100 SubItems per Item (I hope that's not your case… :) )

The second was a little trickier (or at least it seemed so at first) but then I stumbled through the ListView_SubItemHitTest macro in MSDN which solved my problem.

The only other thing you must be aware of is that the CListCtrl masks differ from the CTreeCtrl‘s on the prefix, which in the earlier must be LVHT_.

The functions

I didn't have much time to override and overload as many base class members as Nate Maynard, but the one's I did I hope (at least for me they did) will do the job. They were

  • virtual BOOL
    DeleteAllItems( )
  • virtual BOOL DeleteItem( int nItem )

All the other added functions do the same things as their CToolTipTreeCtrl counterparts, of course having their own adapted arguments and returns. They were

  • WORD
    SetToolTipHitMask(WORD wHitMask)
  • virtual BOOL
    DeleteItem( int nItem )
  • virtual void 
  • virtual BOOL SetItemToolTipText( int nItem,
    int nSubItem, LPCTSTR lpszToolTipText )
  • virtual CString GetItemToolTipText( int
    nItem, int nSubItem )

One last remark: I have added a DeleteAllToolTips statement in the class' destructor so as to avoid memory leakages by not cleaning up.


There you have it, and it works! But is this the end of it?

I think not. You see, there are some unanswered questions. For example, check the note I left on this code-snippet:

         //Deal with UNICODE
      #ifndef _UNICODE
              if (pNMHDR->code == TTN_NEEDTEXTA)
                    lstrcpyn(pTTTA->szText, strTipText, 80);
                     _mbstowcsz(pTTTW->szText, strTipText, 80);
              if (pNMHDR->code == TTN_NEEDTEXTA)
                     _wcstombsz(pTTTA->szText, strTipText, 80);
                     lstrcpyn(pTTTW->szText, strTipText, 80);
         return FALSE;    //We found a tool tip,
                         //tell the framework this message has been handled
// ****** Special note *****
// Still don't understand why the function must return FALSE for CListCtrl
// so as not to cause flickering, as opposed to Nate Maynard's derivation
// from CTreeCtrl.
// I have experimented with disabling Tooltips for the control
// and found out that a "ghost" tooltip appears for a fraction of a second...
// I am completely at a loss...
// Seems to work, though...

   return FALSE; //We didn't handle the message,
                 //let the framework continue propagating the message

Why was I forced to return FALSE on the OnToolTipText override, even when the function did find the string? I would appreciate some enlightening, anyone…

As usual, “if you have any suggestions, find errors, or if you just find it useful, please feel free to drop me a line”.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

João Filipe de Castro Ferreira
Web Developer
Portugal Portugal
MSc student in Automation and Robotics (Computer Vision) at the Electrical and Computer Engineering Department of the University of Coimbra, Portugal.
Also teaching Computer Technologies (Digital Electronics) at the same University.

Comments and Discussions

Questionjust thanks Pin
Senol Van7-Jan-14 22:14
professionalSenol Van7-Jan-14 22:14 
GeneralMy vote of 5 Pin
shepher_whu1-Aug-13 19:35
Membershepher_whu1-Aug-13 19:35 
GeneralMy vote of 5 Pin
zhangxianzhe3-May-11 1:16
Memberzhangxianzhe3-May-11 1:16 
QuestionLimit in the string? Pin
MrNorth275-Mar-08 5:16
MemberMrNorth275-Mar-08 5:16 
AnswerRe: Limit in the string? Pin
spincc11-Aug-08 0:57
Memberspincc11-Aug-08 0:57 
GeneralTab character in text cuts the tooltip. Pin
Moti Werheimer25-Dec-07 23:03
MemberMoti Werheimer25-Dec-07 23:03 
GeneralRe: Tab character in text cuts the tooltip. Pin
Steve Thresher22-Jan-09 12:17
MemberSteve Thresher22-Jan-09 12:17 
GeneralRe: Tab character in text cuts the tooltip. Pin
Moti Werheimer24-Jan-09 8:06
MemberMoti Werheimer24-Jan-09 8:06 
GeneralRe: Tab character in text cuts the tooltip. Pin
Igor Jerosimic2-Aug-09 0:07
MemberIgor Jerosimic2-Aug-09 0:07 
GeneralMultiline ToolTip Pin
Sangeetha_J16-Aug-07 4:17
MemberSangeetha_J16-Aug-07 4:17 
GeneralRe: Multiline ToolTip Pin
sps-itsec463-Sep-07 22:20
Membersps-itsec463-Sep-07 22:20 
QuestionAbout DeleteItem (multy columns list) & UNICODE Pin
Yael80011-Apr-07 9:22
MemberYael80011-Apr-07 9:22 
AnswerRe: About DeleteItem (multy columns list) & UNICODE Pin
l.xie18-Dec-08 23:41
Memberl.xie18-Dec-08 23:41 
Generalthanks! Pin
saygogogo9-Apr-07 15:52
Membersaygogogo9-Apr-07 15:52 
Generalif i want deleting the Items in Listbox with delete button on keyboard Pin
Ing.Raiz8228-Nov-06 6:18
MemberIng.Raiz8228-Nov-06 6:18 
QuestionCan I shorten the show-up duration of tooltip ? [modified] Pin
WHRIA14-Aug-06 6:54
MemberWHRIA14-Aug-06 6:54 
GeneralAnother method for the key Pin
Bill Perkins22-Jun-06 14:51
MemberBill Perkins22-Jun-06 14:51 
GeneralToolTip vs RightClick Pin
stonemind21-Mar-06 18:36
Memberstonemind21-Mar-06 18:36 
GeneralA critical crash problem on win98 Pin
daveice16-Aug-05 20:58
Memberdaveice16-Aug-05 20:58 
GeneralRe: A critical crash problem on win98 Pin
Anna-Jayne Metcalfe15-Feb-06 5:04
MemberAnna-Jayne Metcalfe15-Feb-06 5:04 
GeneralMultiline Tooltip in the list control Pin
jouwpaard23-Jun-05 4:31
Memberjouwpaard23-Jun-05 4:31 
GeneralRe: Multiline Tooltip in the list control Pin
daveice16-Aug-05 20:50
Memberdaveice16-Aug-05 20:50 
GeneralRe: Multiline Tooltip in the list control Pin
CanopenR1-Nov-05 10:57
MemberCanopenR1-Nov-05 10:57 
GeneralRe: Multiline Tooltip in the list control Pin
jfchen4-Dec-05 15:35
Memberjfchen4-Dec-05 15:35 
GeneralSimple problem, please help Pin
haiaw13-Sep-04 0:43
Memberhaiaw13-Sep-04 0:43 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.