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

  • This ESG study by Mark Peters evaluated a common industry-standard disk VTl deduplication system (with 15:1 reduction ratio) versus a tape library with LTO-5, drives with full nightly backups, over a five-year period.  The scenarios included replicated systems and offsite tape vaults.  In all circumstances, the TCO for VTL with deduplication ranged from about 2 to 4 times more expensive than the LTO-5 tape library TCO. The paper shares recent ESG research and lots more. 

  • Live Event Date: September 16, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you starting an on-premise-to-cloud data migration project? Have you thought about how much space you might need for your online platform or how to handle data that might be related to users who no longer exist? If these questions or any other concerns have been plaguing you about your migration project, check out this eSeminar. Join our speakers Betsy Bilhorn, VP, Product Management at Scribe, Mike Virnig, PowerSucess Manager and Michele …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds