1、初始化:
CImageList m_imageList_mall, m_imageList_big; m_imageList_mall.Create(16, 16, 0, 2, 0); m_imageList_big.Create(32, 32, 0, 2, 0); HICON hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);//设置图标 m_imageList_mall.Add(hIcon);//加载图标 CListCtrl *pList = (CListCtrl*)GetDlgItem(IDC_LIST1); pList->SetImageList(&m_imageList_mall, LVSIL_SMALL); static TCHAR *item = L"xiaohaige"; pList->InsertItem(1, item, 0);//设置文本,第三个为对应的图标 pList->SetBkColor(RGB(255, 0, 0)); pList->SetTextBkColor(RGB(255, 0, 0));
2、添加事件处理:
void CMFC_ListCtrlDlg::OnLvnItemchangedList1(NMHDR *pNMHDR, LRESULT *pResult) { LPNMLISTVIEW pNMLV = reinterpret_cast<LPNMLISTVIEW>(pNMHDR); // TODO: 在此添加控件通知处理程序代码 CListCtrl *pList = (CListCtrl*)GetDlgItem(IDC_LIST1); int index = pNMLV->iItem; CString str = pList->GetItemText(index, 0); SetDlgItemText(IDC_STATIC, str); *pResult = 0; }