LPNMITEMACTIVATE temp = (LPNMITEMACTIVATE) pNMHDR;
//get the row number
int nItem = temp->iItem;
//get the column number
int nSubItem = temp->iSubItem;
Has been extended to accommodate subitem hit-testing. The LVHITTESTINFO structure contains information about a hit test. It is used in association with the LVM_HITTEST and LVM_SUBITEMHITTEST messages and their related macros. This structure supersedes the LV_HITTESTINFO structure.
typedef struct _LVHITTESTINFO {
POINT pt;
UINT flags;
int iItem;
int iSubItem;
} LVHITTESTINFO, FAR *LPLVHITTESTINFO;
Members
pt
Position to hit test, in client coordinates.
flags
Variable that receives information about the results of a hit test. This member can be one or more of the following values: LVHT_ABOVE The position is above the control's client area.
LVHT_BELOW The position is below the control's client area.
LVHT_NOWHERE The position is inside the list view control's client window, but it is not over a list item.
LVHT_ONITEMICON The position is over a list view item's icon.
LVHT_ONITEMLABEL The position is over a list view item's text.
LVHT_ONITEMSTATEICON The position is over the state image of a list view item.
LVHT_TOLEFT The position is to the left of the list view control's client area.
LVHT_TORIGHT The position is to the right of the list view control's client area.
You can use LVHT_ABOVE, LVHT_BELOW, LVHT_TOLEFT, and LVHT_TORIGHT to determine whether to scroll the contents of a list view control. Two of these values may be combined. For example, if the position is above and to the left of the client area, you could use both LVHT_ABOVE and LVHT_TOLEFT.
You can test for LVHT_ONITEM to determine whether a specified position is over a list view item. This value is a bitwise-OR operation on LVHT_ONITEMICON, LVHT_ONITEMLABEL, and LVHT_ONITEMSTATEICON.
iItem
Receives the index of the matching item. Or if hit-testing a subitem, this value represents the subitem's parent item.
iSubItem
Version 4.70. Receives the index of the matching subitem. When hit-testing an item, this member will be zero.
//使用GetCurSel()可以得到是那一行,
//用下面的函数可以得到点击的列
int
gxListCtrl::HitTestEx (CPoint& Point, int* pColumn)//返回所点击的列数
{
int ColumnNum = 0;
int Row = HitTest (Point, NULL);
if (pColumn)
*pColumn = 0;
// 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))
{
if (pColumn)
*pColumn = ColumnNum;
return Row;
}
Rect.left += ColWidth;
}
}
}
return -1;
}