一直用不好,不希望用派生,现在已做好一个程序,想在这个基础上给LISTCTRL加上排序功能,可是弄了一天也没弄好。
哪位高人可以给我一份源码,学习一下
万分感谢我的信箱是:[email protected]期盼。我只有20分了,别嫌少

解决方案 »

  1.   

    响应标题栏的点击事件,然后:
    ListView_SortItems Macro--------------------------------------------------------------------------------Uses an application-defined comparison function to sort the items of a list-view control. The index of each item changes to reflect the new sequence. You can use this macro or send the LVM_SORTITEMS message explicitly. SyntaxBOOL ListView_SortItems(
        HWND hwnd,
        PFNLVCOMPARE pfnCompare,
        LPARAM lParamSort
    );Parametershwnd
    Handle to the list-view control. 
    pfnCompare
    Pointer to 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. 
    lParamSort
    Application-defined value that is passed to the comparison function. 
    Return ValueReturns TRUE if successful, or FALSE otherwise.ResThe comparison function has the following form: int CALLBACK CompareFunc(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort);  The lParam1 parameter is the value associated with the first item being compared; and the lParam2 parameter is the value associated with the second item. These are the values that were specified in the lParam member of the items' LVITEM structure when they were inserted into the list. The lParamSort parameter is the same value passed to the LVM_SORTITEMS message. 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. 
    Note   During the sorting process, the list-view contents are unstable. If the callback function sends any messages to the list-view control, the results are unpredictable.
      

  2.   

    //---------list排序--------------(cpp文件)
    int m_iSortCol;//待排序列
    BOOL m_fAsc;//是否顺序排序
    static int CALLBACK ListCompare(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort)
    {
    CListCtrl *pListCtrl = (CListCtrl *)lParamSort;
    CString strItem1 = pListCtrl->GetItemText(lParam1, m_iSortCol);
    CString strItem2 = pListCtrl->GetItemText(lParam2, m_iSortCol); int nRet = strcmp(strItem1, strItem2); if (m_fAsc) nRet *= -1;
    return nRet;
    }void CPage::OnColumnclickList(NMHDR* pNMHDR, LRESULT* pResult) //添加单击列事件
    {
    NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;
    int i = m_list.GetItemCount();
    CString str,strNewData;
    CString str1;
    while(i--)
    m_list.SetItemData(i, i); if( pNMListView->iSubItem == m_iSortCol )
    {
    m_fAsc = !m_fAsc;
    }
    else
    {
    m_fAsc = TRUE;
    m_iSortCol = pNMListView->iSubItem;
    }
    m_list.SortItems( ListCompare, (DWORD)&m_list );  
    *pResult = 0;
    }