CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
可用分押宝游戏火热进行中... 专题改版:Java Web 专题
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  VC/MFC >  基础类

CListCtrl的排序问题。

楼主zizi107(zizi)2005-08-04 11:29:22 在 VC/MFC / 基础类 提问

CListCtrl类里面是不是封装有排序的函数,知道的帮写下.~!  
  如果没有给个思路先. 问题点数:100、回复次数:18Top

1 楼skymartin(天空没有留下我的痕迹,但我曾经飞过)回复于 2005-08-04 11:32:19 得分 1

有一个  
  BOOL   SortItems(  
        PFNLVCOMPARE   pfnCompare,  
        DWORD_PTR   dwData    
  );  
  函数,  
   
  Sorts   list   view   items   using   an   application-defined   comparison   function.  
  BOOL   SortItems(  
        PFNLVCOMPARE   pfnCompare,  
        DWORD_PTR   dwData    
  );  
  Parameters  
  pfnCompare    
  Address   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.    
  dwData    
  Application-defined   value   that   is   passed   to   the   comparison   function.    
  Return   Value  
  Nonzero   if   successful;   otherwise   zero.  
  Remarks  
  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   parameter   is   the   32-bit   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   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.  
        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);  
  }  
  Top

2 楼happyparrot(快乐鹦鹉)回复于 2005-08-04 11:32:41 得分 1

有现成的排序列表控件,需要源代码的话,留下信箱。Top

3 楼laiyiling(陌生人[MVP])回复于 2005-08-04 11:37:56 得分 1

http://www.vchelp.net/vchelp/zart/sortl.asp?type_id=9&class_id=1&cata_id=1&article_id=73&search_term=listctrl  
   
  文章和代码Top

4 楼zhl8011(zizi)回复于 2005-08-04 11:38:55 得分 50

既然有这个函数,我就没有必要再添加一个新的类去做这个排序的事情了吧?  
  还是把它写到新的类中?  
  Top

5 楼lixiaosan(小三)回复于 2005-08-04 11:40:54 得分 1

http://www.codeguru.com/Cpp/controls/listview/  
   
  sorting    
   
  有很多Top

6 楼happyparrot(快乐鹦鹉)回复于 2005-08-04 11:45:13 得分 1

sortitems函数需要一个回调函数。Top

7 楼happyparrot(快乐鹦鹉)回复于 2005-08-04 11:46:11 得分 1

还是做个派生类比较好。Top

8 楼zizi107(zizi)回复于 2005-08-04 11:53:39 得分 0

我的List有4个元,怎么排?  
  是不是也一样的?  
  关键是我对一列排序时,我行的元也好跟着对应的列排序而排序.  
  Top

9 楼happyparrot(快乐鹦鹉)回复于 2005-08-04 11:55:58 得分 1

排序自然是所有的行跟着一起变化了。行是一个整体阿。Top

10 楼WuOu(天堂*蓝珀湖*仰音*诚彦)回复于 2005-08-04 11:56:42 得分 2

BOOL   SortItems(  
        PFNLVCOMPARE   pfnCompare,  
        DWORD_PTR   dwData    
  );中的pfnCompare主要用于指出排序的方式(升序或降序),由于它是个回调函数,所以它必须定义为全局函或类的静态函数,呵呵,这一点很重要哦。Top

11 楼zizi107(zizi)回复于 2005-08-04 11:57:29 得分 0

哦..那我要具体研究下了,呵呵饿了,吃饭先.~~各位.!  
  Top

12 楼zizi107(zizi)回复于 2005-08-04 13:21:43 得分 0

又有个问题了..  
  int   CALLBACK   ListCompare(LPARAM   lParam1,   LPARAM   lParam2,   LPARAM   lParamSort)  
  这个函数是比较函数,其中  
  DEMO_DATA*   pInfo1=strAllData+lParam1;  
  DEMO_DATA*   pInfo2=strAllData+lParam2;  
  的strAllData是一个数组,而我的数据结构是一个CTypedPtrArray型的数组,  
  而且我的数据是CPhoneApp::CPhone    
  CPhone包含数据成员4个,  
  且有3个不同的数组我放在了一个ListCtrl中,  
  上面的比较函数如何写呢?Top

13 楼zizi107(zizi)回复于 2005-08-04 13:27:08 得分 0

是不是要通过CListCtrl::GetItemData(   int   nItem   )获取我一个位置的数据然后再比较呢?Top

14 楼WuOu(天堂*蓝珀湖*仰音*诚彦)回复于 2005-08-04 13:52:00 得分 0

把你的BOOL   SortItems(  
        PFNLVCOMPARE   pfnCompare,  
        DWORD_PTR   dwData    
  )函数的语句写出来。Top

15 楼wang_zhen_jun()回复于 2005-08-04 14:22:29 得分 1

