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

  • Java developers know that testing code changes can be a huge pain, and waiting for an application to redeploy after a code fix can take an eternity. Wouldn't it be great if you could see your code changes immediately, fine-tune, debug, explore and deploy code without waiting for ages? In this white paper, find out how that's possible with a Java plugin that drastically changes the way you develop, test and run Java applications. Discover the advantages of this plugin, and the changes you can expect to see …

  • Protecting business operations means shifting the priorities around availability from disaster recovery to business continuity. Enterprises are shifting their focus from recovery from a disaster to preventing the disaster in the first place. With this change in mindset, disaster recovery is no longer the first line of defense; the organizations with a smarter business continuity practice are less impacted when disasters strike. This SmartSelect will provide insight to help guide your enterprise toward better …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds