Handling Title Tips With Drag/Drop Headers Using The Visual C++ 6.0 CListCtrl | CodeGuru

Handling Title Tips With Drag/Drop Headers Using The Visual C++ 6.0 CListCtrl

I wanted to add the title-tip class to the new version of the CListCtrl in Visual C++ 6.0. However, as has previously been pointed out on this site, the code doesn’t quite work for a List Control with the LVS_EX_HEADERDRAGDROP style enabled. The code fix previously posted is more complicated than neccessary with the new […]

Written By
CodeGuru Staff
CodeGuru Staff
Sep 2, 1998
1 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

I wanted to add the title-tip class to the new version of the CListCtrl in Visual C++ 6.0. However, as has previously been pointed out on this site, the code doesn’t quite work for a List Control with the LVS_EX_HEADERDRAGDROP style enabled. The code fix previously posted is more complicated than neccessary with the new CListCtrl class in VC 6, and I was able to modify Zafir Anjum’s CellRectFromPoint() function to handle all header cases simply. Just modify the CellRectFromPoint() code to look like the following:

int CTRListCtrl::CellRectFromPoint(CPoint & point, RECT * cellrect, int * col)
{
	int colnum;

	// Make sure that the ListView is in LVS_REPORT
	if( (GetStyle() & LVS_TYPEMASK) != LVS_REPORT )
		return -1;

	// Get the top and bottom row visible	
	int row = GetTopIndex();
	int bottom = row + GetCountPerPage();
	if( bottom > GetItemCount() )
		bottom = GetItemCount();

	// Get the number of columns
	CHeaderCtrl* pHeader = CListCtrl::GetHeaderCtrl();
	int nColumnCount = pHeader->GetItemCount();

	// Loop through the visible rows
	for( ;row <= bottom; row++)
	{
		// Get bounding rect of item and check whether point falls in it.		
		CRect rect;
		GetItemRect( row, &rect, LVIR_BOUNDS );

		// We use the rects of the header items to determine if the point falls in a
		// given column. Thus, the order of the columns is no longer important.

		CRect colRect;

		if( rect.PtInRect(point) )
		{
			// Now find the column			
			for( colnum = 0; colnum < nColumnCount; colnum++ )
			{
				pHeader->GetItemRect(colnum, &colRect);

				if(  (point.x >= colRect.left) && (point.x <= (colRect.left + colRect.Width()) ) )
				{
					// Found the column
					RECT rectClient;
					GetClientRect( &rectClient );
					if( point.x > rectClient.right )
						return -1;

					if( col )
						*col = colnum;

					rect.right = colRect.left + colRect.Width();
					rect.left = colRect.left;

					if( rect.right > rectClient.right )
						rect.right = rectClient.right;

					*cellrect = rect;
					return row;
				}
			}
		}
	}
	return -1;
}


CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.