Solved

changing tool tip for a toolbar button

Posted on 1998-07-27
3
251 Views
Last Modified: 2013-11-20
I want to change the tool tip text (what floats over the top of the button when the mouse rests on it) of a button to different values at different points during my program, does anyone know how to do this on the fly? I have a pointer to the toolbar on which the button resides but cannot figure out how to change the tool tip text.

thanks
0
Comment
Question by:nicholso
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 8

Accepted Solution

by:
trestan earned 100 total points
ID: 1319790
0
 
LVL 6

Expert Comment

by:snoegler
ID: 1319791
I took a look into the MFC source codes, and surprisingly the tool-tips are NOT handled by
CToolBar but by the CFrameWnd(CMainFrame) which contains the CToolBar. The problem is,
in the source code the tool tip texts are read directly from the tool-bar resource - no chance to
edit or alter them(Take a look at the source code below).
The only way is to modify the routine itself which is much easier than it sounds.

1. Iin your BEGIN_MESSAGE_MAP/END_MESSAGE_MAP of your CMainFrame you
    have to add:
    ON_NOTIFY_EX_RANGE(TTN_NEEDTEXTW, 0, 0xFFFF, OnToolTipText)
    ON_NOTIFY_EX_RANGE(TTN_NEEDTEXTA,  0, 0xFFFF, OnToolTipText)
    These two lines map the tool-tip text request which is sent by the tool bar to your
    CMainFrame.

2. Now take a look into the original handler
    (defined in 'WINFRM.CPP' in your MFC source dir which is on my machine
     \MSDEV\MFC\SRC)


BOOL CFrameWnd::OnToolTipText(UINT, NMHDR* pNMHDR, LRESULT* pResult)
{
ASSERT(pNMHDR->code == TTN_NEEDTEXTA || pNMHDR->code == TTN_NEEDTEXTW);

// allow top level routing frame to handle the message
if (GetRoutingFrame() != NULL)
return FALSE;

// need to handle both ANSI and UNICODE versions of the message
TOOLTIPTEXTA* pTTTA = (TOOLTIPTEXTA*)pNMHDR;
TOOLTIPTEXTW* pTTTW = (TOOLTIPTEXTW*)pNMHDR;
TCHAR szFullText[256];
CString strTipText;
UINT nID = pNMHDR->idFrom;
if (pNMHDR->code == TTN_NEEDTEXTA && (pTTTA->uFlags & TTF_IDISHWND) ||
pNMHDR->code == TTN_NEEDTEXTW && (pTTTW->uFlags & TTF_IDISHWND))
{
// idFrom is actually the HWND of the tool
nID = _AfxGetDlgCtrlID((HWND)nID);
}

/********************/
/**** !!!!!!!!!!! THIS IS THE IMPORTANT PART <---------------------- ******/
/********************/
if (nID != 0) // will be zero on a separator
{
AfxLoadString(nID, szFullText);
      // this is the command id, not the button index
AfxExtractSubString(strTipText, szFullText, 1, '\n');
}
#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
*pResult = 0;

return TRUE;    // message was handled
}


3. Now take a look at the part after the line with "THIS IS THE IMPORTANT PART"

>>AfxLoadString(nID, szFullText);
>>      // this is the command id, not the button index
>>AfxExtractSubString(strTipText, szFullText, 1, '\n');

Here you have got the message ID(for ex ID_FILE_OPEN) in the var nID. After these lines,
strTipText contains the string which will be displayed as the tool tip.

* Now you could implement something like switch(nID) { ... }

* Or you add a "CArray<CString,CString&>  myToolTips" to your CFrameWnd, and use
  something like that:

 // 0<=buttonIndex<count of buttons
  int buttonIndex=m_wndToolBar.CommandToIndex(nID);
  strToolTip=myToolTips[buttonIndex]

So you can change tool tips by changing myToolTips.

Perhaps you like to set them up to the initial values:

CMainFrame::OnCreate()
{
.. <class wiz created stuff here>

// insert this before return 0;

int i,nButtons=m_wndToolBar.GetToolBarCtrl().GetButtonCount();
for(i=0;i<nButtons;i++) {
  CString szFullText,szToolTip;
  UINT nID; // command ID

  nID=m_wndToolBar.GetItemID();
  AfxLoadString(nID, szFullText);
  AfxExtractSubString(szToolTip, szFullText, 1, '\n');

  myToolTips.SetAtGrow(i,szToolTip);
}

I hope this will help you.
If you want i can send you some sample code, just mail me at alexander.berthold@rol3.com
0
 

Author Comment

by:nicholso
ID: 1319792
I solved my problem, I just needed to toggle between 2 different messages, so I defined them both in the string table, and then just changed the object ID of the toolbar button in question whenever I needed, since that is easy.

thanks so much for all your help.
Anthony
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question