LVM_SUBITEMHITTEST的示例

时间:2016-05-16 03:46:23

标签: c++ listview winapi

有些人可以告诉我一个如何使用LVM_SUBITEMHITTEST的例子吗?

我已经尝试了以下代码,但SendMessage()的返回值和myinfo的相关成员总是-1,无论我在ListView中的哪个位置单击。

case NM_RCLICK:
{
    NMITEMACTIVATE itemClicked = *(NMITEMACTIVATE*)lParam;
    LVHITTESTINFO myinfo;
    memset(&myinfo, 0, sizeof(myinfo));
    POINT cursorPos;
    GetCursorPos(&cursorPos);
    myinfo.pt = cursorPos;
    // I find that itemNumber as well as myinfo->iItem and myinfo->iSubItem
    // are always set to -1 by the following line
    int itemNumber = SendMessage(myListviewHwnd, LVM_SUBITEMHITTEST, 0,(LPARAM)&myinfo);
}

我的代码基于以下MSDN文章:

NM_RCLICK (list view) notification code

LVM_SUBITEMHITTEST message

但他们没有列出任何例子,所以我担心我做了些蠢事:(

1 个答案:

答案 0 :(得分:1)

更新:我做了一些愚蠢的事。 LVM_SUBITEMHITTEST函数需要客户端坐标,而不是屏幕坐标。这是一个有效的例子:

case NM_RCLICK:
{
    NMITEMACTIVATE itemClicked = *(NMITEMACTIVATE*)lParam;
    LVHITTESTINFO myinfo;
    memset(&myinfo, 0, sizeof(myinfo));
    POINT cursorPos;
    GetCursorPos(&cursorPos);
    ScreenToClient(myListviewHwnd, &cursorPos);
    myinfo.pt = cursorPos;
    int itemNumber = SendMessage(myListviewHwnd, LVM_SUBITEMHITTEST, 0, (LPARAM)&myinfo);
}