How to enable tooltip for your dialog controls?

28 04 2008


Have a number of controls in your dialog. May be a single line of tooltip will add beauty to it. So how can you add tooltips for the controls in your dialog? Its easy. Just follow the steps.


Basically you’ve to enable tooltips by calling EnableToolTips() and then handle the TTN_NEEDTEXT message in your dialog. When its time to show a tooltip, windows will send you the TTN_NEEDTEXT message with control’ handle and you’ve to specify the tooltip text to be shown. From handle you can get Dialog control ID. Once you have the control ID, you can specify which tooltip have to be shown for that control. See it step by step. I assume you use MFC. Eventhough you follow the tradition style, just go through the steps, you can easily grasp it.

1) Call EnableToolTips() in yout dialog initialize function. The best place is in dialog initialization routine – CDialog::OnInitDialog().
2) Now add message handler for TTN_NEEDTEXT in messagemap. The following codesnippet enables tooltip notification for all controls in your dialog.

BEGIN_MESSAGE_MAP(CDialogDlg, CDialog)
    ...
    ON_NOTIFY_EX_RANGE(TTN_NEEDTEXT, 0, 0xFFFF, OnToolTipNotify)
END_MESSAGE_MAP()

3) Now declare the tooltip handler in your header as follows.

afx_msg BOOL OnToolTipNotify( UINT id,
                              NMHDR* pNMHDR,
                              LRESULT* pResult );

4) Now define the tooltip handler in your cpp file as follows

BOOL CDialogDlg::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 IDC_BUTTON1:
                // Set the tooltip text.
                pTTT->lpszText = _T("First Button");
            break;

        case IDC_BUTTON2:
            // Set the tooltip text.
            pTTT->lpszText = _T("Second Button");
            break;

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

        return TRUE;
    }

    // Not handled.
    return FALSE;
}


Targeted Audience – Beginners.

Advertisements

Actions

Information

4 responses

30 04 2008
Nishad S

I think this is not a easier method…

Have a look at this.

BOOL CMyDlg::OnInitDialog()
{


m_tooltip.Create( this ); // Member variable (CToolTipCtrl m_tooltip)
m_tooltip.AddTool( GetDlgItem( IDOK ), “OK button” );
m_tooltip.AddTool( GetDlgItem( IDCANCEL ), “Cancel button” );


}

BOOL CTooltipTestDlg::PreTranslateMessage(MSG* pMsg)
{
m_tooltip.RelayEvent( pMsg );
return CDialog::PreTranslateMessage(pMsg);
}

Nothing else to do… Hope this is simple.

30 04 2008
Jijo.Raj

Indeed. If CToolTipCtrl can be used.

17 10 2008
Joe A.

Thank you for posting this tip. I just want to make it a little more clear on where to place some of the functions. Otherwise, the wrong location will cause ClassWizard error when trying to view/edit in MFC ClassWizard.

In step 2)
BEGIN_MESSAGE_MAP(CDlgDiagnostics, CDialog)
//{{AFX_MSG_MAP(CDlgDiagnostics)

//}}AFX_MSG_MAP
ON_NOTIFY_EX_RANGE(TTN_NEEDTEXTW, 0, 0xFFFF, OnToolTipNotify)
ON_NOTIFY_EX_RANGE(TTN_NEEDTEXTA, 0, 0xFFFF, OnToolTipNotify)
END_MESSAGE_MAP()

In Step 3)
//{{AFX_MSG(CDlgDiagnostics)

//}}AFX_MSG
afx_msg BOOL OnToolTipNotify( UINT id, NMHDR* pNMHDR, LRESULT* pResult );
DECLARE_MESSAGE_MAP()

12 05 2009
losthound

Nice info. Thanks

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s




%d bloggers like this: