Search code examples
c++windowsmfctooltip

MFC tooltips only show up on special occasions


I have been tasked with assigning tooltips to each item in a configuration menu. I have completed "adding" the tooltip to each control on the page, but it seems sometimes the tooltip shows up and sometimes it does not, depending on the position of the control on the screen.

To tooltip-erize the pages I first

EnableToolTips(TRUE);

In each CPropertyPage's OnInitDialog method. I then add the notification map

ON_NOTIFY_EX(TTN_NEEDTEXT, 0, OnToolTipText)

With the function OnToolTipText looking as such

BOOL CCfgPrefPage::OnToolTipText( UINT id, NMHDR * pNMHDR, LRESULT * pResult )
{
    TOOLTIPTEXT *pTTT = (TOOLTIPTEXT *)pNMHDR;
    UINT nID = pNMHDR->idFrom;

    if (pTTT->uFlags & TTF_IDISHWND)
    {
        nID = ::GetDlgCtrlID((HWND)nID);
        if(nID)
        {
            if( nID == GetDlgItem(IDC_PICKDIST_EDIT)->GetDlgCtrlID())  
                _tcsncpy_s(pTTT->szText, _T("Tool Tip Text"), _TRUNCATE);
            else if( nID == GetDlgItem(IDC_ENDPTTOL_EDIT)->GetDlgCtrlID())  
                _tcsncpy_s(pTTT->szText, _T("Tool Tip Text"), _TRUNCATE);

            pTTT->lpszText = pTTT->szText; // Sanity Check
            pTTT->hinst = AfxGetResourceHandle(); // Don't think this is needed at all
            return TRUE;
        }
    }
    return FALSE;
}

It seems for some of my controls the tool tip will not show up. For most of the check box controls the tool tip displays, but for a few they just do not show. There are no other controls covering them up, they are not disabled.

Another thing, if I use the non-standard cursor windows repeatedly flashes the tool tip, so much so it is unreadable in some cases. How can I fix this? This is not a problem on CEdit controls, so why is it a problem elsewhere?

EDIT: Update, the controls that have been on the pages for years seem to show tool tips. Any control that I try to add now/today will not show tool tips at all. No matter the position, control type, settings, I cannot get a single tool tip to show on a newly inserted control.


Solution

  • If you do not want to use helper class I have proposed then fix the problems in your code. First, use ON_NOTIFY_EX_RANGE macro when mapping the even handler, like this (this will cover all IDs):

    ON_NOTIFY_EX_RANGE(TTN_NEEDTEXTA, 0, 0xFFFF, OnToolTipText)
    

    Next, you need to fix your function. I see a few problems here. First, when testing for TTF_IDISHWND flag you only need to re-initalise the nID. You do not need to apply this to the whole function. Second, after all manipulations, your nID will be the actual dialog ID. There is no need to GetDlgItem() function

    BOOL CCfgPrefPage::OnToolTipText( UINT id, NMHDR * pNMHDR, LRESULT * pResult )
    {
        TOOLTIPTEXT *pTTT = (TOOLTIPTEXT *)pNMHDR;
        UINT nID = pNMHDR->idFrom;
    
        if (pTTT->uFlags & TTF_IDISHWND)
        {
            nID = ::GetDlgCtrlID((HWND)nID);
        }
        if(nID)
        {
            if( nID == IDC_PICKDIST_EDIT)  
                _tcsncpy_s(pTTT->szText, _T("Tool Tip Text"), _TRUNCATE);
            else if( nID == IDC_ENDPTTOL_EDIT)  
                _tcsncpy_s(pTTT->szText, _T("Tool Tip Text"), _TRUNCATE);
    
            //pTTT->lpszText = pTTT->szText; // Sanity Check
            *pResult = 0;
            return TRUE;
        }
        return FALSE;
    }