How do I use a derived CListCtrl with a CListView?

WEBINAR:
On-Demand

Application Security Testing: An Integral Part of DevOps



The short answer is - you don't. The long answer is that you derive from CListView instead and add the same functionality to the CListView derived class. The reason for this is that MFC is designed in such a way that only one C++ object can be associated with one window or control. When you use a CListView, the list view control is already associated with the view class so you cannot have another C++ object associated with the control.

The call to GetListCtrl() actually returns a pointer to the CListView object after casting it to a pointer to CListCtrl. So the return value from GetListCtrl() doesn't really point to a CListCtrl but instead it points to the ClistView.

The workaround is that you derive from CListView and add the same functions and method handlers that you have for the CListCtrl. The class wizard supports all the same window message for ClistView derived class that it does for the CListCtrl class. In this scenario, we use the old form of code reuse, we copy and paste the code.

Let's assume that one of the messages that the derived CListCtrl handles is the LVN_ENDLABELEDIT notification. To add this functionality to the CListView derived class, simply use the class wizard to add the message handler, copy the code from the CListCtrl derived class and finally precede all calls to CListCtrl methods with 'GetListCtrl()->'. Here's an example to illustrate the point.

void CMyListCtrl::OnEndLabelEdit(LPNMHDR pnmhdr, LRESULT *pLResult)
{
	LV_DISPINFO 	*plvDispInfo = (LV_DISPINFO *)pnmhdr;
 	LV_ITEM	*plvItem = &plvDispInfo->item;

	if (plvItem->pszText != NULL)
		SetItemText(plvItem->iItem, plvItem->iSubItem, plvItem->pszText);
}

void CMyListView::OnEndLabelEdit(LPNMHDR pnmhdr, LRESULT *pLResult)
{
	LV_DISPINFO 	*plvDispInfo = (LV_DISPINFO *)pnmhdr;
 	LV_ITEM	*plvItem = &plvDispInfo->item;

	if (plvItem->pszText != NULL)
		GetListCtrl().SetItemText(plvItem->iItem, 
				plvItem->iSubItem, plvItem->pszText);
}



Comments

  • You must have javascript enabled in order to post comments.

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

Top White Papers and Webcasts

  • Using Internet of Things (IoT) technologies, colleges and universities are linking anything and everything to create new applications that elevate the campus experience. It's called a Smart Campus. And it's transforming campus life for students, faculty and visitors. When everything is connected, devices and applications can interact with users and with each other in ways that weren't possible before: digital classrooms, connected campus ID cards, connected lighting and building environmental systems and …

  • Global data deduplication provides important benefits over traditional deduplication processes because it removes redundant data through entire enterprises, not just single devices. Global deduplication increases the data deduplication ratio — the size of the original data measured against the size of the data store after redundancies are removed. This helps reduce the amount of storage required at a time when businesses face exponential storage growth. This eBook explores the chief benefits of global …

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date