给你一个函数做参考,其中Point为鼠标位置,nSubItem返回列,函数返回行
int CXXXX::HitTestEx(CPoint &Point, int &nSubItem)
{
nSubItem = 0;
int ColumnNum = 0;
int Row = HitTest (Point, NULL);
// Make sure that the ListView is in LVS_REPORT
if ((GetWindowLong (m_hWnd, GWL_STYLE) & LVS_TYPEMASK) != LVS_REPORT)
return Row;
// Get the top and bottom row visible
Row = GetTopIndex();
int Bottom = Row + GetCountPerPage();
if (Bottom > GetItemCount())
Bottom = GetItemCount();
// Get the number of columns
CHeaderCtrl* pHeader = (CHeaderCtrl*) GetDlgItem(0);
int nColumnCount = pHeader->GetItemCount();
// Loop through the visible rows
for(; Row <= Bottom; Row++)
{
// Get bounding rect of item and check whether point falls in it.
CRect Rect;
GetItemRect (Row, &Rect, LVIR_BOUNDS);
if (Rect.PtInRect (Point))
{
// Now find the column
for (ColumnNum = 0; ColumnNum < nColumnCount; ColumnNum++)
{
int ColWidth = GetColumnWidth (ColumnNum);
if (Point.x >= Rect.left && Point.x <= (Rect.left + ColWidth))
{
nSubItem = ColumnNum;
return Row;
}
Rect.left += ColWidth;
}
}
}