Article ID: 147842
Article Last Modified on 11/21/2006
// In the .h file of CMyListCtrl:
class CMyListCtrl : public CListCtrl
{
...
protected:
DECLARE_MESSAGE_MAP()
public:
afx_msg void OnClickOnNMClick(NMHDR* pNMHDR, LRESULT* pResult);
};
// In the .cpp file of CMyListCtrl:
BEGIN_MESSAGE_MAP(CMyListCtrl, CListCtrl)
...
ON_NOTIFY_REFLECT(NM_CLICK, OnClickOnNMClick)
END_MESSAGE_MAP()
void CMyListCtrl::OnClickOnNMClick(NMHDR* pNMHDR, LRESULT* pResult)
{
// TODO: Add your control notification handler code here
*pResult = 0;
}
void CMyListCtrl::OnClickOnNMClick(NMHDR* pNMHDR, LRESULT* pResult)
{
// Get the current mouse location and convert it to client
// coordinates.
DWORD pos = GetMessagePos();
CPoint pt(LOWORD(pos), HIWORD(pos));
ScreenToClient(&pt);
// Get indexes of the first and last visible items in listview
// control.
int index = GetTopIndex();
int last_visible_index = index + GetCountPerPage();
if (last_visible_index > GetItemCount())
last_visible_index = GetItemCount();
// Loop until number visible items has been reached.
while (index <= last_visible_index)
{
// Get the bounding rectangle of an item. If the mouse
// location is within the bounding rectangle of the item,
// you know you have found the item that was being clicked.
CRect r;
GetItemRect(index, &r, LVIR_BOUNDS);
if (r.PtInRect(pt))
{
UINT flag = LVIS_SELECTED | LVIS_FOCUSED;
SetItemState(index, flag, flag);
break;
}
// Get the next item in listview control.
index++;
}
*pResult = 0;
}
131788 OdListVw.exe highlights entire row in a ListView control
Additional query words: CListView
Keywords: kbcode kbhowto kblistview kbprogramming kbuidesign KB147842