CListCtrl控件使用方法总结

今天第一次用CListCtrl控件,遇到不少问题,查了许多资料,现将用到的一些东西总结如下:

以下未经说明,listctrl默认view 风格为report

相关类及处理函数

MFC:CListCtrl类

SDK:以 “ListView_”开头的一些宏。如 ListView_InsertColumn

1. CListCtrl 风格

      LVS_ICON: 为每个item显示大图标
      LVS_SMALLICON: 为每个item显示小图标
      LVS_LIST: 显示一列带有小图标的item
      LVS_REPORT: 显示item详细资料

      直观的理解:windows资源管理器,“查看”标签下的“大图标,小图标,列表,详细资料”


2. 设置listctrl 风格及扩展风格

      LONG lStyle;
      lStyle = GetWindowLong(m_list.m_hWnd, GWL_STYLE);//获取当前窗口style
      lStyle &= ~LVS_TYPEMASK; //清除显示方式位
      lStyle |= LVS_REPORT; //设置style
      SetWindowLong(m_list.m_hWnd, GWL_STYLE, lStyle);//设置style
 
      DWORD dwStyle = m_list.GetExtendedStyle();
      dwStyle |= LVS_EX_FULLROWSELECT;//选中某行使整行高亮(只适用与report风格的listctrl)
      dwStyle |= LVS_EX_GRIDLINES;//网格线(只适用与report风格的listctrl)
      dwStyle |= LVS_EX_CHECKBOXES;//item前生成checkbox控件
      m_list.SetExtendedStyle(dwStyle); //设置扩展风格
  
      注:listview的style请查阅msdn
      http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wceshellui5/html/wce50lrflistviewstyles.asp

 

3. 插入数据

      m_list.InsertColumn( 0, "ID", LVCFMT_LEFT, 40 );//插入列
      m_list.InsertColumn( 1, "NAME", LVCFMT_LEFT, 50 );
      int nRow = m_list.InsertItem(0, “11”);//插入行
      m_list.SetItemText(nRow, 1, “jacky”);//设置数据

 

4. 一直选中item

    选中style中的Show selection always,或者在上面第2点中设置LVS_SHOWSELALWAYS


5. 选中和取消选中一行

    int nIndex = 0;
    //选中
    m_list.SetItemState(nIndex, LVIS_SELECTED|LVIS_FOCUSED, LVIS_SELECTED|LVIS_FOCUSED);
    //取消选中
    m_list.SetItemState(nIndex, 0, LVIS_SELECTED|LVIS_FOCUSED);
 

6. 得到listctrl中所有行的checkbox的状态

      m_list.SetExtendedStyle(LVS_EX_CHECKBOXES);
      CString str;
      for(int i=0; i<m_list.GetItemCount(); i++)
      {
           if( m_list.GetItemState(i, LVIS_SELECTED) == LVIS_SELECTED || m_list.GetCheck(i))
           {
                str.Format(_T("第%d行的checkbox为选中状态"), i);
                AfxMessageBox(str);
           }
      }


7. 得到listctrl中所有选中行的序号


      方法一:
      CString str;
      for(int i=0; i<m_list.GetItemCount(); i++)
      {
           if( m_list.GetItemState(i, LVIS_SELECTED) == LVIS_SELECTED )
           {
                str.Format(_T("选中了第%d行"), i);
                AfxMessageBox(str);
           }
      }

      方法二:
      POSITION pos = m_list.GetFirstSelectedItemPosition();
      if (pos == NULL)
           TRACE0("No items were selected!\n");
      else
      {
           while (pos)
           {
                int nItem = m_list.GetNextSelectedItem(pos);
                TRACE1("Item %d was selected!\n", nItem);
                // you could do your own processing on nItem here
           }
      }


8. 得到item的信息

      TCHAR szBuf[1024];
      LVITEM lvi;
      lvi.iItem = nItemIndex;
      lvi.iSubItem = 0;
      lvi.mask = LVIF_TEXT;
      lvi.pszText = szBuf;
      lvi.cchTextMax = 1024;
      m_list.GetItem(&lvi);

      关于得到设置item的状态,还可以参考msdn文章
      Q173242: Use Masks to Set/Get Item States in CListCtrl
               http://support.microsoft.com/kb/173242/en-us


9. 得到listctrl的所有列的header字符串内容

      LVCOLUMN lvcol;
      char  str[256];
      int   nColNum;
      CString  strColumnName[4];//假如有4列

      nColNum = 0;
      lvcol.mask = LVCF_TEXT;
      lvcol.pszText = str;
      lvcol.cchTextMax = 256;
      while(m_list.GetColumn(nColNum, &lvcol))
      { 
           strColumnName[nColNum] = lvcol.pszText;
           nColNum++;
      }


10. 使listctrl中一项可见,即滚动滚动条

    m_list.EnsureVisible(i, FALSE);

11. 得到listctrl列数

    int nHeadNum = m_list.GetHeaderCtrl()->GetItemCount();

12. 删除所有列

      方法一:
         while ( m_list.DeleteColumn (0))
       因为你删除了第一列后,后面的列会依次向上移动。

      方法二:
      int nColumns = 4;
      for (int i=nColumns-1; i>=0; i--)
          m_list.DeleteColumn (i);


13. 得到单击的listctrl的行列号

      添加listctrl控件的NM_CLICK消息相应函数
      void CTest6Dlg::OnClickList1(NMHDR* pNMHDR, LRESULT* pResult)
      {
           // 方法一:
           /*
           DWORD dwPos = GetMessagePos();
           CPoint point( LOWORD(dwPos), HIWORD(dwPos) );
   
           m_list.ScreenToClient(&point);
   
           LVHITTESTINFO lvinfo;
           lvinfo.pt = point;
           lvinfo.flags = LVHT_ABOVE;
     
           int nItem = m_list.SubItemHitTest(&lvinfo);
           if(nItem != -1)
           {
                CString strtemp;
                strtemp.Format("单击的是第%d行第%d列", lvinfo.iItem, lvinfo.iSubItem);
                AfxMessageBox(strtemp);
           }
          */
   
          // 方法二:
          /*
           NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;
           if(pNMListView->iItem != -1)
           {
                CString strtemp;
                strtemp.Format("单击的是第%d行第%d列",
                                pNMListView->iItem, pNMListView->iSubItem);
                AfxMessageBox(strtemp);
           }
          */
           *pResult = 0;
      }

 

14. 判断是否点击在listctrl的checkbox上

      添加listctrl控件的NM_CLICK消息相应函数
      void CTest6Dlg::OnClickList1(NMHDR* pNMHDR, LRESULT* pResult)
      {
           DWORD dwPos = GetMessagePos();
           CPoint point( LOWORD(dwPos), HIWORD(dwPos) );
   
           m_list.ScreenToClient(&point);
   
           LVHITTESTINFO lvinfo;
           lvinfo.pt = point;
           lvinfo.flags = LVHT_ABOVE;
     
           UINT nFlag;
           int nItem = m_list.HitTest(point, &nFlag);
           //判断是否点在checkbox上
           if(nFlag == LVHT_ONITEMSTATEICON)
           {
                AfxMessageBox("点在listctrl的checkbox上");
           } 
           *pResult = 0;
      }


15. 右键点击listctrl的item弹出菜单

      添加listctrl控件的NM_RCLICK消息相应函数
      void CTest6Dlg::OnRclickList1(NMHDR* pNMHDR, LRESULT* pResult)
      {
           NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;
           if(pNMListView->iItem != -1)
           {
                DWORD dwPos = GetMessagePos();
                CPoint point( LOWORD(dwPos), HIWORD(dwPos) );
    
                CMenu menu;
                VERIFY( menu.LoadMenu( IDR_MENU1 ) );
                CMenu* popup = menu.GetSubMenu(0);
                ASSERT( popup != NULL );
                popup->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON, point.x, point.y, this );
           } 
           *pResult = 0;
  }


 

16. item切换焦点时(包括用键盘和鼠标切换item时),状态的一些变化顺序

      添加listctrl控件的LVN_ITEMCHANGED消息相应函数
      void CTest6Dlg::OnItemchangedList1(NMHDR* pNMHDR, LRESULT* pResult)
      {
           NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;
           // TODO: Add your control notification handler code here
    
           CString sTemp;
  
           if((pNMListView->uOldState & LVIS_FOCUSED) == LVIS_FOCUSED && 
            (pNMListView->uNewState & LVIS_FOCUSED) == 0)
           {
                sTemp.Format("%d losted focus",pNMListView->iItem);
           }
           else if((pNMListView->uOldState & LVIS_FOCUSED) == 0 &&
               (pNMListView->uNewState & LVIS_FOCUSED) == LVIS_FOCUSED)
           {
                sTemp.Format("%d got focus",pNMListView->iItem);
           } 
  
           if((pNMListView->uOldState & LVIS_SELECTED) == LVIS_SELECTED &&
            (pNMListView->uNewState & LVIS_SELECTED) == 0)
           {
                sTemp.Format("%d losted selected",pNMListView->iItem);
           }
           else if((pNMListView->uOldState & LVIS_SELECTED) == 0 &&
            (pNMListView->uNewState & LVIS_SELECTED) == LVIS_SELECTED)
           {
                sTemp.Format("%d got selected",pNMListView->iItem);
           }
    
           *pResult = 0;
      }


 

17. 改变选中行的颜色

首先是添加以下消息

ON_NOTIFY( NM_CUSTOMDRAW, IDC_LIST1, OnDrawColorForMyList )  //为改变颜色添加的消息

再添加类成员函数,就OK了:

None.gif
None.gif
//改变 m_List 控件单行的颜色
None.gif
void CSSDTDlg::OnDrawColorForMyList( NMHDR *pNmHdr, LRESULT *pResult )
ExpandedBlockStart.gif
{
InBlock.gif NMLVCUSTOMDRAW
* pLVCD = reinterpret_cast<NMLVCUSTOMDRAW*>( pNmHdr );
InBlock.gif
InBlock.gif    
*pResult = CDRF_DODEFAULT;
InBlock.gif
InBlock.gif 
if ( CDDS_PREPAINT == pLVCD->nmcd.dwDrawStage )
ExpandedSubBlockStart.gif 
{
InBlock.gif        
*pResult = CDRF_NOTIFYITEMDRAW;
ExpandedSubBlockEnd.gif }

InBlock.gif    
else if ( CDDS_ITEMPREPAINT == pLVCD->nmcd.dwDrawStage )
ExpandedSubBlockStart.gif 
{
InBlock.gif        
*pResult = CDRF_NOTIFYSUBITEMDRAW;
ExpandedSubBlockEnd.gif }

InBlock.gif    
else if ( (CDDS_ITEMPREPAINT | CDDS_SUBITEM) == pLVCD->nmcd.dwDrawStage )
ExpandedSubBlockStart.gif 
{
InBlock.gif  COLORREF clrNewTextColor, clrNewBkColor;
InBlock.gif        
InBlock.gif  
int    nItem = static_cast<int>( pLVCD->nmcd.dwItemSpec );
InBlock.gif
InBlock.gif  
//选中行显示字体为红色,否则为黑色
InBlock.gif
  if(m_item == nItem)  //设置为红色
ExpandedSubBlockStart.gif
  {
InBlock.gif   clrNewTextColor 
= RGB( 25500 );
ExpandedSubBlockEnd.gif  }

InBlock.gif  
else  //设置为黑色
ExpandedSubBlockStart.gif
  {
InBlock.gif   clrNewTextColor 
= RGB( 000 );
ExpandedSubBlockEnd.gif  }

InBlock.gif
InBlock.gif  
//设置背景色
InBlock.gif
  if( nItem%2 ==0 )
ExpandedSubBlockStart.gif  
{
InBlock.gif   clrNewBkColor 
= RGB( 240240240 ); //偶数行背景色为灰色
ExpandedSubBlockEnd.gif
  }

InBlock.gif  
else
ExpandedSubBlockStart.gif  
{
InBlock.gif   clrNewBkColor 
= RGB( 255255255 ); //奇数行背景色为白色
ExpandedSubBlockEnd.gif
  }

InBlock.gif
InBlock.gif  pLVCD
->clrText = clrNewTextColor;
InBlock.gif  pLVCD
->clrTextBk = clrNewBkColor;
InBlock.gif
InBlock.gif        
*pResult = CDRF_DODEFAULT;
ExpandedSubBlockEnd.gif }

ExpandedBlockEnd.gif}

None.gif
None.gif



更多技巧参见以下链接:
http://blog.csdn.net/danforn/archive/2008/06/03/2508070.aspx


====================================================================

MFC CListCtrl中的 CheckBox的选中与单击响应

若只是单纯实现状态改变消息响应,可采取一下方法:

即实现CListCtrl的LVN_ITEMCHANGED消息响应:

void CStatusBarDlg::OnItemchangedStateList(NMHDR* pNMHDR, LRESULT* pResult) {	LPNMLISTVIEW pNMLV = reinterpret_cast<LPNMLISTVIEW>(pNMHDR); 	if((pNMLV->uOldState & INDEXTOSTATEIMAGEMASK(1)) /* old state : unchecked */ 	   && (pNMLV->uNewState & INDEXTOSTATEIMAGEMASK(2)) /* new state : checked */ 	   ) 	{ 		TRACE("Item %d is checked\n", pNMLV->iItem);	} 	else if((pNMLV->uOldState & INDEXTOSTATEIMAGEMASK(2)) /* old state : checked */ 			&& (pNMLV->uNewState & INDEXTOSTATEIMAGEMASK(1)) /* new state : unchecked */ 			) 	{ 		TRACE("Item %d is unchecked\n", pNMLV->iItem); 	} 	else 	{ 		TRACE("Item %d does't change the check-status\n", pNMLV->iItem); 	} 	*pResult = 0;}

若想要判断是否为鼠标单击至CheckBox引起响应,则需要实现NM_CLICK消息即可:


