Change tooltips at runtime

WEBINAR: On-demand webcast

How to Boost Database Development Productivity on Linux, Docker, and Kubernetes with Microsoft SQL Server 2017 REGISTER >

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



Comments

  • C4244 Warning

    Posted by mfcbug on 05/29/2006 03:50am

    Hi 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

    Reply
  • Boh^!^!

    Posted by Legacy on 02/10/2004 12:00am

    Originally 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:00am

    Originally 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.
    Cool.

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • As all sorts of data becomes available for storage, analysis and retrieval - so called 'Big Data' - there are potentially huge benefits, but equally huge challenges...
  • The agile organization needs knowledge to act on, quickly and effectively. Though many organizations are clamouring for "Big Data", not nearly as many know what to do with it...
  • Cloud-based integration solutions can be confusing. Adding to the confusion are the multiple ways IT departments can deliver such integration...

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date