Best way to dynamically change the tooltip text of a toolbar?
What's the easiest way to dynamically change the tooltip text (and the information displayed on the statusbar) of a toolbar created with MFC >=4.0?
Re: Best way to dynamically change the tooltip text of a toolbar?
Hi,
By default MFC uses string resources with the same ID as the toolbar buttons for status
messages and tooltip text.
To alter statusbar messages :
Override the GetMessageString function of the CMDIFrameWnd like this:
void CMainFrame::GetMessageString(UINT nID, CString& sMessage) const
{
// load appropriate string
if(!m_bCustomMessage) {
CFrameWnd::GetMessageString(nID,sMessage);
return;
}
switch(nID) {
case ID_1:
sMessage = _T("New hint 1");
break;
case ID_2:
sMessage = _T(""New hint 2"");
break;
default:
CFrameWnd::GetMessageString(nID, sMessage);
}
}
To override the default tooltip text handling two messages must be intercepted in the
CxxFrame, 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_1:
strTipText = _T("Tip 1");
return TRUE;
case ID_2:
strTipText = _T("Tip 2");
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 );
}
Hope this helps,
Oleg.
Re: Best way to dynamically change the tooltip text of a toolbar?
Thanks alot, it was just what I needed! I wasn't even close to getting it right. Thanks again.