Adding tooltips for item images


One thing Ive never seen ( other than in my applications ) is tooltips for the item images. I am not saying that none of the commercial application use tooltips for the item images, but Ive not seen one yet. In fact, in some of the applications searching through the help file was also fruitless. I still didnt know what the images meant.

Anyway, heres what needs to be done if you want to implement tooltips for the item images and I hope you do want to implement them.

Just one more thing before we start implementing the tooltips. Like the list view control, the new tree view control creates its own tooltip control but unlike the list view control it does so in both Win95 and WinNT. Since I couldnt find any documentation for the build in tooltip, I presume that it is undocumented and susceptible to change. Anyway, in this section, we use the MFC provided mechanism.

Step 1: Enable tool tips

Enabling tooltip for a window simply requires a call to EnableToolTips(TRUE). A good place to insert this statement is in the PreSubclassWindow(). No matter how the control is created, this function is always called by MFC. The same is not true for the OnCreate() function. OnCreate() is called only if you create the control by a call to Create() or CreateEx() and is not called if the control is created from a dialog resource.
 
void CTreeCtrlX::PreSubclassWindow() 
{
	CTreeCtrl::PreSubclassWindow();

	EnableToolTips(TRUE);
}
 

Step 2: Override the OnToolHitTest() virtual function

The MFC framework calls this function to determine whether or not to display a tooltip at the given point. The document suggests that a value of 1 be returned if the point falls over a tool ( over something that we want to display a tooltip for ). This is not true however. This function should return different non zero numbers for different tools in the window.

In this funciton we are only handling the case when the mouse is over item icon or the state icon. You may wish to add tooltips for other elements of the tree control. In both the cases we compute the bounding rectangle for the icon and we set the id equal to the handle of the tree item whose icons falls under the mouse. Note that though we use the same id for the item icon and the state icon for the same item, the return values are different. The different return value forces MFC to update the tooltip.

Although, we can specify the tooltip text in this function itself, it is best not to add too much computing in this function. This function gets called each time the mouse moves so it might be a good idea if you can optimize the function.

 

int CTreeCtrlX::OnToolHitTest(CPoint point, TOOLINFO * pTI) const
{
	RECT rect;

	UINT nFlags;
	HTREEITEM hitem = HitTest( point, &nFlags );
	if( nFlags & TVHT_ONITEMICON )
	{
		CImageList *pImg = GetImageList( TVSIL_NORMAL );
		IMAGEINFO imageinfo;
		pImg->GetImageInfo( 0, &imageinfo );

		GetItemRect( hitem, &rect, TRUE );
		rect.right = rect.left - 2;
		rect.left -= (imageinfo.rcImage.right + 2);

		pTI->hwnd = m_hWnd;
		pTI->uId = (UINT)hitem;
		pTI->lpszText = LPSTR_TEXTCALLBACK;
		pTI->rect = rect;
		return pTI->uId;
	}
	else if( nFlags & TVHT_ONITEMSTATEICON )
	{
		CImageList *pImg = GetImageList( TVSIL_NORMAL );
		IMAGEINFO imageinfo;
		pImg->GetImageInfo( 0, &imageinfo );

		GetItemRect( hitem, &rect, TRUE );
		rect.right = rect.left - (imageinfo.rcImage.right + 2);

		pImg = GetImageList( TVSIL_STATE );
		rect.left = rect.right - imageinfo.rcImage.right ;

		pTI->hwnd = m_hWnd;
		pTI->uId = (UINT)hitem;
		pTI->lpszText = LPSTR_TEXTCALLBACK;
		pTI->rect = rect;
		
		// return value should be different from that used for item icon
		return pTI->uId*2;	      
	}
	return -1;
}

Step 3: Add handler for TTN_NEEDTEXT

Add a handler for the TTN_NEEDTEXT notification message. This message is sent by the tooltip control when it needs the text to display in the tooltip. Since in the previous step, we specified LPSTR_TEXTCALLBACK for the text we need to handle this notification. The class wizard does not support this notification and the entry in the message map will have to be done manually. Good thing too, since we actually have to handle both versions, e.i. TTN_NEEDTEXTA and TTN_NEEDTEXTW. Here is what the message map looks like.
 
BEGIN_MESSAGE_MAP(CTreeCtrlX, CTreeCtrl)
	//{{AFX_MSG_MAP(CTreeCtrlX)
	:
	:
	//}}AFX_MSG_MAP
	ON_NOTIFY_EX_RANGE(TTN_NEEDTEXTW, 0, 0xFFFF, OnToolTipText)
	ON_NOTIFY_EX_RANGE(TTN_NEEDTEXTA, 0, 0xFFFF, OnToolTipText)
END_MESSAGE_MAP()
And here is what the declaration looks like in the class.
protected:
	//{{AFX_MSG(CTreeCtrlX)
	:
	:
	//}}AFX_MSG
	afx_msg BOOL OnToolTipText( UINT id, NMHDR * pNMHDR, LRESULT * pResult );
	DECLARE_MESSAGE_MAP()

And now the function itself. We have to handle the ANSI version and the UNICODE version of the message somewhat differently. We also ignore the message from the built in tooltip control. We recognize the message from the built in tooltip control by the fact that the id is equal to the window handle of the tree view control and the flags has the TTF_IDISHWND flag set. Based on the mouse cursor position we determine whether we need to supply the tooltip text for the item icon or the state icon. The code below simply supplies the icon index as the tooltip text, but of course, you would supply something meaningful.


BOOL CTreeCtrlX::OnToolTipText( UINT id, NMHDR * pNMHDR, LRESULT * pResult )
{
	// need to handle both ANSI and UNICODE versions of the message
	TOOLTIPTEXTA* pTTTA = (TOOLTIPTEXTA*)pNMHDR;
	TOOLTIPTEXTW* pTTTW = (TOOLTIPTEXTW*)pNMHDR;
	CString strTipText;
	UINT nID = pNMHDR->idFrom;

	// Do not process the message from built in tooltip 
	if( nID == (UINT)m_hWnd &&
		(( pNMHDR->code == TTN_NEEDTEXTA && pTTTA->uFlags & TTF_IDISHWND ) ||
		( pNMHDR->code == TTN_NEEDTEXTW && pTTTW->uFlags & TTF_IDISHWND ) ) )
		return FALSE;

	// Get the mouse position
	const MSG* pMessage;
	CPoint pt;
	pMessage = GetCurrentMessage();
	ASSERT ( pMessage );
	pt = pMessage->pt;
	ScreenToClient( &pt );

	UINT nFlags;
	HTREEITEM hitem = HitTest( pt, &nFlags );
	if( nFlags & TVHT_ONITEMICON )
	{
		int nImage, nSelImage;
		GetItemImage( (HTREEITEM ) nID, nImage, nSelImage );
		strTipText.Format( "Image : %d", nImage );
	}
	else
	{
		strTipText.Format( "State : %d", GetItemState( (HTREEITEM ) nID, 
				    	TVIS_STATEIMAGEMASK ) );
	}

#ifndef _UNICODE
	if (pNMHDR->code == TTN_NEEDTEXTA)
		lstrcpyn(pTTTA->szText, strTipText, 80);
	else
		_mbstowcsz(pTTTW->szText, strTipText, 80);
#else
	if (pNMHDR->code == TTN_NEEDTEXTA)
		_wcstombsz(pTTTA->szText, strTipText, 80);
	else
		lstrcpyn(pTTTW->szText, strTipText, 80);
#endif
	*pResult = 0;

	return TRUE;    // message was handled
}
 



Comments

  • Suffering From Erectile Dysfunction? Do away with It Naturally

    Posted by aveeffifielm on 06/21/2013 04:41pm

    The length of time Does It Consider To Cure Erectile Dysfunction Completely? Consult your doctor first before taking any type of medication or pill [url=http://rxhealth.pw ] viagra information pfizer [/url] see considerably more Erectile Dysfunction Treatment - herbal products and Natural Cures to make Weak Erection

    Reply
  • grxvlwgg

    Posted by ymfrgvyx on 04/23/2013 12:38am

    izoeoufi http://handtaschelouisvuittonsalz.de/ gmrgdiuh http://hollisteroutletukoffzget.co.uk/ lqfapdft http://thomassabocharmssaleofraus.com/ vkzrtemm http://buypoloralphlaurenoffrz.fr/ bhztngju http://hotzlouisvuittontaschenbillig.de/ zqqtmnrk louis vuitton speedy 25 vvueorlr hollister kids gzdpoghj thomas sabo au swiefqth polo ralph lauren bqdldkgp louis vuitton online shop sale There are a few stores that genuinely realize what that you are experiencing and also can assist you have the automotive you will need. Typically the limits set up with regard to general fault insurance cover can be quite small-scale and can in no way covers the different fees that happen to be sustained at a leading car accident. Are you searching for vehicular power parts? Want reliable service then Pricol is the destination.

    Reply
  • ksilnaya

    Posted by oyvopail on 04/21/2013 09:38pm

    yvyknywg http://louisvuittontascheoffz.de/ ijdoserk http://ofrzabercrombieandfitchoutlet.co.uk/ pmkkffys http://louisvuittongeldborsejetztt.de/ umrjynpr http://hollisterukitsale.co.uk/ tncupntr http://nowthomassabocharmscheapzaus.com/ tznrxdye louis vuitton schal nhblqdwr abercrombie and fitch outlet ogmkrwwu louis vuitton deutschland iorqcihp hollister uk lqfoxulo thomas sabo charms This Harvest The states provides you with entry to over 10, 000 name brand items when part of ones own request company. Acquire him one thing to demonstrate off. Garlic herb cloves, 10

    Reply
  • Something other people does when contemplating nike and moreover the things that youhas to do different.

    Posted by icoppyapedcap on 04/21/2013 08:47pm

    Ce [url=http://hunter-rain-boots.webnode.jp]ハンターレインブーツ[/url] vWy [url=http://hunterrainbootsjp.webnode.jp]ハンターブーツ[/url] i VjlUyy GwoB [url=http://hunter-boots8.webnode.jp]レインブーツハンター[/url] sq P [url=http://rain-boots-men.webnode.jp]長靴[/url] qm [url=http://hunter-rain-boots-ja.webnode.jp]レインブーツ人気[/url] NuuHqgLnq P[url=http://rainshoesja.webnode.jp]長靴[/url] elSlyWqrLx [url=http://ja-hunter-rain-boots.webnode.jp]レインブーツハンター[/url] k IxqDeh [url=http://rain-boots-popular.webnode.jp]レインブーツメンズ[/url] Dmx [url=http://rain-boots-men6.webnode.jp]ハンターレインブーツ[/url] Bxk Bxi [url=http://jahunterrainboots.webnode.jp]レインブーツメンズ[/url] Bln

    Reply
  • gsxiighb

    Posted by myenwlid on 04/21/2013 05:53pm

    gwcevogw http://louisvuittonoutletjetztoffz.de/ mzdcmxmt http://abercrombieandfitchoutlettz.co.uk/ vpqtspws http://louisvuittonhandtaschenoffz.de/ knucmlkd http://getithollisteruk.co.uk/ mwssqcjz http://thomassabocharmsbuyitaus.com/ pqjqqolc louis vuitton outlet iwgsyzyq abercrombie and fitch outlet emglosrg louis vuitton taschen outlet dqbsjdif hollister uk toatczkg thomas sabo charms A pair of on the completely new motifs are generally beneficial for people who want an small look, sufficient reason for Kiwi 3. A peek at Glaciers Wines Typically the dayap green tea, as well heated or perhaps iced, is frequently dished up within Philippine vegetarian restaurants.

    Reply
  • ivbjbeul

    Posted by zhozudqi on 04/21/2013 02:14pm

    vjhqmfnb http://oakleysunglassesaustraliao.co.uk/ omookehe http://casquebeatspascherq.fr/ ofzgappv http://louisvuittonwalletet.com/ hzdmttij http://hollisteroutletlet.co.uk/ ojbgqcva http://todsbagndrivingshoese.com/ ktncnhmy oakley sunglasses australia xhnmptud casque beats pas cher htramofe louis vuitton wallet yoqtrdgm hollister outlet sfcvsgtm tods shoes sale Intending halibut fishing in Alaska could be the point out regarding a keen fisherman's escape. 2. This kind of truth happens to be suggested in a number of scientific research.

    Reply
  • What the competition actually does in regards to nike and furthermore what youought to do totally different.

    Posted by icoppyapedcap on 04/21/2013 09:30am

    Sv [url=http://hunter-rain-boots.webnode.jp]レインブーツハンター[/url] hFl [url=http://hunterrainbootsjp.webnode.jp]ハンターレインブーツ[/url] g QuaSeb JwbO [url=http://hunter-boots8.webnode.jp]レインブーツメンズ[/url] kv J [url=http://rain-boots-men.webnode.jp]ブーツ[/url] la [url=http://hunter-rain-boots-ja.webnode.jp]レインブーツメンズ[/url] ZfwCrzCso A[url=http://rainshoesja.webnode.jp]長靴[/url] wvUfxTtyFd [url=http://ja-hunter-rain-boots.webnode.jp]ハンターレインブーツ[/url] b TgdXrw [url=http://rain-boots-popular.webnode.jp]レインシューズ[/url] Oxu [url=http://rain-boots-men6.webnode.jp]hunter レインブーツ[/url] Pzl Dub [url=http://jahunterrainboots.webnode.jp]レインブーツメンズ[/url] Pen

    Reply
  • This sample is completely outdated !!!!

    Posted by Legacy on 02/19/2004 12:00am

    Originally posted by: Sergey

    If you're looking for a way to add tooltips to your tree control please refer to another article: http://www.codeguru.com/treeview/TreeViewToolTips.html
    

    Reply
  • Prgram always Returning false

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

    Originally posted by: jayostu

    hi
    i have written the same code but the OnToolTipText function is always returning FALSE
    hope ur going to read this message
    bye
    jay

    Reply
  • Change background color

    Posted by Legacy on 09/07/2001 12:00am

    Originally posted by: Friedrich K�bler

    Excellent work.
    I have only a little problem. Is there any possibility to change the tiptextcolor or the backgroundcolor ?

    Reply
  • Loading, Please Wait ...

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

Top White Papers and Webcasts

  • On-demand Event Event Date: September 17, 2014 Another day, another end-of-support deadline. You've heard enough about the hazards of not migrating to Windows Server 2008 or 2012. What you may not know is that there's plenty in it for you and your business, like increased automation and performance, time-saving technical features, and a lower total cost of ownership. Check out this webcast and join Rich Holmes, Pomeroy's practice director of virtualization, as he discusses the future state of your servers, …

  • On-demand Event Event Date: September 10, 2014 Modern mobile applications connect systems-of-engagement (mobile apps) with systems-of-record (traditional IT) to deliver new and innovative business value. But the lifecycle for development of mobile apps is also new and different. Emerging trends in mobile development call for faster delivery of incremental features, coupled with feedback from the users of the app "in the wild." This loop of continuous delivery and continuous feedback is how the best mobile …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds