CListView tooltips

bree2474
bree2474 used Ask the Experts™
on
I have some classes that are derived from CListView.  I use the report mode to display items.  I would like to display additional information about the items when the mouse passes over the item text.  Can someone get me started?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Author

Commented:
Okay, assuming these solutions work, both solutions involve subclassing CListCtrl.  The classes that I'm working with are subclasses of CListView, which already has a CListCtrl.  

Commented:
I had this on my list of stuff to do for a little app I'm writing, so I've been giving it a shot, without any luck.

I thought the following was going to do it, assuming that the CListView is the parent of the list control:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccore98/HTML/_core_tool_tips.asp

It doesn't work though, and there's a clue as to why in the CListView documentation, I think:
"The view is the control, cast to CListView"

Hmm, then you might think that the frame window would get the tooltext notification, but no luck there either.  

I'll be keeping a close eye on this one - please post a solution if you find it   ---   Jim
Bootstrap 4: Exploring New Features

Learn how to use and navigate the new features included in Bootstrap 4, the most popular HTML, CSS, and JavaScript framework for developing responsive, mobile-first websites.

Author

Commented:
Well Jim, I figured it out on my own.  If I get a chance I will post a solution tommorow.  It turned out to be fairly simple.

Author

Commented:
Ok, here's what you have to do:

1.  add a CToolTipCtrl member to your CListView class:
   CToolTipCtrl m_tooltip;

2.  in your OnInitialUpdate override, you need to create the control:
void CMyListView::OnInitialUpdate()
{
     CListView::OnInitialUpdate();    
     m_tooltip.Create( this );    
}

3.  Relay messages to the control, it will handle the ones that apply to it.  The if condition is necessary because messages may be sent before the control is created, which causes an Assertion to fail:
BOOL CMyListView::PreTranslateMessage(MSG* pMsg)
{
     if(m_tooltip)
          m_tooltip.RelayEvent( pMsg );
     return CListView::PreTranslateMessage(pMsg);
}

4.  Each time you insert an item, create a tool for it. GetItemRect gets the bounding Rectangle of the item (the option I used is for the label only). Once we have the rectangle, we can create the tool.  The last parameter to add tool is the tool id, which must be >0, so I used i+1:

Rect         rect;
CListCtrl&   theCtrl;
int          nItemsToCreate=???;

      for(int i=0; i<nItemsToCreate; i++)
      {      
         theCtrl.InsertItem(i, _T("item label"));
      theCtrl.GetItemRect(i, &rect, LVIR_LABEL);
      m_tooltip.AddTool(&theCtrl, _T("tool tip text"), &rect, i+1);
      }

5. Now you're tools have created and should work, but you still have some work to do if you sort your items on the click of a column header.  I wrote a helper function which I call at the end of the OnColumnClick message handler.  I called it ResetToolTips:
CMyListView::ResetToolTips()
{
CListCtrl&     theCtrl= GetListCtrl();
CString          sTip;
int          count= theCtrl.GetItemCount();

     for(int i=0; i<count; i++)
     {
           sTip= ???;
          m_tooltip.UpdateTipText(sTip,&theCtrl, i+1);
     }
}

I hope the solution to my own question is helpful.  Feel free to throw a few points my way if it does.  

Commented:
Ha ha, very good.  Thanks for the answer - I saw this class and suspected we were going to have to use it - the code will be cut-and-paste.
I'll be happy to throw some points your way - um, how do I do that?  Wait, that's another question!

Author

Commented:
Here's what I've seen people do:

Post a question for as many points as you want to offer me.
Make the title "Question for bree2474".  The question can say "points for you", or whatever you want it to say.  I'll see it, post a comment.  You accept the comment as an answer and I get the points.  Or even better, you can let me know if you got it to work in the body of your question.  If you had problems getting it to work, you can ask me about it there.

Brian

Commented:
I'll give it a try when I get home.

Commented:
Works great.  One more comment; I do a wholesale repopulation of my list, so I need to remove the existing tools; otherwise empty regions can still show tips:

int i = GetListCtrl().GetItemCount();
while(i > 0) {
     m_ToolTip.DelTool(&GetListCtrl(), i);
     i--;
}
GetListCtrl().DeleteAllItems();

I'll submit a question for you in this category, Brian.

Author

Commented:
Yeah, if you're deleting the items it makes sense to delete the tools too.  Just so you know for future reference, if you set the tooltip text to an empty string (i.e. _T("") ), it will not display the tooltip at all.  

Commented:
Dear expert(s),

A request has been made to delete this Q in CS:
http://www.experts-exchange.com/Community_Support/CleanUp/Q_20405814.html

Without a response in 72 hrs, a moderator will finalize this question by:

 - Saving this Q as a PAQ and refunding the points to the questionner

When you agree or disagree, please add a comment here.

Thank you.

modulo

Community Support Moderator
Experts Exchange
Commented:
Saving this Q as a PAQ and refunding the points to the questionner

modulo

Community Support Moderator
Experts Exchange

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial