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



About the Author

John Z. Czopowik VC++ MVP

Microsoft VC++ MVP

Comments

  • Using this Method with Visual C++ Version 5

    Posted by sweeneymini on 06/16/2005 07:12am

    I have the upto date MFC DLL AFAIK, so can I use the new functionality of this version (e.g. using TVN_GETINFOTIP) or am I restricted to using what MFC version it came with?

    Reply
  • Delay Time ?

    Posted by Legacy on 06/03/2003 12:00am

    Originally posted by: Yury

    This samble very good and simple. But i have unpleasant problem with delay time. 
    
    This code does not make any effect. WHY ???

    CToolTipCtrl* pTTCtrl = MyTree.GetToolTips();
    if(pTTCtrl != NULL)
    {
    pTTCtrl->SetDelayTime(TTDT_AUTOPOP, 2000);
    pTTCtrl->SetDelayTime(TTDT_INITIAL, 2000);
    pTTCtrl->SetDelayTime(TTDT_RESHOW, 2000);
    }

    The tooltipCtrl of tree use default value=500 anyway.

    • Delay Time ?

      Posted by yotamwimmer on 12/12/2010 05:41pm

      Were you able to solve this issue?

      Reply
    Reply
  • tree_view_newbie

    Posted by Legacy on 11/18/2002 12:00am

    Originally posted by: c_k


    Would need some easy Tutorial, to implement a Tree View.
    Your exampe might be good, but where to insert the code.
    (SDI ?, Doc/View ?????)

    Reply
  • Some corrections

    Posted by Legacy on 08/28/2002 12:00am

    Originally posted by: Giraud

    message handler:
    
    

    afx_msg void OnTvnGetInfoTip (NMHDR* pNMHDR, LRESULT* pResult);

    and some ";" in the source code

    don't forget to remove TVS_NOTOOLTIPS style
    when you add TVS_INFOTIP

    ModifyStyle(TVS_NOTOOLTIPS, TVS_INFOTIP);

    and it works correctly !

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Event Date: April 15, 2014 The ability to effectively set sales goals, assign quotas and territories, bring new people on board and quickly make adjustments to the sales force is often crucial to success--and to the field experience! But for sales operations leaders, managing the administrative processes, systems, data and various departments to get it all right can often be difficult, inefficient and manually intensive. Register for this webinar and learn how you can: Align sales goals, quotas and …

  • Live Event Date: August 14, 2014 @ 2:00 p.m. ET / 11:00 a.m. PT Data protection has long been considered "overhead" by many organizations in the past, many chalking it up to an insurance policy or an extended warranty you may never use. The realities of today make data protection a must-have, as we live in a data driven society. The digital assets we create, share, and collaborate with others on must be managed and protected for many purposes. Check out this upcoming eSeminar and join eVault Chief Technology …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds