Share via


CWnd::EnableToolTips

BOOL EnableToolTips( BOOL bEnable );

Return Value

TRUE if tooltips are enabled; otherwise FALSE.

Parameters

bEnable

Specifies whether the tooltip control is enabled or disabled. TRUE enables the control; FALSE disables the control.

Remarks

Call this member function to enable tooltips for the given window. Override OnToolHitTest to provide the struct(s) for the window.

Note   Some windows, such as CToolBar, provide a built-in implementation of OnToolHitTest.

See in the Win32 SDK Programmer’s Reference for more information on this structure.

Simply calling EnableToolTips is not enough to display tool tips for your child controls unless the parent window is derived from CFrameWnd. This is because CFrameWnd provides a default handler for the TTN_NEEDTEXT notification. If your parent window is not derived from CFrameWnd, that is, if it is a dialog box or a form view, tool tips for your child controls will not display correctly unless you provide a handler for the TTN_NEEDTEXT tool tip notification. See in Visual C++ Programmer’s Guide for a sample handler.

The default tool tips provided for your windows by EnableToolTips do not have text associated with them. In order to retrieve text for the tool tip to display, the TTN_NEEDTEXT notification is sent to the tool tip control's parent window just before the tool tip window is displayed. If there is no handler for this message to assign some value to the pszText member of the TOOLTIPTEXT structure, there will be no text displayed for the tool tip.

Example

BEGIN_MESSAGE_MAP(CMyView, CView)
   ...
   ON_NOTIFY_EX_RANGE(TTN_NEEDTEXTW, 0, 0xFFFF, OnToolTipNotify)
   ON_NOTIFY_EX_RANGE(TTN_NEEDTEXTA, 0, 0xFFFF, OnToolTipNotify)
END_MESSAGE_MAP()

void CMyView::OnInitialUpdate()
{
   CView::OnInitialUpdate();

   CEdit* pEdit = new CEdit;
   pEdit->Create(ES_MULTILINE | WS_CHILD | WS_VISIBLE | WS_TABSTOP | WS_BORDER,
      CRect(10, 10, 100, 100), this, 111);
   EnableToolTips(TRUE);   // enable tool tips for view
}

//Notification handler
BOOL CMyView::OnToolTipNotify(UINT id, NMHDR *pNMHDR,
   LRESULT *pResult)
{
   // need to handle both ANSI and UNICODE versions of the message
   TOOLTIPTEXTA* pTTTA = (TOOLTIPTEXTA*)pNMHDR;
   TOOLTIPTEXTW* pTTTW = (TOOLTIPTEXTW*)pNMHDR;
   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 = ::GetDlgCtrlID((HWND)nID);
   }

   if (nID != 0) // will be zero on a separator
      strTipText.Format("Control ID = %d", nID);

   if (pNMHDR->code == TTN_NEEDTEXTA)
      lstrcpyn(pTTTA->szText, strTipText, sizeof(pTTTA->szText));
   else
      _mbstowcsz(pTTTW->szText, strTipText, sizeof(pTTTW->szText));
   *pResult = 0;

   return TRUE;    // message was handled
}

CWnd OverviewClass MembersHierarchy Chart

See Also   CWnd::CancelToolTips, CWnd::OnToolHitTest, CToolBar,