MFC list control CListCtrl usage demonstration
- 2020-10-31 21:54:24
- OfStack
CListCtrl gets the selected 1 row
POSITION ps = m_list.GetFirstSelectedItemPosition();
int nSel = m_list.GetNextSelectedItem(ps);// The index of the selected row ( based 0 )
// or
if(m_list.GetSelectedCount() <1)
return;
int nSel = m_list.GetSelectionMark();
CListCtrl controls remain highlighted when they lose focus
1. Add variable m_nSelItem to header file
int m_nSelItem;
2. Add NM_KILLFOCUS (lose focus) and NM_SETFOCUS (get focus) messages to the ListCtrl control
Add the code manually as follows:
Add a declaration to the header file
afx_msg void OnNMKillfocusList(NMHDR *pNMHDR, LRESULT *pResult);
afx_msg void OnNMSetfocusList(NMHDR *pNMHDR, LRESULT *pResult);
Add the mapping to the source file
[
ON_NOTIFY(NM_KILLFOCUS, IDC_LISTCTRL, OnNMKillfocusList)
ON_NOTIFY(NM_SETFOCUS, IDC_LISTCTRL, OnNMSetfocusList)
Add a function to the source file
void CPageListView::OnNMKillfocusList(NMHDR *pNMHDR, LRESULT *pResult)
{
// TODO: Add control notification handler code here
m_nSelItem = m_ListCtrl.GetSelectionMark();
m_ListCtrl.SetItemState(m_nSelItem, LVIS_DROPHILITED, LVIS_DROPHILITED);
*pResult = 0;
}
void CPageListView::OnNMSetfocusList(NMHDR *pNMHDR, LRESULT *pResult)
{
// TODO: Add control notification handler code here
m_ListCtrl.SetItemState(m_nSelItem, FALSE, LVIF_STATE);
*pResult = 0;
}
Note: Attribute 1 of Always Show Selection must be set to FALSE