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

  • 10 Rules that Make or Break Enterprise App Development Projects In today's app-driven world, application development is a top priority. Even so, 68% of enterprise application delivery projects fail. Designing and building applications that pay for themselves and adapt to future needs is incredibly difficult. Executing one successful project is lucky, but making it a repeatable process and strategic advantage? That's where the money is. With help from our most experienced project leads and software engineers, …

  • The first phase of API management was about realizing the business value of APIs. This next wave of API management enables the hyper-connected enterprise to drive and scale their businesses as API models become more complex and sophisticated. Today, real world product launches begin with an API program and strategy in mind. This API-first approach to development will only continue to increase, driven by an increasingly interconnected web of devices, organizations, and people. To support this rapid growth, …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds