Click here to Skip to main content
15,867,568 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.1K   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

 
GeneralHaving problems getting this to work Pin
vachaun24-Jul-08 5:08
vachaun24-Jul-08 5:08 
Generalmore detail Pin
supfar31-Oct-07 16:05
supfar31-Oct-07 16:05 
Generalx64 Pin
Chmely9-Nov-06 1:04
Chmely9-Nov-06 1:04 
GeneralI have the same needness about MultiLine tips Pin
HarveyLiu4-Jul-04 17:46
HarveyLiu4-Jul-04 17:46 
You did a good job! At the same time,
Can you give me any advise to make the tips info show in multiline? Thanks so much!Confused | :confused:

I find the solution by google, so, I modify the msg I just posted Smile | :)

Your can visit this link to get the solution:
http://www.codeguru.com/Cpp/controls/listview/tooltiptitletip/article.php/c4161/

In fact, the most important thing is to get the tooltip pointer,and call the function SetMaxTipWidth(500); By this way, then you add "\r\n" to the tooltip text, you can see the multiline now.

Add the follow codes to the begining about your function :
OnToolTipText( UINT id, NMHDR * pNMHDR, LRESULT * pResult )
{
// I want to implement this in PreSubclassWindow(), but it crashes.
if(!m_bToolTipCtrlCustomizeDone)
{
_AFX_THREAD_STATE* pThreadState = AfxGetThreadState();
CToolTipCtrl *pToolTip = pThreadState->m_pToolTip;
// Set max tip width in pixel.
// you can change delay time, tip text or background color also. enjoy yourself!
pToolTip->SetMaxTipWidth(500);
m_bToolTipCtrlCustomizeDone = TRUE;
}

// need to handle both ANSI and UNICODE versions of the message
......
......
}
GeneralRe: I have the same needness about MultiLine tips Pin
Krangla14-Sep-04 4:20
Krangla14-Sep-04 4:20 
GeneralRe: I have the same needness about MultiLine tips Pin
venkat920028-Aug-07 4:38
venkat920028-Aug-07 4:38 
GeneralErrors if want buffer > 80 Pin
l_d_allan5-Jan-04 12:33
l_d_allan5-Jan-04 12:33 
GeneralRe: Errors if want buffer > 80 Pin
Samuele1-Jul-04 5:21
Samuele1-Jul-04 5:21 
GeneralMemory Leak Pin
pleaseDeleteMyAccount26-Jun-03 4:42
pleaseDeleteMyAccount26-Jun-03 4:42 
GeneralRe: Memory Leak Pin
RayCool10-Jul-03 1:13
RayCool10-Jul-03 1:13 
GeneralIn Place Tooltip Pin
PoloM124-Apr-03 6:36
PoloM124-Apr-03 6:36 
GeneralMULTIPLE LINE TOOLTIP Pin
mister trunks11-Dec-02 10:31
mister trunks11-Dec-02 10:31 
GeneralProgram Error!! Pin
barquing4-Dec-02 1:09
sussbarquing4-Dec-02 1:09 
GeneralThis does not work for a tree control added in a class derived from CView Pin
Anonymous14-Oct-02 2:18
Anonymous14-Oct-02 2:18 
GeneralA few questions Pin
Thomas Freudenberg28-Feb-02 8:17
Thomas Freudenberg28-Feb-02 8:17 
GeneralRe: A few questions Pin
2-Mar-02 12:15
suss2-Mar-02 12:15 
GeneralDowload links do not work... Pin
25-Jan-02 8:50
suss25-Jan-02 8:50 
GeneralRe: Dowload links do not work... Pin
i_a_z27-Sep-05 20:26
i_a_z27-Sep-05 20:26 

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.