1. 常用初始化
CListCtrl* pList = (CListCtrl*)GetDlgItem(IDC_LIST_MEETING_ER);
pList->ModifyStyle(0, LVS_REPORT| LVS_SHOWSELALWAYS | LVS_SINGLESEL); //
pList->SetExtendedStyle(LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES | LVS_EX_HEADERDRAGDROP);
pList->InsertColumn(0, _T("会议ID"), LVCFMT_LEFT, 50);
pList->InsertColumn(1, _T("会议名称"), LVCFMT_LEFT, 150);
pList->InsertColumn(2, _T("会议时间"), LVCFMT_LEFT, 130);
pList->InsertColumn(3, _T("会议议程"), LVCFMT_LEFT, 200);
2. 获得选中项(单项)
CListCtrl *listCtrl = (CListCtrl*)GetDlgItem(IDC_LIST_PARTICIPANT);
// 这里一定要判断, 否则在没有列表项时, 下面会报错
if(listCtrl->GetItemCount() <= 0)
{
return ;
}
// 如果列表项为0时, 该函数会报错
int nSel = *listCtrl->GetNextItem(-1, LVNI_SELECTED);
if(nSel < 0)
{
return;
}
3. 获取选中项(多项)
CListCtrl *pList = (CListCtrl*)GetDlgItem(IDC_LIST_PARTICIPANT);
POSITION pos = pList->GetFirstSelectedItemPosition();
while(pos)
{
nSel = pList->GetNextSelectedItem(pos);
}
4. 添加复选框, 并且把复选框放到最后一列
CListCtrl* pCListCtrl = (CListCtrl*)GetDlgItem(IDC_LIST1);
pCListCtrl->ModifyStyle(0, LVS_REPORT| LVS_SHOWSELALWAYS | LVS_SINGLESEL); //
// LVS_EX_HEADERDRAGDROP | LVS_EX_CHECKBOXES属性必须要
pCListCtrl->SetExtendedStyle(LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES | LVS_EX_HEADERDRAGDROP | LVS_EX_CHECKBOXES);
pCListCtrl->InsertColumn(0, _HYL("选择"), LVCFMT_LEFT, 60);
pCListCtrl->InsertColumn(1, _HYL("A"), LVCFMT_LEFT, 60);
pCListCtrl->InsertColumn(2, _HYL("B"), LVCFMT_LEFT, 150);
pCListCtrl->InsertColumn(3, _HYL("B"), LVCFMT_LEFT, 150);
pCListCtrl->InsertColumn(4, _HYL("B"), LVCFMT_LEFT, 150);
pCListCtrl->InsertColumn(5, _HYL("B"), LVCFMT_LEFT, 150);
// 把复选框放到最后一列
CHeaderCtrl *pCHeaderCtrl = pCListCtrl->GetHeaderCtrl();
int nCount = pCHeaderCtrl->GetItemCount();
int* pA = new int[nCount];
//pCHeaderCtrl->GetOrderArray(pA, nCount);
int i;
for(i = 0; i < nCount; ++i)
{
pA[i] = i + 1;
}
pA[nCount - 1] = 0;
pCHeaderCtrl->SetOrderArray(nCount, pA);
delete [] pA;
pA = 0;
//
// How to get notification when an item is checked / unchecked:
void DemoDlg::OnItemchangedLinksList(NMHDR* pNMHDR, LRESULT* pResult)