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);
CListCtrl *listCtrl = (CListCtrl*)GetDlgItem(IDC_LIST_PARTICIPANT); // 这里一定要判断, 否则在没有列表项时, 下面会报错 if(listCtrl->GetItemCount() <= 0) { return ; } // 如果列表项为0时, 该函数会报错 int nSel = *listCtrl->GetNextItem(-1, LVNI_SELECTED); if(nSel < 0) { return; }
CListCtrl *pList = (CListCtrl*)GetDlgItem(IDC_LIST_PARTICIPANT); POSITION pos = pList->GetFirstSelectedItemPosition(); while(pos) { nSel = pList->GetNextSelectedItem(pos); }
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)
{
NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;
*pResult = 0;
if (pNMListView->uOldState == 0 && pNMListView->uNewState == 0)
return; // No change
BOOL bPrevState = (BOOL)(((pNMListView->uOldState &
LVIS_STATEIMAGEMASK)>>12)-1); // Old check box state
if (bPrevState < 0) // On startup there's no previous state
bPrevState = 0; // so assign as false (unchecked)
// New check box state
BOOL bChecked=(BOOL)(((pNMListView->uNewState & LVIS_STATEIMAGEMASK)>>12)-1);
if (bChecked < 0) // On non-checkbox notifications assume false
bChecked = 0;
if (bPrevState == bChecked)// No change in check box
return;
// Now bChecked holds the new check box state
// ....
}
// for this to work, you must map the following message:
ON_NOTIFY(LVN_ITEMCHANGED, IDC_MYLIST, OnItemchangedLinksList)
// Setting the check box state of an item:
// Try the following piece of code
//设置CheckBox的状态代码
void SetLVCheck (WPARAM ItemIndex, BOOL bCheck)
{
ListView_SetItemState (m_lvTestList.m_hWnd, ItemIndex,
UINT((int(bCheck) + 1) << 12), LVIS_STATEIMAGEMASK);
}
// 单选 SendMessage(hSelectBox, LB_SETCURSEL, i, 0); // 多选 SendMessage(hSelectBox, LB_SETSEL, 1, i); // 选择nSel, 并且可见 m_UserList.SetItemState(nSel, LVIS_SELECTED | LVIS_FOCUSED, LVIS_SELECTED | LVIS_FOCUSED); m_UserList.EnsureVisible(nSel, FALSE); // int m_Selected = 80; m_listctrl.EnsureVisible(m_Selected, FALSE); //滚动条自动滚动到第80行 m_listctrl.SetExtendedStyle(LVS_EX_FULLROWSELECT); //设置高亮显示属性 m_listctrl.SetFocus(); //set list focus m_listctrl.SetItemState(m_Selected, LVIS_SELECTED, LVIS_SELECTED); //选中行
// 添加消息映射 ON_NOTIFY(LVN_ITEMCHANGED, IDC_MYLIST, OnItemchangedLinksList) // 消息处理函数 void CDlg::OnItemchangedLinksList(NMHDR* pNMHDR, LRESULT* pResult) { NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR; *pResult = 0; if (pNMListView->uOldState == 0 && pNMListView->uNewState == 0) return; // No change // Old check box state BOOL bPrevState = (BOOL)(((pNMListView->uOldState & LVIS_STATEIMAGEMASK)>>12)-1); if (bPrevState < 0) // On startup there's no previous state bPrevState = 0; // so assign as false (unchecked) // New check box state BOOL bChecked = (BOOL)(((pNMListView->uNewState &LVIS_STATEIMAGEMASK)>>12)-1); if (bChecked < 0) // On non-checkbox notifications assume false bChecked = 0; if (bPrevState == bChecked) // No change in check box return; // Now bChecked holds the new check box state // .... }
// MFC CListCtrl中的 CheckBox的选中与单击响应若只是单纯实现状态改变消息响应,可采取一下方法: // 即实现CListCtrl的LVN_ITEMCHANGED消息响应: void CDlg::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 CDlg::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; } // http://blog.163.com/chenluyu0910@126/blog/static/99042293201373115353711
void CDlgXXXX::OnLvnItemchangedListXXX(NMHDR *pNMHDR, LRESULT *pResult) { LPNMLISTVIEW pNMLV = reinterpret_cast<LPNMLISTVIEW>(pNMHDR); // TODO: Add your control notification handler code here *pResult = 0; // 增加这一句, 避免响应多次 if(LVIF_STATE == pNMLV->uChanged && (pNMLV->uNewState & LVIS_SELECTED)) { // your code ... } }