< 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;
}