Microsoft KB Archive/141758

= How to add tooltips for controls to an MFC modal dialog box =

Article ID: 141758

Article Last Modified on 11/21/2006

-

APPLIES TO

 Microsoft Foundation Class Library 4.2, when used with:  Microsoft Visual C++ 2.1

 Microsoft Visual C++ 2.2

 Microsoft Visual C++ 4.0 Standard Edition

 Microsoft Visual C++ 4.1 Subscription</li></ul>

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

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

 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++ 4.2 Professional Edition</li></ul>

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

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

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

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

-

<div class="notice_section">

This article was previously published under Q141758

<div class="summary_section">

SUMMARY
To make the CToolTipCtrl class work correctly, you must call the CToolTipCtrl::RelayEvent function. When you use this function, the mouse messages can 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 the CToolTipsCtrl::RelayEvent function. However, for a modal dialog box in MFC versions earlier than version 4.0, the CDialog::PreTranslateMessage function is not called because modal dialog boxes have their own message loops.

In MFC 4.0 and later versions, this is not a problem because of changes to the implementation of DoModal. Therefore, to use the CToolTipCtrl class in a modal dialog box, you must use a different approach for versions earlier than 4.0. This article gives you step-by-step examples that show you how to use the CToolTipCtrl class in an MFC modal dialog box for MFC 4.0 and earlier versions.

<div class="moreinformation_section">

Step-by-step examples
The following procedures generate a default MFC skeleton application, add tooltips to the OK button in the About dialog box, and add tooltips to the dialog box itself.

For MFC 4.0 or later versions, follow these steps: <ol> <li>Use the Appwizard in Visual C++ to generate an MFC application. Name the application Tooltips. Use all the Appwizard default settings.</li> <li> Use ClassWizard to add a PreTranslateMessage override to CAboutDialog. CAboutDialog::PreTranslateMessage(MSG* pMsg) {       if (NULL != m_pToolTip) m_pToolTip->RelayEvent(pMsg);

return CDialog::PreTranslateMessage(pMsg); } </li> <li> Use ClassWizard to add a member variable for the OK button in the CAboutDlg class. Name the member variable m_btOK. Also, add an m_pToolTip pointer to a CToolTipCtrl object. 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 may first have to add a default destructor. CAboutDlg::CAboutDlg : CDialog(CAboutDlg::IDD) {      m_pToolTip = NULL; }

CAboutDlg::~CAboutDlg {      delete m_pToolTip; } </li> <li> Override the OnInitDialog function of the CAboutDlg class to set up the tooltip control. BOOL CAboutDlg::OnInitDialog {     CDialog::OnInitDialog;

//Set up the tooltip m_pToolTip = new CToolTipCtrl; if(!m_pToolTip->Create(this)) {          TRACE("Unable To create ToolTip\n"); return TRUE; }

if(m_pToolTip->AddTool(this, "About Box")) {          TRACE("Unable to add Dialog to the tooltip\n"); }

if (m_pToolTip->AddTool(&m_btOK,"OK Button")) {          TRACE("Unable to add OK button to the tooltip\n"); }

m_pToolTip->Activate(TRUE);

return TRUE; } </li> <li>Rebuild the application. Then, open the About dialog box. You will see the tooltips.</li></ol>

For versions of MFC earlier than version 4.0, follow these steps: <ol> <li>Use the Appwizard in Visual C++ to generate an MFC application. Name the application Tooltips. Use all the Appwizard default settings.</li> <li>Include the <Afxcmn.h> header file in the Stdafx.h file.</li> <li> Add the following member variables to the CTooltipsApp class in the Tooltips.h file. class CTooltipsApp : public CWinApp {      //...   public: HWND   m_hwndDialog; CToolTipCtrl*  m_gpToolTip;

//...  }; </li> <li> Initialize the two variables in the application's constructor to NULL. CTooltipsApp::CTooltipsApp {      m_hwndDialog = NULL; m_gpToolTip = NULL; } </li> <li> Override the CTooltipsApp::ProcessMessageFilter function. 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. Name the member variable m_btOK. Also, add an m_pToolTip pointer to a CToolTipCtrl object. 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 may first have to add a default destructor. CAboutDlg::CAboutDlg : CDialog(CAboutDlg::IDD) {      m_pToolTip = NULL; }

CAboutDlg::~CAboutDlg {      delete m_pToolTip; } </li> <li> Override the OnInitDialog function of the CAboutDlg class to pass the handle of the dialog box to the application. BOOL CAboutDlg::OnInitDialog {      CDialog::OnInitDialog;

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

if (!m_pToolTip) {          m_pToolTip = new CToolTipCtrl; if(!m_pToolTip->Create(this)) {              TRACE("Unable To create ToolTip\n"); return TRUE; }

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

if(m_pToolTip->AddTool(this, "About Box")) {              TRACE("Unable to add Dialog to the tooltip\n"); }

if (m_pToolTip->AddTool(&m_btOK,"OK Button")) {              TRACE("Unable to add OK button to the tooltip\n"); }

m_pToolTip->Activate(TRUE); }

return TRUE;//return TRUE unless you set the focus to a control //EXCEPTION: OCX Property Pages should return FALSE } </li> <li> Override 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. Then, open the About dialog box. You will see the tooltips.</li></ol>

Additional query words: 2.10 2.20 3.10 3.20 4.00 alwaysupdate dskbsweep

Keywords: kbhowto kbtooltip kbuidesign kbdlg kbcode KB141758

-

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

© Microsoft Corporation. All rights reserved.