Custom status messages and tooltips

By default MFC uses string resources with the same ID as the toolbar buttons for status messages and tooltip text.

Sometimes you might want to change some of these messages or texts in runtime. This requires a little work. The following explanations are written for MDI.

The easiest first; custom statusbar messages:

For this you must override the GetMessageString function of the CMDIFrameWnd. The following is an example:


void CMainFrame::GetMessageString(UINT nID, CString& rMessage) const
{
	// load appropriate string
	if(!m_bCustomMessage) {
		CFrameWnd::GetMessageString(nID, rMessage);
		return;
	}
	
	switch(nID) {
	case ID_LOOK_UP:
		rMessage = _T("Look out");
		break;
	case ID_LOOK_DOWN:
		rMessage = _T("Look down");
		break;
	default:
		CFrameWnd::GetMessageString(nID, rMessage);
	}
}

Instead of hardcoding the messages, other string resources than the default can be loaded and assigned. This above functionality works also for menus.

Now for something slightly more difficult; custom tooltip text. I had to dig a little for this. To override the default tooltip text handling two messages must be intercepted in the CMDIChildFrame, namely TTN_NEEDTEXTW and TTN_NEEDTEXTA. One of these (depending on unicode/not unicode) is sent when tooltip text is needed for a toolbar button.


BEGIN_MESSAGE_MAP(CMyChildFrame, CMDIChildWnd)
//{{AFX_MSG_MAP(CNetbasChildFrame)
ON_NOTIFY_EX_RANGE(TTN_NEEDTEXTW, 0, 0xFFFF, OnToolTipText)
ON_NOTIFY_EX_RANGE(TTN_NEEDTEXTA, 0, 0xFFFF, OnToolTipText)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMyChildFrame message handlers
BOOL CMyChildFrame::GetToolText( UINT nID, CString& strTipText )
{
	switch(nID) {
	case ID_LOOK_UP:
		strTipText = _T("Tip out");
		return TRUE;
	case ID_LOOK_DOWN:
		strTipText = _T("Tip down");
		return TRUE;
	}
	return FALSE;
}

#define _countof(array) (sizeof(array)/sizeof(array[0]))

BOOL CMyChildFrame::OnToolTipText(UINT nID, NMHDR* pNMHDR,
								  LRESULT*pResult)
{
	ASSERT(pNMHDR->code == TTN_NEEDTEXTA || pNMHDR->code == TTN_NEEDTEXTW);
	
	TOOLTIPTEXTA* pTTTA = (TOOLTIPTEXTA*)pNMHDR;
	TOOLTIPTEXTW* pTTTW = (TOOLTIPTEXTW*)pNMHDR;
	
	CString strTipText;
	if ( GetToolText( pNMHDR->idFrom, strTipText ) )
	{
#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
		return TRUE;
	}

	return CMDIChildWnd::OnToolTipText( nID, pNMHDR, pResult );
}

Here I have implemented a helper function that checks if we want to override the given button. In that case we assign a tooltip text. Otherwise we let the message pass through.



Comments

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

Top White Papers and Webcasts

  • Live Event Date: December 11, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT Market pressures to move more quickly and develop innovative applications are forcing organizations to rethink how they develop and release applications. The combination of public clouds and physical back-end infrastructures are a means to get applications out faster. However, these hybrid solutions complicate DevOps adoption, with application delivery pipelines that span across complex hybrid cloud and non-cloud environments. Check out this …

  • Due to internal controls and regulations, the amount of long term archival data is increasing every year. Since magnetic tape does not need to be periodically operated or connected to a power source, there will be no data loss because of performance degradation due to the drive actuator. Read this white paper to learn about a series of tests that determined magnetic tape is a reliable long-term storage solution for up to 30 years.

Most Popular Programming Stories

More for Developers

RSS Feeds