void CStatusBarDlg::OnClickStateList(NMHDR* pNMHDR, LRESULT* pResult)
{
DWORD dwPos = GetMessagePos();  
CPoint point( LOWORD(dwPos), HIWORD(dwPos) );  

m_StatusList.ScreenToClient(&point);  

LVHITTESTINFO lvinfo;  
lvinfo.pt = point;  
lvinfo.flags = LVHT_ABOVE;  

UINT nFlag;  
int nItem = m_StatusList.HitTest(point, &nFlag);  
//判断是否点在checkbox上  
if(nFlag == LVHT_ONITEMSTATEICON)  
{
TRACE("You click CheckBox,Item will be Clicked!");
   }
*pResult = 0;
}

防止CListCtrl闪烁的几种方法

1.使用SetRedraw禁止窗口重绘,操作完成后,再恢复窗口重绘

m_ctlList.SetRedraw(FALSE); 

//以下为更新数据操作

//……

//恢复窗口重绘

m_ctlList.SetRedraw(TRUE);

2.使用LockWindowUpdate禁止窗口重绘,操作完成后,用UnlockWindowUpdate恢复窗口重绘

m_ctlList.LockWindowUpdate(); 

//以下为更新数据操作

//……

//恢复窗口重绘

m_ctlList.UnlockWindowUpdate(); 

3.使用ListCtrl的内部双缓冲

m_ctlLisit.SetExtendedStyle(m_ctlLisit.GetExtendedStyle()|LVS_EX_DOUBLEBUFFER);

VC6未定义LVS_EX_DOUBLEBUFFER宏,使用者可以自定义,如下:

#define LVS_EX_DOUBLEBUFFER 0x00010000

4.Virtual List

首先要设置ListCtrl风格为LVS_REPORT | LVS_OWNERDATA或在ListCtrl属里中的More Styles页面中选中Owner data复选框。

其次要向应LVN_GETDISPINFO消息;

void OnGetdispinfoList(NMHDR* pNMHDR, LRESULT* pResult)
{
 LV_DISPINFO* pDispInfo = (LV_DISPINFO*)pNMHDR; 

 LV_ITEM *pItem = &(pDispInfo)->item;
 char szText[128] = {0};
 if (pItem->mask & LVIF_TEXT)
 {

//使缓冲区数据与表格子项对应

//m_ArrayBuff为二维数组

//定义如下 int m_ArrayBuff[2048][4];
  _stprintf(szText,_T("%d"),m_ArrayBuff[pItem->iItem][pItem->iSubItem]);   
  pItem->pszText = szText;  
 } 
 *pResult = 0;
}

最后便是生成缓冲区数据

void Insertdata()
{

 //删除之前的数据
 m_ctlList.SetItemCountEx(0);
 m_ctlList.Invalidate();
   m_ctlList.UpdateWindow();
 srand( (unsigned)time( NULL ));
 
 //生成新的数据缓冲区
 int nItemCount = 2048;
 for (int i = 0;i < nItemCount; i ++)
 {
  for (int k = 0;k < 4;k ++)
  {
   m_ArrayBuff[i][k] = rand()%2048 + 1;
  }
 }
 if (nItemCount < 2)    
  m_ctlList.SetItemCountEx(1);   
 else
  m_ctlList.SetItemCountEx(nItemCount);
 m_ctlList.Invalidate();

若要修改数据,只要修改缓冲区m_ArrayBuff的数据即可以

5.Custom Redraw

既然是自绘,首先当然是重载CListCtrl类,并接管WM_ERASEBKGND消息,去掉默认的处理,改为不处理

BOOL CListCtrlEx::OnEraseBkgnd(CDC* pDC)
{

//响应WM_ERASEBKGND消息 
 return false;
 //屏蔽默认处理
 //return CListCtrl::OnEraseBkgnd(pDC);
}

void CListCtrlEx::OnPaint()

{

   //响应WM_PAINT消息

   CPaintDC dc(this); // device context for painting

   CRect rect;

   CRect headerRect;

   CDC MenDC;//内存ID  

   CBitmap MemMap;

   GetClientRect(&rect);   

   GetDlgItem(0)->GetWindowRect(&headerRect);  

   MenDC.CreateCompatibleDC(&dc);  

   MemMap.CreateCompatibleBitmap(&dc,rect.Width(),rect.Height());

   MenDC.SelectObject(&MemMap);

   MenDC.FillSolidRect(&rect,RGB(228,236,243));  

   //这一句是调用默认的OnPaint(),把图形画在内存DC表上  

   DefWindowProc(WM_PAINT,(WPARAM)MenDC.m_hDC,(LPARAM)0);      

   //输出  

   dc.BitBlt(0,headerRect.Height(),rect.Width(), rect.Height(),&MenDC,0, headerRect.Height(),SRCCOPY);  

   MenDC.DeleteDC();

   MemMap.DeleteObject();

}



你可能感兴趣的:(style,report,资源管理器,详细资料)