How to do tooltip for CBitmapButton in CView

I have an MDI app with child windows derived from CView. In a child windows I have a couple of buttons of class CBitmapButton. What do I have to do to get the tooltips to show for these buttons?
gatesAsked:
Who is Participating?
 
tma050898Commented:
Answer on the way...
0
 
RONSLOWCommented:
EnableToolTips(true) for your view and supply a handler for the TTN_NEEDTEXT message.  This handler will look at the TOOLTIPTEXT structure passed and determine which control the mouse was over and set the tooltip text appropriately.
 

0
 
gatesAuthor Commented:
I tried all this before posting the question. The problem I encountered was: only the toolbar buttons's tooltip activate the TTN_NEEDTEXT handler. When the mouse was over the CBitmapButton child windows in the CView window, TTN_NEEDTEXT handler was not called. I also tried creating a CTooltipCtr and AddTool with the CBitmapButton as argument but without success.
I need the exact code that works, because I already read the document as well as the MSJ 4/97 article on tooltips.
0
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.

 
tma050898Commented:
gates,

While this code was originally written for controls on a dialog, I just tested it for a CBitmapButton control that is dynamically created on a view and it works without any problem.

To use this class simply add a member variable of type CToolTip to the view, call the tool tip's Create function in the view's OnInitialUpdate function and call any of the three overloaded AddWindowTool functions for each control you wish to have a tooltip.

class CToolTip : public CToolTipCtrl
{
public:
 BOOL AddWindowTool(UINT uiControlId);
 BOOL AddWindowTool(UINT uiControlId, UINT uiStringId);
 BOOL AddWindowTool(UINT uiControlId, LPCTSTR pszText);

protected:
 BOOL AddRectTool(CWnd* pWnd, LPCTSTR pszText,
  LPCRECT lpRect, UINT nIDTool);
};

BOOL CToolTip::AddWindowTool(UINT uiControlId) // assumes control id = string id
{
 return AddWindowTool(uiControlId, uiControlId);
}

BOOL CToolTip::AddWindowTool(UINT uiControlId, UINT uiStringId)
{
 CString strText;
 VERIFY(strText.LoadString(uiStringId));
 return (AddWindowTool(uiControlId, strText));
}

BOOL CToolTip::AddWindowTool(UINT uiControlId, LPCTSTR pszText)
{
 BOOL bSuccess = FALSE;

 CWnd* pParentWnd = GetOwner();
 ASSERT(pParentWnd); // did you call create first?
 if (pParentWnd)
 {
  CWnd* pWnd = pParentWnd->GetDlgItem(uiControlId);
  ASSERT(pWnd);
  if (pWnd)
  {
   TOOLINFO ti;
   ti.cbSize = sizeof (TOOLINFO);
   ti.uFlags = TTF_IDISHWND | TTF_SUBCLASS;
   ti.hwnd = pWnd->GetParent()->GetSafeHwnd();
   ti.uId = (UINT) pWnd->GetSafeHwnd();
   ti.hinst = AfxGetInstanceHandle();
   ti.lpszText = (LPTSTR) pszText;
   SendMessage( TTM_DELTOOL, 0, (LPARAM) &ti );
   bSuccess = (BOOL) SendMessage( TTM_ADDTOOL, 0, (LPARAM) &ti );
  }
 }

 return bSuccess;
}

BOOL CToolTip::AddRectTool(CWnd* pWnd, LPCTSTR pszText,LPCRECT lpRect, UINT nIDTool)
{
TOOLINFO ti;
ti.cbSize = sizeof (TOOLINFO);
ti.uFlags = TTF_SUBCLASS;
ti.hwnd = pWnd->GetSafeHwnd();
ti.uId = nIDTool;
ti.hinst = AfxGetInstanceHandle();
ti.lpszText = (LPTSTR) pszText;
::CopyRect( &ti.rect, lpRect );
return (BOOL) SendMessage( TTM_ADDTOOL, 0, (LPARAM) &ti );
}

Here's an example of the how to use the different CToolTip AddWindowTool functions. As you can see, there are three overloaded functions to pick from depending on how and where you are storing the tooltip text for the controls.

void CYourView::OnInitialUpdate()
{
 ...
 // creation of your CBitmapButton

 m_tooltip.Create(this);

 m_tooltip.AddWindowTool(ID_YOUR_BUTTON, "OK"); // hard coded tooltip

 m_tooltip.AddWindowTool(ID_YOUR_BUTTON); // string id = control id

 m_tooltip.AddWindowTool(ID_YOUR_BUTTON, IDS_YOUR_BUTTON); // string id != control id
 
 ...
}

IMPORTANT!!!
As I wouldn't want other people taking credit for my work, so I want to mention that Ian (the only name I have) wrote this class. I simply added the 2 overloaded AddWindowTool functions so that string resource ids could be used. His email is ianm@iexplorers.com if you want to thank him for providing this code for everyone.

Tom

0
 
RONSLOWCommented:
That answer looks familiar, tom :-)

Anyway, gates (bill?) .. did you EnableToolTips(true) first?  I am surprised it did not wark .. and that your views tooltip text handler got called for a tooblar button .. are you sure you put the handler in the correct place?

In my view class, I create a tooltip member for the view class

In my mouse move I detect the item under the mouse (and remember if) and activate the tooltip.  Then in my
ON_NOTIFY_EX_RANGE(TTN_NEEDTEXT,0,0xFFFF,OnToolTipNeedText)
handler I check if there is an item that requires a tooltip, and if so I set the text appropriately

I can supply code if you want.

0
 
tma050898Commented:
RONSLOW,

>>>That answer looks familiar, tom :-)
LOL ! It's the staple of an OOP...code reuse! Looks like we get to rekindle that argument over which class works best ;)

Tom


0
 
gatesAuthor Commented:
TMA,
Your answer gave me enough hint to fix my problem even though most of the code is from another developer and even though I don't use the code in my work. It's the result that counts so I grade your help Excellent. The key hint is the use of CTooltipCtrl. (Ronslow: I don't use the NeedText handler approach). Many thanks, both, for your help.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.