HOWTO: Add Tooltips to ActiveX Controls

Last reviewed: June 26, 1997
Article ID: Q141871
The information in this article applies to:
  • The Microsoft Foundation Classes (MFC) included with: - Microsoft Visual C++, 32-bit Edition, versions 4.0, 4.1, 4.2, 5.0

SUMMARY

This article demonstrates how to add a ToolTip to an ActiveX Control.

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.

  1. Use the MFC ActiveX Control Wizard to generate a basic control named Basic.

  2. Open the Stdafx.h file associated with the project and add the following line:

          #include <afxcmn.h>
    

    The header file Afxcmn.h contains declarations for MFC classes that serve as wrappers to Windows Common Controls including CToolTipCtrl.

  3. Add the following lines to COleControl-derived class CBasicCtrl located in Basicctl.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.

  4. Use ClassWizard to add an OnCreate handler to the message map. 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);
    
    

  5. To relay appropriate messages to the ToolTip control, add handlers for WM_LBUTTONDOWN, WM_LBUTTONUP, and WM_MOUSEMOVE to the control's message map. The message map and code for these handlers follows:

          // Message maps
          //{{AFX_MSG(CTestpropCtrl)
    
             afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
             afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
             afx_msg void OnLButtonUp(UINT nFlags, CPoint point);
             afx_msg void OnMouseMove(UINT nFlags, CPoint point);
          //}}AFX_MSG
                DECLARE_MESSAGE_MAP()
    
          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.
    
    

  6. Alter CBasicCtrl::DoPropExchange by adding code to initialize the m_toolTipText and m_showToolTip properties:

          PX_Bool(pPX, _T("ShowToolTip"), m_showToolTip, FALSE);
          PX_String(pPX, _T("ToolTipText"), m_toolTipText, _T(""));
    

  7. To allow the user of the control some control over the ToolTip functionality, use ClassWizard to add the following Automation properties to the CBasicCtrl class:

       External Name:          ShowToolTip            ToolTipText
       Type:                   BOOL                   CString
       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.
    
    

  8. 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 CBaseCtrl::OnShowToolTipChanged()
          {
             if (m_ttip.m_hWnd && AmbientUserMode()) {
                m_ttip.Activate(m_showToolTip);
                SetModifiedFlag();
             }
          }
    
    

Test the Control

To test the control, build it, launch the ActiveX Control Test Container and insert the control into the Test Container. Then follow these steps:

  1. On the View menu, click Properties.

  2. Pull down the Property combo, set the ShowToolTip property to -1, and choose Apply.

  3. Pull down the Property combo, set the ToolTipText property to SomeTip, and choose Apply.

  4. Move the pointer over the client area of the control.

A ToolTip containing the text "Some Tip" should be displayed over the client area of the control.

REFERENCES

Microsoft Visual C++ version 4.x or 5.0 Books Online.

Cluts, Nancy, "Programming the Windows 95 User Interface," Microsoft Press, 1995, Pages 26-28.


Keywords : kbcode kbprg MfcOLE
Technology : kbMfc kbole
Version : 4.0 4.1 4.2 5.0
Platform : NT WINDOWS
Issue type : kbhowto


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: June 26, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.