How to add tooltips for controls to an MFC modal dialog box
| Article ID | : | 141758 |
| Last Review | : | November 21, 2006 |
| Revision | : | 3.1 |
This article was previously published under Q141758
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.
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.
MORE INFORMATION
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:
| 1. | Use the Appwizard in Visual C++ to generate an MFC application. Name the application Tooltips. Use all the Appwizard default settings. |
| 2. | Use ClassWizard to add a PreTranslateMessage override to CAboutDialog. CAboutDialog::PreTranslateMessage(MSG* pMsg) |
| 3. | 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 |
| 4. | 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) |
| 5. | Override the OnInitDialog function of the CAboutDlg class to set up the tooltip control. BOOL CAboutDlg::OnInitDialog() |
| 6. | Rebuild the application. Then, open the About dialog box. You will see the tooltips. |
| 1. | Use the Appwizard in Visual C++ to generate an MFC application. Name the application Tooltips. Use all the Appwizard default settings. |
| 2. | Include the |
| 3. | Add the following member variables to the CTooltipsApp class in the Tooltips.h file. class CTooltipsApp : public CWinApp |
| 4. | Initialize the two variables in the application's constructor to NULL. CTooltipsApp::CTooltipsApp() |
| 5. | Override the CTooltipsApp::ProcessMessageFilter function. BOOL CTooltipsApp::ProcessMessageFilter(int code, LPMSG lpMsg) |
| 6. | 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 |
| 7. | 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) |
| 8. | Override the OnInitDialog function of the CAboutDlg class to pass the handle of the dialog box to the application. BOOL CAboutDlg::OnInitDialog() |
| 9. | Override the PostNcDestroy function of the CAboutDlg class to reset the variables in the application class. void CAboutDlg::PostNcDestroy( ) |
| 10. | Rebuild the application. Then, open the About dialog box. You will see the tooltips. |
No comments:
Post a Comment