Change tooltips at runtime

This example demonstrates the various aspects of changing the text of the
tool tips at runtime. It is far from perfect but does the job and has been
stable in several application where I use it.

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.

Download Zip File


More by Author

Must Read