To get notified of the event that an item in a list control has been selected, add a handler for the LVN_ITEMCHANGED notification message. The following is a sample handler for a reflected LVN_ITEMCHANGED notification message.
void CListControl::OnItemChanged(NMHDR* pNMHDR, LRESULT* pResult) {
NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;
*pResult = 0;
// Has the item's state changed?
if ((pNMListView->uChanged & LVIF_STATE) != LVIF_STATE)
return; // state has not changed
// Has the selected state changed?
if (!(pNMListView->uNewState & LVIS_SELECTED) || (pNMListView->uOldState & LVIS_SELECTED))
return; // selected state has not changed
// Do something with the (newly selected) item
}
This could also be used to get notified that an item has gotten the focus; just use LVIS_FOCUSED for the item's state value instead of LVIS_SELECTED.
See my Visual C++ Programmer Stuff page for more C++ stuff.