Sort in ListCtrl

zhaozj2021-02-17  45

// Transfer from MFC Development Guide,

http://www.vchelp.net/

Sort in ListCtrl

Wonderful WYY_CQ@21cn.com

http://www.vchelp.net/

The top of the list control (CListCtrl has a row button, and the user can sort the records by selecting a different column. But CListCtrl does not automatically sort, we need to add a callback function for sorting to compare the size of the two data, and therefore also need to respond to the sort button clicked message. The specific practice will be described below.

CListCtrl provides a function for sorting, the function is: BOOL ClistCtrl :: SortItems (PfnlvCompare PFNCompare, DWORD DWDATA). Where the first parameter is the address of the global sort function, the second parameter is user data, you can pass a data or pointers according to your needs. This function returns -1 represents the first item in front of the second item, and returns 1 represents the first item in the second item, and returns 0 represents two levels.

The function of the sorted function is: int Callback ListCompare (LParam LPARAM1, LPARAM LPARAM2, LPARAM LPARMSORT), where the third parameter is the data passed by the caller (ie, the second parameter DWDATA when calling SortItems). The first and second parameters are two itemdata for comparison, you can pass DWord ClistCtrl :: GetItemData (int NITEM) / BOOL Clistctrl :: Setitemdata (int NITEM, DWORD DWDATA) to perform each item ItemData access. This value can also be set when you select a specific CListCtrl :: INSERTITEM when adding items. Since you can only determine the location of the item by this value when you are sorting, you should define the meaning of this value.

In the last point, we need to know when to sort, implement this, can be processed in the parent window to process the LVN_COLUMNCLICK message.

Let's take a look at an example, this example is a derived class and supports both sequential / reverse order. For simple me to sort global data, there will be multiple groups of data that need to be sorted in practical applications, so you need to tell the sending function to sort any data by passing the parameters.

// Global data

Struct demo_data

{

Char szname [20];

Int IAGE;

} stralldata [5] = {{"王", 30}, {"Zhang Mou", 40}, {"Wu Mou", 32}, {"Chen", 20}, {"Li Sou", 36 }};

// ClistCtrl derived class definition

Class Csortlist: Public ClistCtrl

{

// construction

PUBLIC:

Csortlist ();

BOOL M_FASC; / / Sequential order

INT M_NSORTEDCOL; / / Current Sort Column

protected:

// {{AFX_MSG (csortlist)

//}} AFX_MSG

...

}

// In the parent window, contains the CListCtrl derived class object

Class csort_in_list_ctrldlg: public cdialog

{

// construction

PUBLIC:

Csort_in_list_ctrldlg (cwnd * pparent = null); // Standard Constructor

// Dialog Data

// {{AFX_DATA (csort_in_list_ctrldlg)

Enum {IDD = IDD_SORT_IN_LIST_CTRL_DIALOG}; csortlist m_listtest;

//}} AFX_DATA

}

/ / Define LVN_COLUMNCLICK message mapping in the parent window

Begin_MESSAGE_MAP (Csort_in_list_ctrldlg, cdialog)

// {{AFX_MSG_MAP (csort_in_list_ctrldlg)

ON_NOTIFY (LVN_COLUMNCLICK, IDC_LIST1, ONCOLUMNCLICKLIST1)

//}} AFX_MSG_MAP

END_MESSAGE_MAP ()

//Initialization data

BOOL CSORT_IN_LIST_CTRLDLG :: OnInitDialog ()

{

CDIALOG :: OnInitdialog ();

// Initialize the list of data in ListCtrl

M_ListTest.insertColumn (0, "Name");

M_ListTest.InsertColumn (1, "age");

M_ListTest.SetColumnWidth (0,80);

M_ListTest.SetColumnWidth (1,80);

For (int i = 0; i <5; i )

{

M_ListTest.Insertitem (i, stralldata [i] .szname);

Char szage [10];

Sprintf (Szage, "% D", StrallData [i] .IAGE);

M_ListTest.SetItemText (i, 1, szage);

/ / Set each itemData as an index of data in an array

/ / Determine data by this itemData in the sort function

M_ListTest.SetItemData (i, i);

}

Return True; // Return True UnsS you set the focus to a control

}

// Processing a message

Void csort_in_list_ctrldlg :: ONCOLUMNCLICKLIST1 (NMHDR * PNMHDR, LRESULT * PRESULT)

{

NM_ListView * pnmlistview = (nm_listview *) PNMHDR;

// Setting the sort method

IF (pnmlistview-> isubitem == m_listtest.m_nsortedcol)

m_listtest.m_fasc =! m_listtest.m_fasc;

Else

{

m_listtest.m_fasc = true;

m_listtest.m_nsortedcol = pnmlistview-> isubitem;

}

// Call the sort function

M_ListTest.SortItems (ListCompare, (DWORD) & m_listtest);

* PRESULT = 0;

}

// Sort function implementation

Int Callback ListCompare (LParam Lparam1, Lparam Lparamsort)

{

// Get the csortlist object pointer by passing the parameters to get a sort mode

Csortlist * pv = (csortlist *) lparamsort;

// Determine the data via itemData

Demo_data * pinfo1 = stralldata lparam1;

Demo_data * pinfo2 = stralldata lparam2;

CSTRING SZCOMP1, SZComp2;

INT iCompre;

Switch (pv-> m_nsortedcol)

{

Case (0):

// Sort by the first list

Szcomp1 = pinfo1-> szname;

Szcomp2 = pinfo2-> szname;

Icome = szcomp1.compare (SZComp2);

Break;

Case (1):

// According to the second list

IF (Pinfo1-> IAGE == PINFO2-> IAGE)

ICompRES = 0;

Else

Icome = (Pinfo1-> IAGE IAGE)? - 1: 1;

Break;

DEFAULT:

Assert (0);

Break;

}

/ / Adjust according to the current sorting mode

IF (pv-> m_fasc)

Return iComprees;

Else

Return iComprees * -1;

}

Download this article Demonstration code

转载请注明原文地址:https://www.9cbs.com/read-28747.html

New Post(0)