Change tooltips at runtime
Posted
by Hans Wedemeyer
on August 5th, 1998
Need to override the OnToolTipText()
virtual afx_msg BOOL OnToolTipText(UINT nID, NMHDR* pNMHDR, LRESULT* pResult );Add message handlers for
ON_NOTIFY_EX_RANGE( TTN_NEEDTEXTW, 0, 0xFFFF, OnToolTipText)
ON_NOTIFY_EX_RANGE( TTN_NEEDTEXTA, 0, 0xFFFF, OnToolTipText)
This is the handler.Also does UNICODE.
BOOL CToolTipView::OnToolTipText(UINT nID, NMHDR* pNMHDR, LRESULT* pResult )
{
ASSERT ( pNMHDR->code == TTN_NEEDTEXTA || TTN_NEEDTEXTW );
TOOLTIPTEXTA* pTTTA = ( TOOLTIPTEXTA *)pNMHDR;
TOOLTIPTEXTW* pTTTW = ( TOOLTIPTEXTW *)pNMHDR;
CString strTipText;
CString strMessage;
if( GetToolText(pNMHDR->idFrom, strTipText, strMessage))
{
#ifndef _UNICODE
if(pNMHDR->code == TTN_NEEDTEXTA)
lstrcpyn(pTTTA->szText,strTipText,_countof(pTTTA->szText));
else
_mbstowcsz(pTTTW->szText,strTipText,_countof(pTTTW->szText));
#else
if(pNMHDR->code == TTN_NEEDTEXTA)
_wcstombsz(pTTTA->szText,strTipText,_countof(pTTTA->szText));
else
lstrcpyn(pTTTW->szText,strTipText,_countof(pTTTW->szText));
#endif
// change status bar message here, need a pointer to CMainFrame
// SetMessageText(strMessage);
// ok we handled the message, return TRUE
return TRUE;
}
// we did NOT handle the message, pass it on, this is important, don't miss it!
return CToolTipView::OnToolTipText(nID,pNMHDR,pResult);
}
Here we do the actual work of changing the text. Each Item must be handled and can be done by a switch.
BOOL CToolTipView::GetToolText( UINT nID, CString& strTipText, CString& /*strMessage*)
{
CString strFullString;
switch( nID )
{
case ID_FILE_NEW: // have to handle all toolbar tool tip messages here
case ID_FILE_OPEN:
case ID_FILE_SAVE:
case ID_EDIT_CUT:
case ID_EDIT_COPY:
case ID_EDIT_PASTE:
case ID_FILE_PRINT:
case ID_APP_ABOUT:
if (strFullString.LoadString(nID))
AfxExtractSubString ( strTipText, strFullString, 1,'\n');
break;
// demonstrates how to change the tool tip for a specific button
case ID_CHANGE_TOOL_TIP:
// local CString containg the new text, which you can set anytime at runtime
// and will be displayed here when the user puts the mouse on the button.
strTipText = m_strNewToolTipText;
// if the user tried to enter a zero legth string, it could
// cause confusion, so put the default sring back in
if ( strTipText.GetLength() == 0 ) // then put back the default string
{
if (strFullString.LoadString (ID_CHANGE_TOOL_TIP))
AfxExtractSubString ( strTipText, strFullString, 1,'\n' );
}
break;
}
return TRUE;
}
A Note!
I wrote this code with the help of these mfc-l members about a year ago.Michael Hupp, Kostya Sebov, Tim Hodgson, Thibault Mangold, Dicky Singh, David Little and David Elliot.

Comments
C4244 Warning
Posted by mfcbug on 05/29/2006 03:50amHi Hans, I am getting following warning while calling function GetToolText(pNMHDR->idFrom, strTipText) warning C4244: 'argument' : conversion from 'UINT_PTR' to 'UINT', possible loss of data. I am running it on WinXP and VS.NET. Regards Abhi
ReplyBoh^!^!
Posted by Legacy on 02/10/2004 12:00amOriginally posted by: Andrea
Io 'un c'ho capito una sega! Boh.. fate vobis!
Reply
I like the way U did this, I can use this.
Posted by Legacy on 09/18/2002 12:00amOriginally posted by: Conrad Braam
I was wondering if writting a little stip function like U did that actually looked at the ID anddecided at runtime what to do was the only way, this is even better since U used a member variable, thus saving the problems with doing a LoadString here at runtime.
ReplyCool.