如下未經說明,listctrl默認view 風格爲report
-------------------------------------------------------------------------------
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 ); php
//新插入的在上面
int nRow = m_list.InsertItem(0, "11");// 插入行
m_list.SetItemText(nRow, 1, "jacky");//設置其它列數據 html
//新插入的數據在下面 shell
int nIndex = m_list.GetItemCount();
LV_ITEM lvItem;
lvItem.mask = LVIF_TEXT ;
lvItem.iItem = nIndex; //行數
lvItem.iSubItem = 0;
lvItem.pszText = (char*)(LPCTSTR)strCount; //第一列
//在最後一行插入記錄值.
m_list.InsertItem(&lvItem);
//插入其它列
m_list.SetItemText(nIndex,1,strLat);
--------------------------------------------------------------------------------
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;
} windows