Microsoft KB Archive/135873

= How To Add Tooltips for Controls on an MFC Modal Dialog Box =

PSS ID Number: 135873

Article Last Modified on 6/29/2004

-

The information in this article applies to:

 Microsoft Visual C++ .NET (2003) The Microsoft Foundation Classes (MFC), when used with:  Microsoft Visual C++, 32-bit Editions 2.1

 Microsoft Visual C++, 32-bit Editions 2.2

 Microsoft Visual C++, 32-bit Editions 4.0

 Microsoft Visual C++, 32-bit Editions 4.1</li></ul>

 Microsoft Visual C++, 32-bit Enterprise Edition 4.2</li></ul>

 Microsoft Visual C++, 32-bit Enterprise Edition 4.2b</li></ul>

 Microsoft Visual C++, 32-bit Enterprise Edition 5.0</li></ul>

 Microsoft Visual C++, 32-bit Enterprise Edition 6.0</li></ul>

 Microsoft Visual C++, 32-bit Professional Edition 4.2</li></ul>

 Microsoft Visual C++, 32-bit Professional Edition 4.2b</li></ul>

 Microsoft Visual C++, 32-bit Professional Edition 5.0</li></ul>

<ul> <li>Microsoft Visual C++, 32-bit Professional Edition 6.0</li></ul>

<ul> <li>Microsoft Visual C++, 32-bit Learning Edition 6.0</li></ul>

<ul> <li>Microsoft Visual C++ .NET (2002)</li></ul> </li></ul>

-

<div class="notice_section">

This article was previously published under Q135873

<div class="notice_section">

Note Microsoft Visual C++ .NET (2002) supports 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
To make the CToolTipCtrl class work correctly, you must call the CToolTipCtrl::RelayEvent function. This makes it possible for the mouse messages to be passed to the tooltip control.

For a non-modal dialog box window in an MFC application, use the window's CWnd::PreTranslateMessage function to call CToolTipsCtrl::RelayEvent. However, for a modal dialog box, the CDialog::PreTranslateMessage function is not called because modal dialog boxes have their own message loops. Therefore, to use CToolTipCtrl in a modal dialog box, you need a different approach. This article gives a step-by-step example that shows how to use the CToolTipCtrl class in a MFC modal dialog box, it involves adding two additional member variables to the application class and overwriting the CWinApp::ProcessMessageFilter function for the application class.

<div class="moreinformation_section">

Step-by-Step Example
The following procedure generates a default MFC skeleton application and adds tooltips to the OK button on the About dialog box and the dialog box itself: <ol> <li>Use the Appwizard in Visual C++ to generate an MFC application, call it Tooltips, and use all the Appwizard default settings.</li> <li>Include the <afxcmn.h> header file in the stdafx.h file.

NOTE: Do not do this step for Visual C++ .NET.</li> <li> Add the following member variables to the CTooktipsApp class in the Tooltips.h file: class CTooltipsApp : public CWinApp {     //...   public: HWND   m_hwndDialog; CToolTipCtrl*  m_gpToolTip;

//...  };                        </li> <li> Initiate the two variables in the application's constructor to NULL: CTooltipsApp::CTooltipsApp {     m_hwndDialog = NULL; m_gpToolTip = NULL; }                       </li> <li> Overwrite the CTooltipsApp:: ProcessMessageFilter function as follows: BOOL CTooltipsApp::ProcessMessageFilter(int code, LPMSG lpMsg) {     if (m_hwndDialog != NULL) if (lpMsg->hwnd == m_hwndDialog ||             ::IsChild(m_hwndDialog, lpMsg->hwnd)) {                          if (NULL != m_gpToolTip) m_gpToolTip->RelayEvent(lpMsg); }          return CWinApp::ProcessMessageFilter(code, lpMsg); }                       </li> <li> Use Classwizard to add a member variable for the OK button in the CAboutDlg class, and call it m_btOK. Also, add a m_pTooltip pointer to a CToolTipCtrl object:

NOTE: For Visual C++ .NET you can add a memeber variable using the "Class View" right. (Click the AboutDialog calls, and then on Add menu, select Add Variable.) class CAboutDlg : public CDialog {  public: CAboutDlg;

// Dialog Data //AFX_DATA

CToolTipCtrl* m_pTooltip;

//...  };                        </li> <li> Add code to the CAboutDlg class' constructor and destructor to initialize and release the tooltip object, you might also need to add a default destructor first: CAboutDlg::CAboutDlg : CDialog(CAboutDlg::IDD) {     m_pTooltip = NULL; }

CAboutDlg::~CAboutDlg {     delete m_pTooltip; }                       </li> <li> Overwrite the OnMouseMove function of the CAboutDlg class to set up the tooltip control: void CAboutDlg::OnMouseMove(UINT nFlags, CPoint point) {      //Set up the tooltip if (!m_pTooltip) {         int rt; m_pTooltip = new CToolTipCtrl; rt = m_pTooltip->Create(this); ASSERT(rt!=0);

((CTooltipsApp*)AfxGetApp)->m_gpToolTip= m_pTooltip;

rt = m_pTooltip->AddTool(this, "About Box"); ASSERT(rt!=0); rt = m_pTooltip->AddTool(&m_btOK,"OK Button"); ASSERT(rt!=0);

m_pTooltip->Activate(TRUE); }

CDialog::OnMouseMove(nFlags, point); }                       </li> <li> Overwrite the OnInitDialog function of the CAboutDlg class to pass the dialog's handle to the application: BOOL CAboutDlg::OnInitDialog {     CDialog::OnInitDialog;

((CTooltipsApp*)AfxGetApp)->m_hwndDialog=m_hWnd;

return TRUE; // return TRUE unless you set the focus to a control // EXCEPTION: OCX Property Pages should return FALSE }                       </li> <li> Overwrite the PostNcDestroy function of the CAboutDlg class to reset the variables in the application class: void CAboutDlg::PostNcDestroy {     CDialog::PostNcDestroy;

((CTooltipsApp*)AfxGetApp)->m_hwndDialog= NULL; ((CTooltipsApp*)AfxGetApp)->m_gpToolTip= NULL; }                       </li> <li>Rebuild the application and bring up the About dialog box, you will see the tooltips.</li></ol>

Keywords: kbCmnCtrls kbCtrl kbDlg kbFAQ kbhowto kbToolTip KbUIDesign KB135873

Technology: kbAudDeveloper kbMFC kbVC210 kbVC220 kbVC32bitSearch kbVC400 kbVC410 kbVC420 kbVC420b kbVC500 kbVC500Search kbVC600 kbVCNET2002 kbVCNET2003 kbVCNETSearch kbVCsearch

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.