Friday, June 19, 2009

How to Enable Tooltip for Dialog Controls

Lets get things straight. You want the user to be more dumb and want him/her to get an idea of what he wants to do with all the controls on his screen without reading the help file? Then dive right in. Else, Ciao. Have a good day.



Step 1: Enable the tooltip command using the command EnableToolTips (TRUE). Ideal location to include this line would be in your class' init routine CYourClass::InitDialog.

Step 2: Windows sends the tooltip related controls in TTN_NEEDTEXT message. We need to handle this. Add the following code to enable your handler in the MessageMap function

BEGIN_MESSAGE_MAP(CMyClassDlg, CDialog)
ON_NOTIFY_EX_RANGE(TTN_NEEDTEXT, 0, 0xFFFF, OnToolTipNotify)
END_MESSAGE_MAP()


Step 3: Declare the handler function in your header.
afx_msg bool OnToolTipNotify( UINT id, NMHDR* pNMHDR, 
LRESULT* pResult );


Step 4: We are near. Just add then handler code in your cpp. Below is a sample of the same

bool CMyClassDlg::OnToolTipNotify( 
UINT id, NMHDR* pNMHDR, LRESULT* pResult )
{
// Get the tooltip structure.
TOOLTIPTEXT *pTTT = (TOOLTIPTEXT *)pNMHDR;

// Actually the idFrom holds Control's handle.
UINT CtrlHandle = pNMHDR->idFrom;

// Check once again that the
// idFrom holds handle itself.
if (pTTT->uFlags & TTF_IDISHWND)
{
// Get the control's ID.
UINT nID = ::GetDlgCtrlID( HWND( CtrlHandle ));

// Now you have the ID. depends on control,
// set your tooltip message.
switch( nID )
{
case ID_BUTTON1:
// Set the tooltip text.
pTTT->lpszText = _T("First Button");
break;

case ID_EDIT1:
// Set the tooltip text.
pTTT->lpszText = _T("First Edit Box");
break;

default:
// Set the tooltip text.
pTTT->lpszText = _T("Tooltips everywhere!!!");
break;
}

return TRUE;
}

// Not handled.
return FALSE;
}