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

  • Packaged application development teams frequently operate with limited testing environments due to time and labor constraints. By virtualizing the entire application stack, packaged application development teams can deliver business results faster, at higher quality, and with lower risk.

  • With 81% of employees using their phones at work, companies have stopped asking: "Is corporate data leaking from personal devices?" and started asking: "How do we effectively prevent corporate data from leaking from personal devices?" The answer has not been simple. ZixOne raises the bar on BYOD security by not allowing email data to reside on the device. In addition, Zix allows employees to maintain complete control of their personal device, therefore satisfying privacy demands of valued employees and the …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds