Issue in sorting Items in a CListCtrl in MFC

Posted by

MFC in Visual C++
6.0 has a problem with header notifications for the ListView control.
Although a handler can be added, in the current version it isn't
called. For instance, use Class Wizard or the WizardBar to add a
Windows Message Handler. If the ID for the ListView control is
highlighted, a number of notification messages are available for
selection. To sort the items when the header is clicked for a given
column, select the notification HDN_ITEMCLICK. An ON_NOTIFY message map
entry is generated, as well as a handler function. For the current
example, the entry appears as follows:

ON_NOTIFY(HDN_ITEMCLICK, IDC_LIST1, OnItemclickList1)

The
problem here is that the notification doesn't actually originate from
the ListView control; instead, the Header control created by the
ListView sends the notification. The message map entry listed above
does not work. The fix is simple, however, since the Header control
always has an ID of 0, the macro can be edited to work correctly:

ON_NOTIFY(HDN_ITEMCLICK, 0, OnItemclickList1)

Then, in the OnItemclickList1 handler, the SortItems call is made:

Code:

void CSortListDlg::OnItemclickList1(NMHDR* pNMHDR, LRESULT* pResult)
{
        NMLISTVIEW *pLV = (NMLISTVIEW *) pNMHDR;
       
        m_ctlListView.SortItems(SortFunc, pLV->iItem);
       
        *pResult = 0;
}

The
notification message header (NMHDR) is actually a ListView
notification, NMLISTVIEW, that contains the index to the column that
was clicked. In this example, this is represented by iItem. More
complex lists might need to reference the iSubItem element of this
structure as well. The address of the callback function is passed to
SortItems, along with the column number which was clicked.

Read more: http://support.microsoft.com/kb/250614

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *