CListCtrl 一些使用技巧(更新中....)

来源:互联网 发布:数据调查公司 编辑:程序博客网 时间:2024/05/16 07:31

1、

问:CListCtrl 用DeleteItem删除中间项后,后面的项没有往上推,怎么解决?


答:可以在之后调用Arrange, CListCtrl::Arrange( UINT nCode ) 。


示例:

// Align all of the list view control items along the top

// of the window (the list view control must be in icon or

// small icon mode).


m_myListCtrl.Arrange(LVA_ALIGNTOP);

 

2、

问:

利用: 
CListCtrl   m_List; 
POSITION   pos; 
pos=m_List.GetFirstSelectedItemPosition(); 
int   nItem=m_List.GetNextSelectedItem(pos); 
m_List.DeleteItem(nItem   ); 
删除指定行后,如何让剩余行仍按序排序? 

如                 删除2行后             我需要 
1                         1                           1 
2                         3                           2 
3                         4                           3 
4                         5                           4 
5 

 

答:

BOOL   SortItems( 
      PFNLVCOMPARE   pfnCompare, 
      DWORD_PTR   dwData   
); 

//   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); 
}