How to display custom tooltips in CTreeCtrl?

I have a class derived from CTreeCtrl. In OnCreate()I replace the default object with CToolTipCtrla custom one:

int CMyTreeCtrl::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
    if (CTreeCtrl::OnCreate(lpCreateStruct) == -1)
        return -1;

    // Replace tool tip with our own which will
    // ask us for the text to display with a TTN_NEEDTEXT message
    CTooltipManager::CreateToolTip(m_pToolTip, this, AFX_TOOLTIP_TYPE_DEFAULT);
    m_pToolTip->AddTool(this, LPSTR_TEXTCALLBACK);
    SetToolTips(m_pToolTip);

    // Update: Added these two lines, which don't help either
    m_pToolTip->Activate(TRUE);
    EnableToolTips(TRUE);

    return 0;
}

My message handler looks like this:

ON_NOTIFY_EX(TTN_NEEDTEXT, 0, &CMyTreeCtrl::OnTtnNeedText)

However, I never get a message TTN_NEEDTEXT. I looked with Spy ++, and it looks like this message is never sent.

What could be the problem?

Update

I'm not sure if this is important: the parent window CTreeCtrlis of type CDockablePane. Could there be some extra work for this?

+3
source share
5 answers

At last! I (partially) solved this:

It seems that the parent window of CDockablePane really caused this problem ...

, , , CTreeCtrl. .

OnCreate():

int CMyPane::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
    if (CDockablePane::OnCreate(lpCreateStruct) == -1)
        return -1;

const DWORD dwStyle = WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CLIPCHILDREN |
    TVS_CHECKBOXES | TVS_DISABLEDRAGDROP | TVS_HASBUTTONS | TVS_HASLINES | TVS_LINESATROOT |
    TVS_INFOTIP | TVS_NOHSCROLL | TVS_SHOWSELALWAYS;

// TREECTRL_ID is a custom member constant, set to 1
if(!m_tree.Create(dwStyle, m_treeRect, this, TREECTRL_ID ) )
{
    TRACE0("Failed to create trace tree list control.\n");
    return -1;
}

// m_pToolTip is a protected member of CDockablePane
m_pToolTip->AddTool(&m_tree, LPSTR_TEXTCALLBACK, &m_treeRect, TREECTRL_ID);
m_tree.SetToolTips(m_pToolTip);


return 0;

}

, AddTool() , ASSERT uFlag, . , . CRect (0, 0, 10000, 10000) CTor. , . . : .

, :

// Message map entry
ON_NOTIFY(TVN_GETINFOTIP, TREECTRL_ID, &CMobileCatalogPane::OnTvnGetInfoTip)


// Handler
void CMyPane::OnTvnGetInfoTip(NMHDR *pNMHDR, LRESULT *pResult)
{
    LPNMTVGETINFOTIP pGetInfoTip = reinterpret_cast<LPNMTVGETINFOTIP>(pNMHDR);

    // This is a CString member
    m_toolTipText.ReleaseBuffer();
    m_toolTipText.Empty();

    // Set your text here...

    pGetInfoTip->pszText = m_toolTipText.GetBuffer();

    *pResult = 0;
}
+4

, , .

EnableToolTips(TRUE);

, , , , . , :

  • , OnCreate() rotine .
  • , .
  • , , . (, , , , .)

    //

    EnableToolTips (TRUE),

    //

    CToolTipCtrl * pToolTipCtrl = (CToolTipCtrl *) CWnd:: FromHandle ((HWND):: SendMessage (m_hWnd, LVM_GETTOOLTIPS, 0, 0L));

+4

CTreeCtrl, , RelayEvent ctrl, , . :

MyTreeCtrl.h:

virtual BOOL PreTranslateMessage(MSG* pMsg);

MyTreeCtrl.cpp:

BOOL CMyTreeCtrl::PreTranslateMessage(MSG* pMsg) 
{
    m_pToolTip.Activate(TRUE);
    m_pToolTip.RelayEvent(pMsg);

    return CTreeCtrl::PreTranslateMessage(pMsg);
}

, .

+1

OnToolHitTest()?

() 1

() 2:

(nHit), TOOLINFO. , VIRGIL CMainFrame:: OnToolHitTest:

 int nHit = MAKELONG(pt.x, pt.y);
 pTI->hwnd = m _ hWnd;
 pTI->uId  = nHit;
 pTI->rect = CRect(CPoint(pt.x-1,pt.y-1),CSize(2,2));
 pTI->uFlags |= TTF _ NOTBUTTON;
 pTI->lpszText = LPSTR _ TEXTCALLBACK;

- hwnd uId, . 2 2 , . "", , . TTF _ NOTBUTTON uFlags, . MFC, afxwin.h; MFC . MFC , TTF _ ALWAYSTIP. , , MFC , . , , MFC TOOLINFO, . LPSTR _ TEXTCALLBACK. (, -, MFC), , . , WM_ NOTIFY TTN _ NEEDTEXT.

+1
source

Try to specially process all the tooltips:

ON_NOTIFY_EX_RANGE(TTN_NEEDTEXT, 0, 0xFFFF, &CMyTreeCtrl::OnNeedTipText)

If this does not work, you may need to manually call RelayEvent () from PreTranslateMessage ().

0
source

Source: https://habr.com/ru/post/1699233/


All Articles