Click here to Skip to main content
15,879,535 members
Articles / Desktop Programming / MFC

CTreeCtrl\CListCtrl\CListBox With ToolTip Based On the Item Data

Rate me:
Please Sign up or sign in to vote.
4.22/5 (16 votes)
24 Jan 20023 min read 200.3K   5.3K   32   18
A CTreeCtrl derived class providing tooltips based on item data

Sample Image

Introduction

Let's say you using a ctrl that supports item data (CTreeView for example) and you wish to add tool tip support, well, you can find some good articles explaining exactly what to do (look in the treeview section in code guru, Zafir Anjum's article will give you all the information you need. But, what if you want to display a custom tool tip for each item? One which is not based only on the item text? Well, I've searched for a class which will give me the freedom to display any tool tip I want and couldn't find any.

The following method will allow you to display an item tool tip, which you will be composed according to the item data, so you can, for example, show a tool tip with a phone number when your mouse is over a name in a list ctrl .

The methods will be shown through the use of a CTreeCtrl derived class, but can be used with other controllers that support set\get item data. This article will not focus on the technical sides of adding tool tip support to your controller.

I would like to thank Yaniv ben ari for his great idea.

What Is This Class?

CTreeCtrlCh is a CTreeCtrl derived class, which display tool tip according to the item data You can attach any string you like to the any tree item using SetItemData function, and this text will be displayed as tool tip.

How Does This Class Work?

This class uses an abstract class as an ItemData template. The user must derive its own ItemData class from the abstract class ,and must implement a virtual function called: GetToolTipString, this function returns a CString object which will be displayed as tool tip. The user should construct this returned CString object according to his own ItemData class. sounds a little complicated? It's not...follow me to the next section.

How To Use This Class?

  • Add the files you've downloaded to your project directory and add them to your project.
  • Add a TreeCtrl to your dialog, and assign him a member using the control wizard.
  • Add the following statement to your dialog header file: #include "TreeCtrlCh.h".
  • Change the tree control variable type to CTreeCtrlch.
  • In the dialog header file, construct a new class derived from ItemDataABSTRACT as public. This class will be used as your tree item data, add any data member you want. Also add the function CString GetToolTipString to that class.
    C++
    class TTipItemData:public ItemDataABSTRACT
    {
    public:
    
    	TTipItemData();
    
    	CString strTool;
    	POSITION pos;
    
    	CString GetToolTipString();
    };
  • In your dialog implementation file, you need to implement the GetToolTipString() function. This function MUST return a CString object, this object will be displayed as the item tool tip. In this function, you can manipulate the item data members and construct any string you want like additional information on the item, dates, size, etc .
    C++
    TTipItemData::TTipItemData() 
    {
    	strTool = "";
    	pos = NULL;
    };
    
    CString TTipItemData::GetToolTipString() //here, you compose the tool tip according to item data 
    {
    	CString tmp;
    	return strTool;
    };
  • When you to add item data, declare a pointer to the class you've derived from ItemDataABSTRACT assign data to its member, and use SetItemData to add the ItemData pointer.
    C++
    TTipItemData* p ; 
    p = new TTipItemData;
    p->strTool = "is ran";
    
    level1= m_Tree.InsertItem("");
    m_Tree.SetItemText(level1,"First Name");
    m_Tree.SetItemData(level1,DWORD(p));
  • There is no need to delete the memory allocated, the CTreeCtrlch will free all allocated memory used for item data.

License

This article has no explicit license attached to it, but may contain usage terms in the article text or the download files themselves. If in doubt, please contact the author via the discussion board below.

A list of licenses authors might use can be found here.


Written By
Web Developer
United Kingdom United Kingdom
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Generalx64 Pin
Chmely9-Nov-06 1:04
Chmely9-Nov-06 1:04 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.