Solved

How to do tooltip for CBitmapButton in CView

Posted on 1998-05-30
7
602 Views
Last Modified: 2013-11-20
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?
0
Comment
Question by:gates
  • 3
  • 2
  • 2
7 Comments
 
LVL 10

Expert Comment

by:RONSLOW
ID: 1315781
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
 

Author Comment

by:gates
ID: 1315782
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
 
LVL 3

Accepted Solution

by:
tma050898 earned 200 total points
ID: 1315783
Answer on the way...
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 3

Expert Comment

by:tma050898
ID: 1315784
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
 
LVL 10

Expert Comment

by:RONSLOW
ID: 1315785
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
 
LVL 3

Expert Comment

by:tma050898
ID: 1315786
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
 

Author Comment

by:gates
ID: 1315787
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

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

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: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
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.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

758 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now