Support for adding sorting by clistctrl

Source: Internet
Author: User

1. Add the message Response Function and ing in the parent window.

Void cstatisticctrl: onlistpolicy (nmhdr * ppolicystruct, lresult * result)
{
Nm_listview * PLV = (nm_listview *) ppolicystruct;

Int CNT = m_list.getitemcount ();
For (INT I = 0; I <CNT; I ++)
M_list.setitemdata (I, I );

Info. m_list = & m_list; Info. Col = PLV-> isubitem;
Static STD: Map <int, int> hashtable;
STD: Map <int, int >:: iterator itor;
If (itor = hashtable. Find (PLV-> isubitem ))! = Hashtable. End ()){
Info. basc =! (Itor-> second );
Hashtable. Erase (itor );
Hashtable. insert (STD: Map <int, int >:: value_type (PLV-> isubitem, info. basc ));
} Else
{
Info. basc = 1;
Hashtable. insert (STD: Map <int, int >:: value_type (PLV-> isubitem, info. basc ));
}

M_list.sortitems (listcompare, (lparam) & info );
}

This is the message function.

On_notify (lvn_columnclick, 1403, onlistnotify) is the message ing, and 1403 is the ID of clistctrl.

Then add the response comparison function.

Typedef struct infotag {
Clistctrl * m_list;
Int basc;
Int Col;
} Info;
Info;

 

Int callback listcompare (lparam lparam1, lparam lparam2, lparam lparamsort)
{
Info * info = (Info *) lparamsort;
Cstring data1, data2;

Switch (Info-> basc ){
Case 0:
Switch (Info-> col)
{
Case 0:
Data1 = Info-> m_list-> getitemtext (lparam1, 0 );
Data2 = Info-> m_list-> getitemtext (lparam2, 0 );
If (data1> data2) return 1;
If (data1 <data2) Return-1;
Break;
Case 1:
Data1 = Info-> m_list-> getitemtext (lparam1, 1 );
Data2 = Info-> m_list-> getitemtext (lparam2, 1 );
If (data1> data2) return 1;
If (data1 <data2) Return-1;
Break;

Case 2:
Data1 = Info-> m_list-> getitemtext (lparam1, 2 );
Data2 = Info-> m_list-> getitemtext (lparam2, 2 );
If (atoi (lpctstr) data1)> atoi (lpctstr) data2) return 1;
If (atoi (lpctstr) data1) <atoi (lpctstr) data2) Return-1;
Break;
}
Break;
Case 1:
Switch (Info-> col)
{
Case 0:
Data1 = Info-> m_list-> getitemtext (lparam1, 0 );
Data2 = Info-> m_list-> getitemtext (lparam2, 0 );
If (data1 <data2) return 1;
If (data1> data2) Return-1;
Break;
Case 1:
Data1 = Info-> m_list-> getitemtext (lparam1, 1 );
Data2 = Info-> m_list-> getitemtext (lparam2, 1 );
If (data1 <data2) return 1;
If (data1> data2) Return-1;
Break;
Case 2:
Data1 = Info-> m_list-> getitemtext (lparam1, 2 );
Data2 = Info-> m_list-> getitemtext (lparam2, 2 );
If (atoi (lpctstr) data1) <atoi (lpctstr) data2) return 1;
If (atoi (lpctstr) data1)> atoi (lpctstr) data2) Return-1;
Break;
}
Break;
}
Return 0;
}

/////////////////////////////

 

There is a row of buttons at the top of the list control (clistctrl). You can sort records by selecting different columns. However, clistctrl does not support Automatic Sorting. We need to add a callback function for sorting to compare the two data sizes. In addition, we need to respond to the Message clicked by the sorting button. The following describes the specific practices.

Clistctrl provides a function for sorting. Its prototype is bool clistctrl: sortitems (pfnlvcompare pfncompare, DWORD dwdata ). The first parameter is the address of the global sorting function, and the second parameter is user data. You can transmit a data or pointer as needed. This function returns-1, indicating that the first row should be before the second item. Return 1, indicating that the first row should be behind the second item. Return 0, indicating that the two items are equal.

The original form of the function used for sorting is: int callback listcompare (lparam lparam1, lparam lparam2, lparam lparamsort ), the third parameter is the data passed by the caller (that is, the second parameter dwdata when sortitems is called ). The first and second parameters are itemdata for comparison. You can use DWORD clistctrl: getitemdata (INT nitem)/bool clistctrl: setitemdata (INT nitem, DWORD dwdata) to access the itemdata of each item. You can also set this value by selecting clistctrl: insertitem when adding an item. Because you can only use this value to determine the position of the item during sorting, you should clearly determine the meaning of this value.

Finally, we need to know when to sort the messages. To achieve this, we can process the lvn_columnclick message in the parent window.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.