Microsoft KB Archive/141871

= How to add ToolTips to ActiveX controls =

Article ID: 141871

Article Last Modified on 11/21/2006

-

APPLIES TO

 Microsoft Foundation Class Library 4.2, when used with:  Microsoft Visual C++ 4.0 Standard Edition

 Microsoft Visual C++ 4.1 Subscription

 Microsoft Visual C++ 4.2 Enterprise Edition

 Microsoft Visual C++ 5.0 Enterprise Edition</li></ul>

 Microsoft Visual C++ 6.0 Enterprise Edition</li></ul>

 Microsoft Visual C++ 4.2 Professional Edition</li></ul>

 Microsoft Visual C++ 5.0 Professional Edition</li></ul>

 Microsoft Visual C++ 6.0 Professional Edition</li></ul>

 Microsoft Visual C++ 6.0 Standard Edition</li></ul>

 Microsoft Visual C++ .NET 2002 Standard Edition</li></ul> </li> Microsoft Visual C++ .NET 2003 Standard Edition</li></ul>

-

<div class="notice_section">

This article was previously published under Q141871

<div class="notice_section">

Note Microsoft Visual C++ .NET 2002 and Microsoft Visual C++ .NET 2003 support both the managed code model that is provided by the Microsoft .NET Framework and the unmanaged native Microsoft Windows code model. The information in this article applies only to unmanaged Visual C++ code.

<div class="summary_section">

SUMMARY
This article demonstrates how to add a ToolTip to an ActiveX control.

<div class="moreinformation_section">

MORE INFORMATION
By default, ActiveX controls do not support ToolTips. The following steps, however, demonstrate how to modify a basic ActiveX control generated using the MFC ActiveX Control Wizard to add this support: <ol> Create a new Visual C++ project by using the MFC ActiveX Control template. Name the new control Basic.</li>  Open the Stdafx.h file associated with the project and add the following line. #include <afxcmn.h> Note Do not perform this step in Visual C++ .NET. The header file Afxcmn.h contains declarations for MFC classes that serve as wrappers to Windows Common Controls including CToolTipCtrl. </li>  Add the following lines to COleControl-derived class CBasicCtrl located in BasicCtrl.h.      CToolTipCtrl m_ttip; void RelayEvent(UINT message, WPARAM wParam, LPARAM lParam); The RelayEvent method will be used by the mouse message handlers to relay those messages to the ToolTip control.

</li> <li> Add a WM_CREATE message handler to the CBasicCtrl message map. To do this, click to select CBasicCtrl in the Class View window. With the CBasicCtrl node selected, open the Properties window, and then click to select Messages. Find the WM_CREATE message, and then add an OnCreate method to handle the message. It is in this routine that the ToolTip control will be created. Add the following code to this handler. if (!m_ttip.Create(this)) TRACE0("Unable to create tip window."); else if (!m_ttip.AddTool(this, LPCTSTR(m_ToolTipText))) TRACE0("Unable to add tip for the control window."); else m_ttip.Activate(m_ShowToolTip); </li> <li> To relay appropriate messages to the ToolTip control, add handlers for WM_LBUTTONDOWN, WM_LBUTTONUP, and WM_MOUSEMOVE to the CBasicCtrl message map. The code for these handlers follows. void CBasicCtrl::OnLButtonDown(UINT nFlags, CPoint point) {        RelayEvent(WM_LBUTTONDOWN, (WPARAM)nFlags,                    MAKELPARAM(LOWORD(point.x), LOWORD(point.y)));

COleControl:: OnLButtonDown(nFlags, point); }

void CBasicCtrl::OnLButtonUp(UINT nFlags, CPoint point) {        RelayEvent(WM_LBUTTONUP, (WPARAM)nFlags,                    MAKELPARAM(LOWORD(point.x), LOWORD(point.y)));

COleControl::OnLButtonUp(nFlags, point); }

void CBasicCtrl::OnMouseMove(UINT nFlags, CPoint point) {        RelayEvent(WM_MOUSEMOVE, (WPARAM)nFlags,                    MAKELPARAM(LOWORD(point.x), LOWORD(point.y)));

COleControl::OnMouseMove(nFlags, point); }

// implementation of the CBasicCtrl::RelayEvent method:

void CBasicCtrl::RelayEvent(UINT message, WPARAM wParam, LPARAM  lParam) {        if (NULL != m_ttip.m_hWnd) { MSG msg;

msg.hwnd= m_hWnd; msg.message= message; msg.wParam= wParam; msg.lParam= lParam; msg.time= 0; msg.pt.x= LOWORD (lParam); msg.pt.y= HIWORD (lParam);

m_ttip.RelayEvent(&msg); }     }  While it might seem reasonable to call CWnd::GetCurrentMessage instead of manually building a message, the value of the point that is returned is expressed in screen coordinates. When the ToolTip performs a hit test to determine if the point of the relayed message falls within the boundary of the client rectangle of any associated tools, the test will fail, and the ToolTip will not be displayed. </li> <li> Alter CBasicCtrl::DoPropExchange by adding code to initialize the m_ToolTipText and m_ShowToolTip properties. PX_Bool(pPX, _T("ShowToolTip"), (BOOL&)m_ShowToolTip, FALSE);In order to view tooltip, this must be true. PX_String(pPX, _T("ToolTipText"), m_ToolTipText, _T("")); And we need to add some text in _T(“ ...”) </li> <li> To allow the user of the control some control over the ToolTip functionality, add the following Automation properties to the CBasicCtrl class. To do this, in the Class View window, select BasicLib (this node displays class view information about the project IDL file). Open this node and right-click the primary Interface node (_DBasic). Click Add, and then click Add Property. Add the following two properties with the listed parameters: <pre class="fixed_text">Property Name:         ShowToolTip            ToolTipText Property Type:         VARIANT_BOOL           BSTR Variable name:         m_ShowToolTip          m_ToolTipText Notification function: OnShowToolTipChanged   OnToolTipTextChanged ShowToolTip will allow the user to suppress the display of the ToolTip, and ToolTipText will track the text that is to be displayed when the ToolTip is visible. </li> <li> Modify the property change notification functions for these properties in the following manner. void CBasicCtrl::OnToolTipTextChanged {        if (m_ttip.m_hWnd && AmbientUserMode) { m_ttip.UpdateTipText(LPCTSTR(m_ToolTipText), this); SetModifiedFlag; }     }

void CBasicCtrl::OnShowToolTipChanged {        if (m_ttip.m_hWnd && AmbientUserMode) { m_ttip.Activate(m_ShowToolTip); SetModifiedFlag; }     } </li></ol>

<div class="references_section">