Click here to Skip to main content
15,881,856 members
Articles / Desktop Programming / MFC
Article

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 20013 min read 257.8K   3.5K   50   49
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! :) ) 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: www.codeproject.com="" 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

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… :) )

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


Written By
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

 
GeneralRe: Problems with building in vc7 on XP, and then running on win98 Pin
João Filipe de Castro Ferreira23-Jan-03 4:05
João Filipe de Castro Ferreira23-Jan-03 4:05 
GeneralSorting Pin
Jared Allen12-Nov-02 18:24
Jared Allen12-Nov-02 18:24 
GeneralRe: Sorting Pin
João Filipe de Castro Ferreira26-Nov-02 3:37
João Filipe de Castro Ferreira26-Nov-02 3:37 
GeneralRe: Sorting Pin
CanopenR1-Nov-05 10:00
CanopenR1-Nov-05 10:00 
Generalthanks man, great class Pin
1-Mar-02 16:07
suss1-Mar-02 16:07 
GeneralRe: thanks man, great class Pin
João Filipe de Castro Ferreira3-May-02 5:59
João Filipe de Castro Ferreira3-May-02 5:59 
GeneralRe: thanks man, great class Pin
Joao Vaz3-May-02 6:09
Joao Vaz3-May-02 6:09 
Generalsolution so you can return TRUE Pin
25-Feb-02 22:05
suss25-Feb-02 22:05 
//Just change the code using this.
// remember set pTI->uId to an unique value (not equal to 0 (zero) for each item in OnToolHitTest

if (nID==0) return TRUE; ///important. by this you block the automatic tooltip of the control (this one interfears with your tooltip causing the disapearance).

if( nFlags & TVHT_ONITEMICON) ///or something else
{
strTipText=GetItemText ( nID-1, 0 );

//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 TRUE; // now returning TRUE as supposed
}
return FALSE;
GeneralRe: solution so you can return TRUE Pin
João Filipe de Castro Ferreira30-Apr-02 1:07
João Filipe de Castro Ferreira30-Apr-02 1:07 
GeneralWhat's nID ? Pin
#Clitte21-Jan-04 2:12
#Clitte21-Jan-04 2:12 
GeneralCorrect a memory problem ! Pin
Mohamed KHADRAOUI4-Feb-02 21:07
Mohamed KHADRAOUI4-Feb-02 21:07 
GeneralRe: Correct a memory problem ! Pin
João Filipe de Castro Ferreira5-Feb-02 4:27
João Filipe de Castro Ferreira5-Feb-02 4:27 
GeneralRe: Correct a memory problem ! Pin
Alepac2-Jul-06 23:56
Alepac2-Jul-06 23:56 
General*IMPORTANT* - Temporarily broken links Pin
João Filipe de Castro Ferreira8-Nov-01 10:36
João Filipe de Castro Ferreira8-Nov-01 10:36 
GeneralBroken links Pin
PJ Arends8-Nov-01 9:11
professionalPJ Arends8-Nov-01 9:11 
GeneralRe: Broken links Pin
João Filipe de Castro Ferreira8-Nov-01 9:20
João Filipe de Castro Ferreira8-Nov-01 9:20 
GeneralRe: Broken links Pin
PJ Arends8-Nov-01 9:31
professionalPJ Arends8-Nov-01 9:31 

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.