Click here to Skip to main content
Click here to Skip to main content

CToolTipListCtrl - A CListCtrl derived class providing per SubItem tooltips

, 12 Nov 2001
Rate this:
Please Sign up or sign in to vote.
A CListCtrl derived class providing per SubItem tooltips, adapted from Nate Maynard's CToolTipTreeCtrl

Sample Image - Snapshot.jpg

Introduction

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! Smile | :) ) 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… Smile | :) ) I will only address the CListCtrl specifics in this article. Please refer to Mr Maynard's article for 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 CListCtrl SubItems, so I couldn't use CMap 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… Smile | :) )

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 DeleteAllToolTips()
  • 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.

Conclusion

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);
              else
                     _mbstowcsz(pTTTW->szText, strTipText, 80);
      #else
              if (pNMHDR->code == TTN_NEEDTEXTA)
                     _wcstombsz(pTTTA->szText, strTipText, 80);
              else
                     lstrcpyn(pTTTW->szText, strTipText, 80);
      #endif
         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”.

License

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 PinmemberMember 105101177-Jan-14 21:14 
GeneralMy vote of 5 Pinmembershepher_whu1-Aug-13 18:35 
GeneralMy vote of 5 Pinmemberzhangxianzhe3-May-11 0:16 
QuestionLimit in the string? PinmemberMrNorth275-Mar-08 4:16 
AnswerRe: Limit in the string? Pinmemberspincc10-Aug-08 23:57 
GeneralTab character in text cuts the tooltip. Pinmembermoti wertheimer25-Dec-07 22:03 
GeneralRe: Tab character in text cuts the tooltip. PinmemberSteve Thresher22-Jan-09 11:17 
GeneralRe: Tab character in text cuts the tooltip. Pinmembermoti wertheimer24-Jan-09 7:06 
GeneralRe: Tab character in text cuts the tooltip. PinmemberIgor Jerosimic1-Aug-09 23:07 
GeneralMultiline ToolTip PinmemberSangeetha_J16-Aug-07 3:17 
GeneralRe: Multiline ToolTip Pinmembermykel3-Sep-07 21:20 
QuestionAbout DeleteItem (multy columns list) & UNICODE PinmemberYael80011-Apr-07 8:22 
AnswerRe: About DeleteItem (multy columns list) & UNICODE Pinmemberxie.liang18-Dec-08 22:41 
Generalthanks! Pinmembersaygogogo9-Apr-07 14:52 
Generalif i want deleting the Items in Listbox with delete button on keyboard PinmemberIng.Raiz8228-Nov-06 5:18 
QuestionCan I shorten the show-up duration of tooltip ? [modified] PinmemberWHRIA14-Aug-06 5:54 
GeneralAnother method for the key Pinmemberbpzander22-Jun-06 13:51 
GeneralToolTip vs RightClick Pinmemberstonemind21-Mar-06 17:36 
GeneralA critical crash problem on win98 Pinmemberdaveice16-Aug-05 19:58 
GeneralRe: A critical crash problem on win98 PinmemberAnna-Jayne Metcalfe15-Feb-06 4:04 
GeneralMultiline Tooltip in the list control PinmemberKenji Horvath23-Jun-05 3:31 
GeneralRe: Multiline Tooltip in the list control Pinmemberdaveice16-Aug-05 19:50 
GeneralRe: Multiline Tooltip in the list control PinmemberCanopenR1-Nov-05 9:57 
GeneralRe: Multiline Tooltip in the list control Pinmemberjfchen4-Dec-05 14:35 
GeneralSimple problem, please help Pinmemberhaiaw12-Sep-04 23:43 

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

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

| Advertise | Privacy | Mobile
Web02 | 2.8.140721.1 | Last Updated 13 Nov 2001
Article Copyright 2001 by João Filipe de Castro Ferreira
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid