Avatar of JP_Goblet
JP_Goblet

asked on 

display tooltips on CListCtrl items

< NOTE : this a repost of a question that I posted earlier on 18/6; the first question got the status PAQ, but in fact it was not answered >

I have written a CListBoxEx class, derived from CListCtrl. It's designed as a replacement for the standard CListBox, very easy to use, but with all the extras of CListCtrl, if needed.
One of the extras I want is to display tooltips giving additionnal information on the item under the mouse cursor. I found a way, it works fine under Windows 95, BUT NOT UNDER WINDOWS NT (no tooltip appears under NT).
I'm a lazy programmer, I don't have the time and even less the desire to learn all the low level subtilities of Windows event routing, experts are there for such things ... please give me quick (& dirty) advice, I pay cash !

Below are extracts of my code concerning tooltips :

////// code in a dialogbox using a CListBoxEx listbox

BOOL CSAPDlg::OnInitDialog()
{
// creation of a CToolTipCtrl control :
m_toolTip.Create( this, TTS_ALWAYSTIP );
m_toolTip.Activate(TRUE);
...
// registering the tooltip in the m_ListSendLic listbox, which is a CListBoxEx :
m_ListSendLic.RegisterTooltip   (&m_toolTip, this, IDC_LIST_SENDLIC);
...
}

BOOL CSAPDlg::OnToolTipNeedText( UINT id, NMHDR* pNMHDR, LRESULT* pResult )
{
TOOLTIPTEXT *pTTT = (TOOLTIPTEXT *)pNMHDR;    
if (! (pTTT->uFlags & TTF_IDISHWND) )    
       switch( pNMHDR->idFrom)
// here get the current item for the listbox identified by pNMHDR->idFrom,
// and fill the tooltip's text buffer with relevant information for this item.
}

////// code in the CListBoxEx class

BEGIN_MESSAGE_MAP(CListBoxEx, CListCtrl)
 //{{AFX_MSG_MAP(CListBoxEx)
 ON_WM_MOUSEMOVE()
 ON_NOTIFY_EX( TTN_NEEDTEXT, 0, OnToolTipNeedText )
 //}}AFX_MSG_MAP
END_MESSAGE_MAP()

void CListBoxEx::RegisterTooltip (CToolTipCtrl *pToolTip, CWnd *pParentWnd, long ID)
{
 m_pToolTip = pToolTip;
 m_pParentWnd = pParentWnd;
 RECT rect;
 m_toolTipID = ID;
 pToolTip->AddTool( this,  LPSTR_TEXTCALLBACK, &rect, ID);
}

void CListBoxEx::OnMouseMove(UINT  nFlags, CPoint point)
{
 CListCtrl::OnMouseMove(nFlags, point);
 int index = HitTest(point);
 if (index != m_MouseIndex)
 // if item under cursor has changed since last call :
 {
  m_MouseIndex = index;
  if (m_pToolTip)
  {
   RECT rect;
   GetItemRect( index, &rect, LVIR_BOUNDS); // rect = rectangle surrounding item under cursor
   m_pToolTip->SetToolRect( this, m_toolTipID, &rect);// new rectangle for the tooltip control --> force call to OnToolTipNeedText
  }
 }
}

BOOL CListBoxEx::PreTranslateMessage(MSG* pMsg)
{
 switch( pMsg->message )
  {
   case WM_LBUTTONDOWN:
   case WM_MOUSEMOVE:
   case WM_LBUTTONUP:
   case WM_RBUTTONDOWN:
   case WM_RBUTTONUP:
   case WM_MBUTTONDOWN:
   case WM_MBUTTONUP:
    if (m_pToolTip)
     m_pToolTip->RelayEvent(pMsg);
    break;
  }
 return CListCtrl::PreTranslateMessage(pMsg);
}

BOOL CListBoxEx::OnToolTipNeedText( UINT id, NMHDR* pNMHDR, LRESULT* pResult )
{
 if (m_pParentWnd)
  m_pParentWnd->SendMessage(WM_NOTIFY, 0, (long) pNMHDR);
 return TRUE;
}
System Programming

Avatar of undefined
Last Comment
ShaunWilde
Avatar of Carel
Carel

Checking but in the mean time, the toolbar has a similar problem. The solution there was to use another CWnd as the receiver than the toolbar itself. It had to do with overlapping ids and common controls versionitis.
ASKER CERTIFIED SOLUTION
Avatar of ShaunWilde
ShaunWilde

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of JP_Goblet
JP_Goblet

ASKER

I found the solution on http://codeguru.earthweb.com/listview/cell_tooltip.shtml

The trick was to replace in
ON_NOTIFY_EX( TTN_NEEDTEXT, 0, OnToolTipNeedText
by these 2 lines :
ON_NOTIFY_EX_RANGE(TTN_NEEDTEXTW, 0, 0xFFFF, OnToolTipNeedText)
ON_NOTIFY_EX_RANGE(TTN_NEEDTEXTA, 0, 0xFFFF, OnToolTipNeedText)

When I grow as an expert I will try to understand this sort of things...

There was still a problem : under Windows NT, the string I put in the tooltîp buffer was ##!**ed.
Once again I found the solution on the same site (), it has to do with wide characters strings used by NT.

Thanks for your help, specially to Carel.
Avatar of JP_Goblet
JP_Goblet

ASKER

I my previous comment I forgot to give  the link giving a solution for the problem of wide charecters strings :

http://codeguru.earthweb.com/mfc/comments/14244.shtml

As I have answered this question myself, your comments are still welcome but how can I change this question as a PAQ ?
Avatar of Carel
Carel

Give some points to Shaun, he pointed you to a nice example! That should close this question.
Avatar of JP_Goblet
JP_Goblet

ASKER

Adjusted points from 0 to 5
Avatar of JP_Goblet
JP_Goblet

ASKER

Comment accepted as answer
Avatar of JP_Goblet
JP_Goblet

ASKER

As your suggestion lead me to the solution, I'm very happy to bombard you with a grant total of 5 points !
(sorry, that's really all that's remaining in my stock)
Avatar of ShaunWilde
ShaunWilde

Ta :) - but it went to 15 with the B :)
System Programming
System Programming

Kernel and system programming is the process of creating the software necessary for a computer or device to function and operate other programs. Some operating systems (such as Microsoft Windows) are proprietary, but others, such as the various Linux distributions, are open source.

41K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo