Q: Why should I add tooltips to my program?
A: By adding tooltips to your controls you enhance the usability of your program.
Q: How do I add tooltips to the controls in my dialog?
A: First of all you need to add member variables of type Control for your controls. In this FAQ I will assume that you have added a member variable for a CButton control called m_myButton and a CEDIT control called m_myEdit.
Then you manually add a member variable of type CToolTipCtrl to your dialog class;
Code:
//In your CDialog derived class' header file
protected:
CToolTipCtrl m_ToolTip;
Now you override the PreTranslateMessage() function in your dialog's class. This relay messages to the ToolTip control:
Code:
BOOL CMyDialog::PreTranslateMessage(MSG* pMsg)
{
m_ToolTip.RelayEvent(pMsg);
return CDialog::PreTranslateMessage(pMsg);
}
The call to RelayEvent() is necessary to pass a mouse message to the tool tip control for processing.
Last but not least we have to make sure our ToolTip control is created, then add the dialog box's controls to the ToolTip and finally activate the ToolTip control. This code should be in the WM_INITDIALOG message handler function, OnInitDialog;
Code:
BOOL CMyDialog::OnInitDialog()
{
CDialog::OnInitDialog();
//Create the ToolTip control
if( !m_ToolTip.Create(this))
{
TRACE0("Unable to create the ToolTip!");
}
else
{
// Add tool tips to the controls, either by hard coded string
// or using the string table resource
m_ToolTip.AddTool( &m_myButton, _T("This is a tool tip!"));
m_ToolTip.AddTool( &m_myEdit, IDS_MY_EDIT);
m_ToolTip.Activate(TRUE);
}
return TRUE;
}
Please note that the string for the tool tip for the CEdit control is defined the string table resource with the ID IDS_MY_EDIT.