在CListView的列标题怎样实现鼠标单击自动排序?
就是列标题上有一个表示排列顺序的向上/向下箭头?是否需要给CListView增加一个style值?

解决方案 »

  1.   

    参考:
    CListCtrl::SortItems
    BOOL SortItems( PFNLVCOMPARE pfnCompare, DWORD dwData );Return ValueNonzero if successful; otherwise zero.ParameterspfnCompareAddress of the application-defined comparison function. The comparison function is called during the sort operation each time the relative order of two list items needs to be compared. The comparison function must be either a static member of a class or a stand alone function that is not a member of any class.dwDataApplication-defined value that is passed to the comparison function.ResSorts list view items using an application-defined comparison function. The index of each item changes to reflect the new sequence.The comparison function has the following form:int CALLBACK CompareFunc(LPARAM lParam1, LPARAM lParam2, 
       LPARAM lParamSort);The comparison function must return a negative value if the first item should precede the second, a positive value if the first item should follow the second, or zero if the two items are equivalent.The lParam1 and lParam2 parameters specify the item data for the two items being compared. The lParamSort parameter is the same as the dwData value.Example// Sort the item in reverse alphabetical order.
    static int CALLBACK 
    MyCompareProc(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort)
    {
       // lParamSort contains a pointer to the list view control.
       // The lParam of an item is just its index.
       CListCtrl* pListCtrl = (CListCtrl*) lParamSort;
       CString    strItem1 = pListCtrl->GetItemText(lParam1, 0);
       CString    strItem2 = pListCtrl->GetItemText(lParam2, 0);   return strcmp(strItem2, strItem1);
    }void snip_CListCtrl_SortItems()
    {
       // The pointer to my list view control.
       extern CListCtrl* pmyListCtrl;   // Sort the list view items using my callback procedure.
       pmyListCtrl->SortItems(MyCompareProc, (LPARAM) pmyListCtrl);
    }