c++ - 在 PropertyPage 上的控件上显示工具提示。将 TRUE 返回到 ON_NOTIFY_EX(TTN_NEEDTEXT...) 时崩溃

标签 c++ mfc tooltip

我有课

class CCfgUserPage : public CPropertyPage

它还拥有各种控件,从复选框到文本区域。我想为每个控件添加工具提示,但似乎有问题。

在 CCfgUserPage 中,我将其添加到消息映射中

ON_NOTIFY_EX(TTN_NEEDTEXT, 0, OnToolTipText )

当此对象捕获到该消息时,它会调用如下所示的函数 OnToolTipText

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

    int partOrient = GetDlgItem(IDC_PARTORIENT_CHECK)->GetDlgCtrlID();

    if (pTTT->uFlags & TTF_IDISHWND)
    {
        // idFrom is actually the HWND of the tool
        nID = ::GetDlgCtrlID((HWND)nID);
        if( nID == partOrient ) // Only Display TT for The buttons with these ID's
        {
            if( nID == partOrient )
                ttStr = "Part Orient";
            pTTT->lpszText = (LPTSTR)(LPCTSTR)ttStr;
            pTTT->hinst = AfxGetResourceHandle();
            return TRUE;
        }
    }
    return FALSE;
}

我还在

中启用了工具提示

CCfgUserPage::OnInitDialog

只要 OnToolTipText 返回 TRUE,应用程序就会崩溃并通知我

Access violation reading location

我正在尝试通过堆栈框架,但它对我来说深入 MFC 以了解出了什么问题。我可能遗漏了什么会导致这种情况发生?

最佳答案

看看你在 MSDN 上的提示:

When you handle the TTN_NEEDTEXT notification message, specify the string to be displayed in one of the following ways:

  • Copy the text to the buffer specified by the szText member.
  • Copy the address of the buffer that contains the text to the lpszText member.
  • Copy the identifier of a string resource to the lpszText member, and copy the handle of the instance that contains the resource to the hinst member.

所以不要这样做:

        CString ttStr;
        // ...
        if( nID == partOrient )
            ttStr = "Part Orient";
        // Below is the unsafe part: you initialize lpszText with something
        //       expected to be valid after you return from the handler
        //       effectively, this is internal buffer of local ttStr valriable
        //       which is to be freed and lpszText would keep point to undefined
        //       memory
        pTTT->lpszText = (LPTSTR)(LPCTSTR)ttStr;
        pTTT->hinst = AfxGetResourceHandle();

你宁愿:

        if(nID == partOrient)
        {
            // NOTE: Here instead you don't create any dynamic instances (strings)
            //       and the value resides directly in the notification structure
            _tcsncpy_s(pTTT->szText, _T("Part Orient"), _TRUNCATE);
            pTTT->lpszText = pTTT->szText; // Just a safety, it's already pointing there
        }

关于c++ - 在 PropertyPage 上的控件上显示工具提示。将 TRUE 返回到 ON_NOTIFY_EX(TTN_NEEDTEXT...) 时崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15618235/

相关文章:

c++ - 一个关于内存使用的简单C++问题

c++ - 如何使用 call 和 alias 指令在 llvm 字节码中定位函数名称?

c++ - 如何防止不同插件中出现重复的资源 ID?

c++ - Windows API 中的逻辑坐标和设备坐标混淆

c# - 带有简单 DLL 的 SEHException

c++ - 矩形之间的碰撞检测 - 产生的角度

c++ - 在 msgbox 中显示当前系统时间

jquery - 基础工具提示不起作用;而是显示默认浏览器工具提示

qt - 使用带有标签的 QML 工具提示

javascript - 将 React 工具提示与样式化的组件一起使用