user1311286
user1311286

Reputation:

MFC tooltips, GetDlgItemText does not return the text to use for tooltip

I recently have been adding tooltips to each dialog item in my application. Prior to adding each to a string resource I wanted to do it in hardcoded text so I can change it easily as I am writing them. Now it has come time to pull strings from the resource files and it seems that I cannot get one to come out and display as a tooltip.

The code below produces a blank tooltip. Though if I replace tmpStr with a real string such as [_T("Tool Tip Text")] it works fine.

Code:

BOOL CCustomDialog::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)
    {

        CString tmpStr;
        if( nID == IDC_BUTTON1)
        {
            GetDlgItemText(IDS_BUTTON1_TT, tmpStr);
            _tcsncpy_s(pTTT->szText, tmpStr, _TRUNCATE);
        }
        *pResult = 0;
        return TRUE;
    }
    return FALSE;
}

What could be the cause of this?

EDIT: If I put the controls ID of the control I wish to display a tooltip on it, it works and shows the controls description as the text. If I add a String resource in the resource file that the control is located in, the string resource will not come out as a tooltip.

So it seems that this is only a problem with the String resources?

Upvotes: 1

Views: 1437

Answers (1)

Roger Rowland
Roger Rowland

Reputation: 26259

In the following section of code

    if( nID == IDC_BUTTON1)
    {
        GetDlgItemText(IDS_BUTTON1_TT, tmpStr);
        _tcsncpy_s(pTTT->szText, tmpStr, _TRUNCATE);
    }

It looks like you have a button with an ID of IDC_BUTTON1 with an associated text string in your resource file with ID of IDS_BUTTON1_TT.

If that's true, then you need to use tmpStr.LoadString(IDS_BUTTON1_TT) to get the text. Don't use GetDlgItemText() unless you want the text of the button control, then you need to use it's ID of IDC_BUTTON1 instead. So, do it like this

    if( nID == IDC_BUTTON1)
    {
        tmpStr.LoadString(IDS_BUTTON1_TT);
        _tcsncpy_s(pTTT->szText, tmpStr, _TRUNCATE);
    }

Upvotes: 1

Related Questions