This documentation is archived and is not being maintained.


Retrieves the bounding rectangle for all or part of an item in the current view.

BOOL GetItemRect(
   int nItem,
   LPRECT lpRect,
   UINT nCode 
) const;


The index of the item whose position is to be retrieved.
Address of a RECT structure that receives the bounding rectangle.
Portion of the list view item for which to retrieve the bounding rectangle. It can be one of these values:
  • LVIR_BOUNDS   Returns the bounding rectangle of the entire item, including the icon and label.
  • LVIR_ICON   Returns the bounding rectangle of the icon or small icon.
  • LVIR_LABEL   Returns the bounding rectangle of the item text.

Return Value

Nonzero if successful; otherwise zero.


// OnClick in the handler for the NM_CLICK notification
void CMyListCtrl::OnClick(NMHDR* pNMHDR, LRESULT* pResult)
   // Get the current mouse location and convert it to client
   // coordinates.
   DWORD pos = GetMessagePos();
   CPoint pt(LOWORD(pos), HIWORD(pos));

   // Get indexes of the first and last visible items in 
   // the 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);

       // Get the next item in listview control.

   *pResult = 0;

See Also

CListCtrl Overview | Class Members | Hierarchy Chart | CListCtrl::GetItemPosition | CListCtrl::SetItemPosition | CListCtrl::GetOrigin