Sort the clistcontrol list header in VC ++

Source: Internet
Author: User

Before declaring the class in the header file XXX. H, define the following content:

Struct mydata
{
Clistctrl * listctrl; // declare the object
Int isub;
Int seq; // determines whether the sequence is ascending or descending.
};

// Callback function

Inline int callback comparefuncs (lparam lparam1, lparam lparam2, lparam lparamsort)
{
Mydata * P = (mydata *) lparamsort;
Clistctrl * List = p-> listctrl;
Int isub = p-> isub;
Lvfindinfo findinfo;
Findinfo. Flags = lvfi_param;
Findinfo. lparam = lparam1;
Int iitem1 = List-> finditem (& findinfo,-1 );
Findinfo. lparam = lparam2;
Int iitem2 = List-> finditem (& findinfo,-1 );
Cstring stritem1 = List-> getitemtext (iitem1, isub );
Cstring stritem2 = List-> getitemtext (iitem2, isub );
// Char * Item1 = (char *) (lpctstr) stritem1;
// Char * item2 = (char *) (lpctstr) stritem2;
If (p-> SEQ)
// Return strcmp (Item1, item2); // The strcmp method is used on the Internet. It is a char-type comparison, so cstring uses the compare method.
Return stritem1.compare (stritem2 );
Else
// Return strcmp (item2, Item1 );
Return stritem2.compare (stritem1 );
};

///// // The callback function does not need to be written by yourself, just copy it directly ////////////////////////////////

Next, we need to instantiate the control.

I applied directly in XXX. h.

Public:
Clistctrl m_list_history;

To instantiate a control:

M_list_history.insertcolumn (0, l "task ");
M_list_history.insertcolumn (1, l "title ");
M_list_history.insertcolumn (2, l "status ");
M_list_history.insertcolumn (3, l "time ");
M_list_history.setcolumnwidth (0, 30); // decrease by 20
M_list_history.setcolumnwidth (1, 57 );////
M_list_history.setcolumnwidth (2, 48 );///
M_list_history.setcolumnwidth (3,160 );///

This is the code of my project. You can instantiate your own controls based on your project (the specific method for instantiating objects is that you can find them on the Internet)

When instantiating your control, remember to use this method: m_list_history.setitemdata (Iitem, I); // sort it

Finally, the onlvncolumnclicklisthistory event is found in the control event (this event occurs when you click the control column)

Void chistorypage: onlvncolumnclicklisthistory (nmhdr * pnmhdr, lresult * presult)
{
Lpnmlistview pnmlv = reinterpret_cast <lpnmlistview> (pnmhdr );
// Todo: add the control notification handler code here
// If (pnmlv-> isubitem = m_nsortedcol1)
Mydata * tmpp = new mydata;
// Tmpp-> listctrl = & m_list;
Tmpp-> listctrl = & m_list_history;
Tmpp-> isub = pnmlv-> isubitem;
Int sortnum = (INT) getprop (m_list_history.getsafehwnd (), L "sort_column ");
Int sortasc = (INT) getprop (m_list_history.getsafehwnd (), L "sort_direction ");
If (sortnum = pnmlv-> isubitem ){
Sortasc = (sortasc + 1) % 2;
Setprop (m_list_history.getsafehwnd (), L "sort_direction", (handle) sortasc );
}
Setprop (m_list_history.getsafehwnd (), L "sort_column", (handle) pnmlv-> isubitem );
Tmpp-> seq = sortasc;
M_list_history.sortitems (& comparefuncs, (lparam) tmpp );
* Presult = 0;
}

You can modify the code by referring to your project, so that the click column of the control is sorted.

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.