Click here to Skip to main content
15,886,033 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.9K   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

 
Questionjust thanks Pin
Senol Van7-Jan-14 21:14
professionalSenol Van7-Jan-14 21:14 
GeneralMy vote of 5 Pin
shepher_whu1-Aug-13 18:35
shepher_whu1-Aug-13 18:35 
GeneralMy vote of 5 Pin
zhangxianzhe3-May-11 0:16
zhangxianzhe3-May-11 0:16 
QuestionLimit in the string? Pin
MrNorth275-Mar-08 4:16
MrNorth275-Mar-08 4:16 
AnswerRe: Limit in the string? Pin
spincc10-Aug-08 23:57
spincc10-Aug-08 23:57 
Using OnToolTipText function, you can indicate a string point to TOOLTIPTEXT->lpszText, the length of text is unlimited. Smile | :)
GeneralTab character in text cuts the tooltip. Pin
Moti Werheimer25-Dec-07 22:03
Moti Werheimer25-Dec-07 22:03 
GeneralRe: Tab character in text cuts the tooltip. Pin
Steve Thresher22-Jan-09 11:17
Steve Thresher22-Jan-09 11:17 
GeneralRe: Tab character in text cuts the tooltip. Pin
Moti Werheimer24-Jan-09 7:06
Moti Werheimer24-Jan-09 7:06 
GeneralRe: Tab character in text cuts the tooltip. Pin
Igor Jerosimic1-Aug-09 23:07
Igor Jerosimic1-Aug-09 23:07 
GeneralMultiline ToolTip Pin
Sangeetha_J16-Aug-07 3:17
Sangeetha_J16-Aug-07 3:17 
GeneralRe: Multiline ToolTip Pin
sps-itsec463-Sep-07 21:20
sps-itsec463-Sep-07 21:20 
QuestionAbout DeleteItem (multy columns list) & UNICODE Pin
Yael80011-Apr-07 8:22
Yael80011-Apr-07 8:22 
AnswerRe: About DeleteItem (multy columns list) & UNICODE Pin
l.xie18-Dec-08 22:41
l.xie18-Dec-08 22:41 
Generalthanks! Pin
saygogogo9-Apr-07 14:52
saygogogo9-Apr-07 14:52 
Generalif i want deleting the Items in Listbox with delete button on keyboard Pin
Ing.Raiz8228-Nov-06 5:18
Ing.Raiz8228-Nov-06 5:18 
QuestionCan I shorten the show-up duration of tooltip ? [modified] Pin
WHRIA14-Aug-06 5:54
WHRIA14-Aug-06 5:54 
GeneralAnother method for the key Pin
Bill Perkins22-Jun-06 13:51
Bill Perkins22-Jun-06 13:51 
GeneralToolTip vs RightClick Pin
stonemind21-Mar-06 17:36
stonemind21-Mar-06 17:36 
GeneralA critical crash problem on win98 Pin
daveice16-Aug-05 19:58
daveice16-Aug-05 19:58 
GeneralRe: A critical crash problem on win98 Pin
Anna-Jayne Metcalfe15-Feb-06 4:04
Anna-Jayne Metcalfe15-Feb-06 4:04 
GeneralMultiline Tooltip in the list control Pin
jouwpaard23-Jun-05 3:31
jouwpaard23-Jun-05 3:31 
GeneralRe: Multiline Tooltip in the list control Pin
daveice16-Aug-05 19:50
daveice16-Aug-05 19:50 
GeneralRe: Multiline Tooltip in the list control Pin
CanopenR1-Nov-05 9:57
CanopenR1-Nov-05 9:57 
GeneralRe: Multiline Tooltip in the list control Pin
jfchen4-Dec-05 14:35
jfchen4-Dec-05 14:35 
GeneralSimple problem, please help Pin
haiaw12-Sep-04 23:43
haiaw12-Sep-04 23: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.