gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 03:20:20 25 4
gpt4 key购买 nike

我有课

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com