分类: C/C++
2008-08-01 16:59:17
void CLCDemoDlg::OnClickList(NMHDR* pNMHDR, LRESULT* pResult) { POSITION pos; pos = m_listCtrl.GetFirstSelectedItemPosition(); // 得到项目索引 IndexInFieldList = m_listCtrl.GetNextSelectedItem(pos); *pResult = 0; }在向上移动操作按钮的WM_CLICK消息响应函数中写入下面代码。
void CLCDemoDlg::OnButtonUp() { m_listCtrl.SetFocus(); if (IndexInFieldList == -1) return; // 判断所选项是否位于行首 if (IndexInFieldList == 0) { AfxMessageBox("已经位于第一行!"); return; } // 提取所选列表项各列类容 CString tempField1, tempField2, tempField3; tempField1 = m_listCtrl.GetItemText(IndexInFieldList, 0); tempField2 = m_listCtrl.GetItemText(IndexInFieldList, 1); tempField3 = m_listCtrl.GetItemText(IndexInFieldList, 2); // 删除所选列表项 m_listCtrl.DeleteItem(IndexInFieldList); // 在IndexInFieldList-1位置处插入上面所删列表项的各列类容 int tempItem; tempItem = m_listCtrl.InsertItem(IndexInFieldList-1, _T(tempField1)); m_listCtrl.SetItemText(tempItem, 1, _T(tempField2)); m_listCtrl.SetItemText(tempItem, 2, _T(tempField3)); IndexInFieldList--; // 使得IndexInFieldList-1位置处项目高亮显示并获得焦点 UINT flag = LVIS_SELECTED|LVIS_FOCUSED; m_listCtrl.SetItemState(IndexInFieldList, flag, flag); }向下移动操作按钮的响应函数代码与上面的代码类似,请参考本文所附源代码,因为代码有相关中文注释,这里我就不再赘述了。