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.
Back to the top
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)
{
if (NULL != m_pToolTip)
m_pToolTip->RelayEvent(pMsg);
return CDialog::PreTranslateMessage(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
{
public:
CAboutDlg();
// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
CButton m_btOK;
//}}AFX_DATA
CToolTipCtrl* m_pToolTip;
//...
}; |
| 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)
{
m_pToolTip = NULL;
}
CAboutDlg::~CAboutDlg()
{
delete m_pToolTip;
} |
| 5. | 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;
} |
| 6. | Rebuild the application. Then, open the About dialog box. You will
see the tooltips. |
For versions of MFC earlier than version 4.0, 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. | Include the <Afxcmn.h> header file in the Stdafx.h file.
|
| 3. | 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;
//...
}; |
| 4. | Initialize the two variables in the application's constructor to NULL.
CTooltipsApp::CTooltipsApp()
{
m_hwndDialog = NULL;
m_gpToolTip = NULL;
} |
| 5. | 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);
} |
| 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
{
public:
CAboutDlg();
// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
CButton m_btOK;
//}}AFX_DATA
CToolTipCtrl* m_pToolTip;
//...
}; |
| 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)
{
m_pToolTip = NULL;
}
CAboutDlg::~CAboutDlg()
{
delete m_pToolTip;
} |
| 8. | 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
} |
| 9. | 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;
} |
| 10. | Rebuild the application. Then, open the About dialog box. You will
see the tooltips. |
Back to the top