我想找一个带排序功能的列表视图类,就像资源管理器那样,我从网上下载了一个类CSortListCtrl 但它是继承于CListCtrl类的,而我在程序中使用的是ListView,为实现排序功能,不知如何做,望高手指点。

解决方案 »

  1.   

    CListCtrl本来就有排序的功能
    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);
    }
      

  2.   

    专门有个排序函数的,允许你在排序中自己手动排序,控件自动调用这个function,函数名称我忘记了,你去codeproject找找,有例子的,要不等我回家给你看一下代码
      

  3.   

    自己实现
    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.
      CListCtrl* pmyListCtrl;  // Sort the list view items using my callback procedure.
      pmyListCtrl->SortItems(MyCompareProc, (LPARAM) pmyListCtrl);
    }并处理LVN_COLUMNCLICK消息。
      

  4.   

    楼目回复!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    首先,感谢各参与,上述各位大虾们都说得很好,基本上是那么实现,但我的困惑是:
    我手头上有一个类CSortListCtrl,从Codeproject下载,基功能可实现列表视图的排序,并有自绘表头,当用记点击列标题时,不仅可以排序列表内容,而且列标题上可改变为一个向上或向下的箭头。我想直接使用它,但它派生于ClistCtrl类,而我的程序不使用对话框方式,我的视图类是一个ClistVeiw类,假如我想使用CSortListCtrl,应该怎么做呢?感谢各位帮助!!
    有关CSortListCtrl类的定义见下贴,再次感谢各位大侠参与。
      

  5.   

    //这是我下载的CSortListCtrl
    /*----------------------------------------------------------------------
    Copyright (C)2001 MJSoft. All Rights Reserved.
              This source may be used freely as long as it is not sold for
    profit and this copyright information is not altered or removed.
    Visit the web-site at www.mjsoft.co.uk
    e-mail comments to [email protected]
    File:     SortHeaderCtrl.h
    Purpose:  Provides the header control, with drawing of the arrows, for
              the list control.
    ----------------------------------------------------------------------*/#ifndef SORTHEADERCTRL_H
    #define SORTHEADERCTRL_H#if _MSC_VER > 1000
    #pragma once
    #endif // _MSC_VER > 1000class CSortHeaderCtrl : public CHeaderCtrl
    {
    // Construction
    public:
    CSortHeaderCtrl();// Attributes
    public:// Operations
    public:// Overrides
    // ClassWizard generated virtual function overrides
    //{{AFX_VIRTUAL(CSortHeaderCtrl)
    public:
    virtual void Serialize(CArchive& ar);
    //}}AFX_VIRTUAL// Implementation
    public:
    virtual ~CSortHeaderCtrl(); void SetSortArrow( const int iColumn, const BOOL bAscending ); // Generated message map functions
    protected:
    void DrawItem( LPDRAWITEMSTRUCT lpDrawItemStruct ); int m_iSortColumn;
    BOOL m_bSortAscending; //{{AFX_MSG(CSortHeaderCtrl)
    // NOTE - the ClassWizard will add and remove member functions here.
    //}}AFX_MSG DECLARE_MESSAGE_MAP()
    };//{{AFX_INSERT_LOCATION}}
    // Microsoft Visual C++ will insert additional declarations immediately before the previous line.#endif // SORTHEADERCTRL_H
    ///////////////////////////////////////////////////////////////////////////
    /*----------------------------------------------------------------------
    Copyright (C)2001 MJSoft. All Rights Reserved.
              This source may be used freely as long as it is not sold for
    profit and this copyright information is not altered or removed.
    Visit the web-site at www.mjsoft.co.uk
    e-mail comments to [email protected]
    File:     SortListCtrl.h
    Purpose:  Provides a sortable list control, it will sort text, numbers
              and dates, ascending or descending, and will even draw the
    arrows just like windows explorer!
    ----------------------------------------------------------------------*/#ifndef SORTLISTCTRL_H
    #define SORTLISTCTRL_H#if _MSC_VER > 1000
    #pragma once
    #endif // _MSC_VER > 1000#ifndef SORTHEADERCTRL_H
    #include "SortHeaderCtrl.h"
    #endif // SORTHEADERCTRL_H#ifdef _DEBUG
    #define ASSERT_VALID_STRING( str ) ASSERT( !IsBadStringPtr( str, 0xfffff ) )
    #else // _DEBUG
    #define ASSERT_VALID_STRING( str ) ( (void)0 )
    #endif // _DEBUG
    class CSortListCtrl : public CListCtrl
    {
    // Construction
    public:
    CSortListCtrl();// Attributes
    public:// Operations
    public:
    BOOL SetHeadings( UINT uiStringID );
    BOOL SetHeadings( const CString& strHeadings ); int AddItem( LPCTSTR pszText, ... );
    BOOL DeleteItem( int iItem );
    BOOL DeleteAllItems();
    void LoadColumnInfo();
    void SaveColumnInfo();
    BOOL SetItemText( int nItem, int nSubItem, LPCTSTR lpszText );
    void Sort( int iColumn, BOOL bAscending );
    BOOL SetItemData(int nItem, DWORD dwData);
    DWORD GetItemData(int nItem) const;// Overrides
    // ClassWizard generated virtual function overrides
    //{{AFX_VIRTUAL(CSortListCtrl)
    protected:
    virtual void PreSubclassWindow();
    //}}AFX_VIRTUAL// Implementation
    public:
    virtual ~CSortListCtrl(); // Generated message map functions
    protected:
    static int CALLBACK CompareFunction( LPARAM lParam1, LPARAM lParam2, LPARAM lParamData );
    void FreeItemMemory( const int iItem );
    BOOL CSortListCtrl::SetTextArray( int iItem, LPTSTR* arrpsz );
    LPTSTR* CSortListCtrl::GetTextArray( int iItem ) const; CSortHeaderCtrl m_ctlHeader; int m_iNumColumns;
    int m_iSortColumn;
    BOOL m_bSortAscending; //{{AFX_MSG(CSortListCtrl)
    afx_msg void OnColumnClick(NMHDR* pNMHDR, LRESULT* pResult);
    afx_msg void OnDestroy();
    //}}AFX_MSG DECLARE_MESSAGE_MAP()
    };//{{AFX_INSERT_LOCATION}}
    // Microsoft Visual C++ will insert additional declarations immediately before the previous line.#endif // SORTLISTCTRL_H
      

  6.   

    用ListView控件即可实现,当点击你要排序的列的列头时,响应你自己的排序算法。