实现点击列头排序:  
   
  定义可以排序的列表类  
  只需要多定义两个变量  
  class   SortCListCtrl   :   public   CListCtrl  
  {  
  //   Construction  
  public:  
    SortCListCtrl();  
   
  //   Attributes  
  public:  
    BOOL   m_fAsc;//是否顺序排序  
    int   m_nSortedCol;//当前排序的列  
    ....  
  }  
  在使用可以排序列表时   实例化自己的变量    
  SortCListCtrl   m_yktlist;  
   
   
  //响应点击列函数  
  void   CAuditingCertView::OnColumnclickListYkt(NMHDR*   pNMHDR,   LRESULT*   pResult)    
  {  
     
    for   (int   i   =   0;   i   <   m_yktlist.GetItemCount();   ++i)  
    {  
      m_yktlist.SetItemData(i,   i);//供排序使用的item编号  
    }  
     
    NM_LISTVIEW*   pNMListView   =   (NM_LISTVIEW*)pNMHDR;  
     
    //设置排序方式  
    if(   pNMListView->iSubItem   ==m_yktlist.m_nSortedCol   )  
      m_yktlist.m_fAsc   =   !m_yktlist.m_fAsc;  
    else  
    {  
      m_yktlist.m_fAsc   =   TRUE;  
      m_yktlist.m_nSortedCol   =   pNMListView->iSubItem;  
    }  
    //调用排序函数,此函数为CListCtrl定义好的,但是需要调用我们定义的函数才比较任意两个项目的值  
    m_yktlist.SortItems(MyListCompare,   (LPARAM)&m_yktlist);    
   
    for   (   i   =   0;   i   <   m_yktlist.GetItemCount();   ++i){  
      m_yktlist.SetItemData(i,   i);//供排序使用的item编号  
      CString   s;  
      s.Format("%d",i+1);//编号  
      m_yktlist.SetItemText(i,0,s);  
       
    }  
    *pResult   =   0;  
  }  
   
  ///全局函数,比较两个项目的依据  
  int   CALLBACK     MyListCompare(LPARAM   lParam1,   LPARAM   lParam2,   LPARAM   lParamSort)  
  {  
    //通过传递的参数来得到CSortList对象指针,从而得到排序方式  
    SortCListCtrl   *   pV=(SortCListCtrl   *)lParamSort;  
    //通过ItemData来确定数据  
    CString   szComp1,szComp2;  
    int   iCompRes;  
    szComp1=pV->GetItemText(lParam1,pV->m_nSortedCol);  
    szComp2=pV->GetItemText(lParam2,pV->m_nSortedCol);  
    switch(pV->m_nSortedCol)  
    {  
    case(0):  
      //以第一列为根据排序   编号  
      iCompRes=atof(szComp1)<=atof(szComp2)?-1:1;  
      break;  
    case(4):  
      //以第5列为根据排序   总次数  
      iCompRes=atof(szComp1)<=atof(szComp2)?-1:1;  
      break;  
    default:  
      iCompRes=szComp1.Compare(szComp2);  
      break;  
    }  
    //根据当前的排序方式进行调整  
    if(pV->m_fAsc)  
      return   iCompRes;  
    else  
      return   -iCompRes;  
     
     
  }  
   
  Top

16 楼zhl8011(zizi)回复于 2005-08-04 14:40:45 得分 0

目前只是可以对第1列排序.其它列点击就会抱错了..Top

17 楼zhl8011(zizi)回复于 2005-08-04 14:58:33 得分 0

原来加上每一列的比较就解决问题了..  
   
  还想问一下高手:如果我要混合排序呢?比如我有3个组,每组有一个组号和value  
  那我现在随便插入的,希望点击两个排序后组号有序,而且value也有序  
   
  给个思路吧..!!Top

18 楼zhl8011(zizi)回复于 2005-08-04 15:01:05 得分 40

是不是我对列排序时就不能用switch()来获取列了,或者说在switch()后面要加上复合条件的比较了  
  Top

相关问题

  • CListCtrl内容排序?
  • CListCtrl排序问题.
  • CListCtrl的排序问题(SortItems)
  • CListCtrl的排序问题
  • CListCtrl的排序问题
  • CListCtrl排序的问题!?
  • CListCtrl类控件如何排序呢?
  • 关于CListCtrl项目排序的问题!!!
  • 关于CListCtrl的排序问题?
  • CListCtrl排序的最简解决方案

关键词

  • 排序
  • 函数
  • 数据
  • application
  • yktlist
  • sortitems
  • nsortedcol
  • clistctrl
  • pfnlvcompare
  • pfncompare

得分解答快速导航

  • 帖主:zizi107
  • skymartin
  • happyparrot
  • laiyiling
  • zhl8011
  • lixiaosan
  • happyparrot
  • happyparrot
  • happyparrot
  • WuOu
  • wang_zhen_jun
  • zhl8011

相关链接

  • Visual C++类图书
  • Visual C++类源码下载

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
世纪乐知(北京)网络技术有限公司 版权所有, 京 ICP 证 020026 号
北京创新乐知广告有限公司 提供技术支持
Copyright © 2000-2007, CSDN.NET, All Rights Reserved
GongshangLogo