Freigeben über


CListCtrl::HitTest

Determines which list view item, if any, is at a specified position.

int HitTest(
   LVHITTESTINFO* pHitTestInfo 
) const;
int HitTest(
   CPoint pt,
   UINT* pFlags = NULL 
) const;

Parameters

  • pHitTestInfo
    Address of an LVHITTESTINFO structure that contains the position to hit test and that receives information about the results of the hit test.

  • pt
    Point to be tested.

  • pFlags
    Pointer to an integer that receives information about the results of the test. See the explanation of the flags member of the LVHITTESTINFO structure in the Windows SDK.

Return Value

The index of the item at the position specified by pHitTestInfo, if any, or -1 otherwise.

Remarks

You can use the LVHT_ABOVE, LVHT_BELOW, LVHT_TOLEFT, and LVHT_TORIGHT values of the structure's flag member to determine whether to scroll the contents of a list view control. Two of these flags can be combined, for example, if the position is above and to the left of the client area.

You can test for the LVHT_ONITEM value of the structure's flag member to determine whether a given position is over a list view item. This value is a bitwise-OR operation on the LVHT_ONITEMICON, LVHT_ONITEMLABEL, and LVHT_ONITEMSTATEICON values of the structure's flag member.

Example

void CListCtrlDlg::OnRClick(NMHDR* pNMHDR, LRESULT* pResult)
{
   LPNMITEMACTIVATE pia = (LPNMITEMACTIVATE)pNMHDR;
   CPoint point(pia->ptAction);

   // Select the item the user clicked on.
   UINT uFlags;
   int nItem = m_myListCtrl.HitTest(point, &uFlags);

   if (uFlags & LVHT_ONITEMLABEL)
   {
      m_myListCtrl.SetItem(nItem, 0, LVIF_STATE, NULL, 0, LVIS_SELECTED, 
         LVIS_SELECTED, 0);
   }

   *pResult = 0;
}

Requirements

Header: afxcmn.h

See Also

Concepts

CListCtrl Class

CListCtrl Members

Hierarchy Chart

CListCtrl::GetItemPosition