本文主要是介绍CListCtrl中高亮插入行,条目上移下移,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1. 样式修改
2. 关键代码
BOOL CMFCApplication3Dlg::OnInitDialog()
{CDialogEx::OnInitDialog();// // 代码省略......//// 设置此对话框的图标。 当应用程序主窗口不是对话框时,框架将自动// 执行此操作SetIcon(m_hIcon, TRUE); // 设置大图标SetIcon(m_hIcon, FALSE); // 设置小图标// TODO: 在此添加额外的初始化代码m_btnMoveDown.EnableWindow(FALSE);m_btnMoveUp.EnableWindow(FALSE);m_MyListCtrl.InsertColumn(0, _T("测试"), 0, 100 );return TRUE; // 除非将焦点设置到控件,否则返回 TRUE
}void CMFCApplication3Dlg::OnBnClickedAddSomeItems()
{// TODO: 在此添加控件通知处理程序代码static std::default_random_engine dre(unsigned int(time(0)));std::uniform_int_distribution<int> uid(100, 999);CString strValue;strValue.Format(_T("测试文本--%d"), uid(dre));int nSelMark = m_MyListCtrl.GetSelectionMark();int nInsertIndex = m_MyListCtrl.GetSelectionMark();if (nInsertIndex < 0){nInsertIndex = m_MyListCtrl.GetItemCount() - 1;if (nInsertIndex < 0) nInsertIndex = 0;}nInsertIndex += 1;int nNewIndex = m_MyListCtrl.InsertItem(nInsertIndex, strValue);//设置当前行高亮 m_MyListCtrl.SetSelectionMark(nNewIndex);m_MyListCtrl.SetItemState(nNewIndex,LVIS_FOCUSED | LVIS_SELECTED, LVIS_FOCUSED | LVIS_SELECTED);m_MyListCtrl.SetFocus();}//down
void CMFCApplication3Dlg::OnBnClickedMoveDown()
{int nSelIndex = m_MyListCtrl.GetSelectionMark();if (nSelIndex < 0 || nSelIndex >= m_MyListCtrl.GetItemCount() - 1){return;}//将文本值与扩展数据交换int nNextIndex = nSelIndex + 1;CString strNextText = m_MyListCtrl.GetItemText(nNextIndex, 0);m_MyListCtrl.SetItemText(nNextIndex, 0, m_MyListCtrl.GetItemText(nSelIndex, 0));m_MyListCtrl.SetItemText(nSelIndex, 0, strNextText);DWORD_PTR dwNextItemData = m_MyListCtrl.GetItemData(nNextIndex);m_MyListCtrl.SetItemData(nNextIndex, m_MyListCtrl.GetItemData(nSelIndex));m_MyListCtrl.SetItemData(nSelIndex, dwNextItemData);//设置行为高亮m_MyListCtrl.SetSelectionMark(nNextIndex);m_MyListCtrl.SetItemState(nNextIndex, LVIS_FOCUSED | LVIS_SELECTED, LVIS_FOCUSED | LVIS_SELECTED);m_MyListCtrl.SetFocus();}//up
void CMFCApplication3Dlg::OnBnClickedMoveUp()
{// TODO: 在此添加控件通知处理程序代码int nSelIndex = m_MyListCtrl.GetSelectionMark();if (nSelIndex <= 0){return;}//将文本值与扩展数据交换int nPrevIndex = nSelIndex - 1;CString strPrevText = m_MyListCtrl.GetItemText(nPrevIndex, 0);m_MyListCtrl.SetItemText(nPrevIndex, 0, m_MyListCtrl.GetItemText(nSelIndex, 0));m_MyListCtrl.SetItemText(nSelIndex, 0, strPrevText);DWORD_PTR dwPrevItemData = m_MyListCtrl.GetItemData(nPrevIndex);m_MyListCtrl.SetItemData(nPrevIndex, m_MyListCtrl.GetItemData(nSelIndex));m_MyListCtrl.SetItemData(nSelIndex, dwPrevItemData);//设置当前行为高亮m_MyListCtrl.SetSelectionMark(nPrevIndex);m_MyListCtrl.SetItemState(nPrevIndex, LVIS_FOCUSED | LVIS_SELECTED, LVIS_FOCUSED | LVIS_SELECTED);m_MyListCtrl.SetFocus();
}void CMFCApplication3Dlg::OnItemchangedList1(NMHDR *pNMHDR, LRESULT *pResult)
{LPNMLISTVIEW pNMLV = reinterpret_cast<LPNMLISTVIEW>(pNMHDR);// TODO: 在此添加控件通知处理程序代码TRACE3("nItem:%d, nNewState:%u, nOldState:%u\n",pNMLV->iItem, pNMLV->uNewState, pNMLV->uOldState);if (pNMLV->uNewState & LVIS_SELECTED){if (pNMLV->iItem == 0){m_btnMoveDown.EnableWindow(TRUE);m_btnMoveUp.EnableWindow(FALSE);}else if (pNMLV->iItem == m_MyListCtrl.GetItemCount() - 1){m_btnMoveDown.EnableWindow(FALSE);m_btnMoveUp.EnableWindow(TRUE);}}*pResult = 0;
}
3. 运行截图
这篇关于CListCtrl中高亮插入行,条目上移下移的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!