MFC中listctrl的使用

时间:2022-09-21 18:55:16

MFC中listCtrl的使用

前一阵子做MFC项目用到了listCtrl控件,今天刚好有空,整理了一下。废话不多说,看代码。
右键菜单:

/*右键菜单*/
void CStudentMngDlg::OnNMRClickStudentList(NMHDR *pNMHDR, LRESULT *pResult)
{
LPNMITEMACTIVATE pNMItemActivate = reinterpret_cast<LPNMITEMACTIVATE>(pNMHDR);
if (pNMItemActivate->iItem != -1)
{
DWORD dwPos = GetMessagePos();
CPoint point(LOWORD(dwPos), HIWORD(dwPos));

CMenu menu;
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;
}
/*item切换焦点时(包括用键盘和鼠标切换item时),状态的一些变化顺序*/
void CStudentMngDlg::OnLvnItemchangedStudentList(NMHDR *pNMHDR, LRESULT *pResult)
{
LPNMLISTVIEW pNMListView = reinterpret_cast<LPNMLISTVIEW>(pNMHDR);
// TODO: 在此添加控件通知处理程序代码
CString sTemp;

if ((pNMListView->uOldState & LVIS_FOCUSED) == LVIS_FOCUSED &&
(pNMListView->uNewState & LVIS_FOCUSED) == 0)
{
sTemp.Format("%d losted focus", pNMListView->iItem);
}
else if ((pNMListView->uOldState & LVIS_FOCUSED) == 0 &&
(pNMListView->uNewState & LVIS_FOCUSED) == LVIS_FOCUSED)
{
sTemp.Format("%d got focus", pNMListView->iItem);
}

if ((pNMListView->uOldState & LVIS_SELECTED) == LVIS_SELECTED &&
(pNMListView->uNewState & LVIS_SELECTED) == 0)
{
sTemp.Format("%d losted selected", pNMListView->iItem);
}
else if ((pNMListView->uOldState & LVIS_SELECTED) == 0 &&
(pNMListView->uNewState & LVIS_SELECTED) == LVIS_SELECTED)
{
sTemp.Format("%d got selected", pNMListView->iItem);
}
*pResult = 0;
}
/*获得listctrl中选中单行的Item信息*/
void CStudentMngDlg::OnNMClickStudentList(NMHDR *pNMHDR, LRESULT *pResult)
{
LPNMITEMACTIVATE pNMItemActivate = reinterpret_cast<LPNMITEMACTIVATE>(pNMHDR);
POSITION pos = m_ctrlStudentList.GetFirstSelectedItemPosition();
if (pos != NULL)
{
int Item = m_ctrlStudentList.GetNextSelectedItem(pos);
CString Sid = m_ctrlStudentList.GetItemText(Item, 1);
CString Sname = m_ctrlStudentList.GetItemText(Item, 2);
MSid = Sid;
MSname = Sname;
else return;
}
*pResult = 0;
}
/*右键删除一个*/
void CStudentMngDlg::DelOneStudent()
{
UpdateData(TRUE);
if (MSid.IsEmpty()) return; // 图书ISBN号
if (MessageBox("确定要删除学生信息?", "提示",
MB_ICONQUESTION | MB_YESNO) != IDYES) // 确认提示
return;

try
{
CString strSQL;
_RecordsetPtr pRecordset("ADODB.Recordset");
strSQL.Format("SELECT * FROM student WHERE sid='%s'", MSid);
pRecordset->Open((_bstr_t)strSQL, _variant_t((IDispatch*)m_pConnection, true),
adOpenDynamic, adLockOptimistic, adCmdText); // 打开记录集

if (!pRecordset->BOF) pRecordset->MoveFirst();
if (pRecordset->_EOF) return; // 不存在该记录
pRecordset->Delete(adAffectCurrent); // 删除记录
pRecordset->Update(); // 更新数据库信息
pRecordset->Close(); // 关闭记录集
AfxMessageBox("删除学生信息成功。");
OnBnClickedQueryStudent();
}
catch (_com_error &e)
{
AfxMessageBox(e.Description());
return;
}
}
/*删除checkbox选中项,即就是批量删除*/
void CStudentMngDlg::DelMoreStudent()
{
if (MessageBox("确定要批量删除学生信息?", "提示",
MB_ICONQUESTION | MB_YESNO) != IDYES) // 确认提示
return;
m_ctrlStudentList.SetExtendedStyle(LVS_EX_CHECKBOXES);
CString DMSid[500];
for (int i = 0; i < 500; i++)
{
DMSid[i] = "0";
}
CString Sid;
CString str;
for (int i = 0; i<m_ctrlStudentList.GetItemCount(); i++)
{
if (m_ctrlStudentList.GetItemState(i, LVIS_SELECTED) == LVIS_SELECTED || m_ctrlStudentList.GetCheck(i))
{
Sid = m_ctrlStudentList.GetItemText(i, 1);
DMSid[i]=Sid;
}
}
for (int i = 0; i < m_ctrlStudentList.GetItemCount(); i++)
{
if (DMSid[i]!="0")
{
try
{
CString strSQL;
_RecordsetPtr pRecordset("ADODB.Recordset");
strSQL.Format("SELECT * FROM student WHERE sid='%s'", DMSid[i]);
pRecordset->Open((_bstr_t)strSQL, _variant_t((IDispatch*)m_pConnection, true),
adOpenDynamic, adLockOptimistic, adCmdText); // 打开记录集

if (!pRecordset->BOF) pRecordset->MoveFirst();
if (pRecordset->_EOF) return; // 不存在该记录
pRecordset->Delete(adAffectCurrent); // 删除记录
pRecordset->Update(); // 更新数据库信息
pRecordset->Close(); // 关闭记录集

}
catch (_com_error &e)
{
AfxMessageBox(e.Description());
return;
}
}
}
AfxMessageBox("批量删除学生信息成功。");
OnBnClickedQueryStudent();

}

ps:我这里是用CString DMSid[500]的字符串数组存取checkbox选中行的指定列信息,其实也可以用CStringArray类。