先看一下這個控件有些什麼屬性吧:函數
1.樣式View:Report表格,list列表code
2.設置風格:SetExtendedStyle事件
SetExtendedStyle函數的定義形式爲:it
DWORD SetExtendedStyle(DWORD dwNewStyle );
參數:dwNewStyle:CListCtrl的擴展樣式(LVS_EX_GRIDLINES //繪製表格;LVS_EX_SUBITEMIMAGES//子項目圖標列表;LVS_EX_CHECKBOXES //帶複選框;LVS_EX_TRACKSELECT //自動換行;LVS_EX_HEADERDRAGDROP//報表頭能夠拖拽;LVS_EX_FULLROWSELECT //選擇整行;LVS_EX_ONECLICKACTIVATE//單擊激活;LVS_EX_TWOCLICKACTIVATE//雙擊激活;LVS_EX_FLATSB//扁平滾動條;LVS_EX_REGIONAL;LVS_EX_INFOTIP;LVS_EX_UNDERLINEHOT;LVS_EX_UNDERLINECOLD;LVS_EX_MULTIWORKAREAS//多工做區)io
SetExtendedStyle函數的具體用法如例:ast
m_list.SetExtendedStyle(m_list.GetExtendedStyle() | LVS_EX_CHECKBOXES | LVS_EX_FULLROWSELECT );
3.滾動條位置Left Scrollbar:滾動條在左側仍是右側class
4.MFC的表格不容許編輯,若是想編輯只能在編輯開始事件中把單元格處理成Edit,在編輯結束後把edit的值更新到list裏:cli
(1)首先屬性Edit Labels設爲true;擴展
(2)增長事件響應函數:List
ON_NOTIFY(NM_DBLCLK, IDC_LIST_COORD, &CCoordManageDlg::OnNMDblclkListCoord) ON_NOTIFY(LVN_ENDLABELEDIT, IDC_LIST_COORD, &CCoordManageDlg::OnLvnEndlabeleditListCoord)
雙擊事件:
void CCoordManageDlg::OnNMDblclkListCoord(NMHDR *pNMHDR, LRESULT *pResult) { LPNMITEMACTIVATE pNMItemActivate = reinterpret_cast<LPNMITEMACTIVATE>(pNMHDR); *pResult = 0; POSITION pos = m_listCoord.GetFirstSelectedItemPosition(); int nSel = m_listCoord.GetNextSelectedItem(pos); if ( nSel >= 0 ) { m_listCoord.SetFocus(); CEdit* pEdit = m_listCoord.EditLabel( nSel ); } }
edit編輯結束事件
void CCoordManageDlg::OnLvnEndlabeleditListCoord(NMHDR *pNMHDR, LRESULT *pResult) { NMLVDISPINFO *pDispInfo = reinterpret_cast<NMLVDISPINFO*>(pNMHDR); *pResult = 0; CString strName = pDispInfo->item.pszText; int nSel = pDispInfo->item.iItem; m_listCoord.SetFocus(); if( ( ! strName.IsEmpty() ) && nSel >= 0 ) m_listCoord.SetItemText( nSel, 0, strName); }
(5)點擊表頭,獲取列頭的編號
ON_NOTIFY(HDN_ITEMCLICK, 0, &CStartDlg::OnHdnItemclickListStartmonitor)
void CStartDlg::OnHdnItemclickListStartmonitor(NMHDR *pNMHDR, LRESULT *pResult) { NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR; int nRow = pNMListView->iItem; switch(nRow) { case 0: break; case 1: break; } }
(6)獲取當前選擇項
POSITION pos = GetFirstSelectedItemPosition(); if (pos != NULL) { int iSubItem = GetNextSelectedItem(pos); }