Displaying Information in a CTreeView ToolTip

This article was contributed by John Czopowik.

Environment:

There is some confusion in the MFC world regarding CTreeView and tooltips. It seems as though the conservative approach of handling TTN_NEEDTEXTW and TTN_NEEDTEXTA notification messages does not work for some, known-only-to-Microsoft, reason. The following code requires comctl32.dll version 4.71 or later. Windows with IE ver 4.0 has this DLL updated already. A newer tree common control has an additional style: You can use the setting style of tree view: TVS_INFOTIP. In turn, the tree control will send notification message TVN_GETINFOTIP when the control is requesting text for tooltips. The handler receives a pointer to NMTVGETINFOTIP structure that has information about the tree item.

In this example, I set tootips to an item text. In the header file of the CTreeView derived class, declare a message handler:

afx_msg void OnTvnGetInfoTip NMHDR pNMHDR LRESULT pResult

And in cpp, use following macro:

BEGIN_MESSAGE_MAP (CTreeViewTestView, CTreeView)
    .
    .
    .
    ON_NOTIFY_REFLECT (TVN_GETINFOTIP, OnTvnGetInfoTip)
END_MESSAGE_MAP()

And define the handler:

void CTestTreeView::OnTvnGetInfoTip(NMHDR *pNMHDR,
                                    LRESULT *pResult)

{
  LPNMTVGETINFOTIP pGetInfoTip = (LPNMTVGETINFOTIP)pNMHDR

  CString csItemTxt =
     m_TreeCtrl.GetItemText(pGetInfoTip->hItem);

  strcpy(pGetInfoTip->pszText, csItemTxt);

  *pResult = 0;

}

Don’t forget to add a ModifyStyle command in OnInitialUpdate:

GetTreeCtrl().ModifyStyle(0,
                          TVS_HASLINES |
                          TVS_LINESATROOT |
                          TVS_HASBUTTONS |
                          TVS_INFOTIP

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read