Search code examples
c++listviewwinapi

Example for LVM_SUBITEMHITTEST


Could some kind person show me an example of how to use LVM_SUBITEMHITTEST?

I've tried the following code, but both the return value of SendMessage() and the relevant members of myinfo are always -1, no matter where in my ListView I click.

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);
}

I based my code off of the following MSDN articles:

NM_RCLICK (list view) notification code

LVM_SUBITEMHITTEST message

But they don't list any examples, so I'm worried I'm doing something stupid :(


Solution

  • Update: I was doing something stupid. The LVM_SUBITEMHITTEST function wants client coordinates, not screen coordinates. Here's a working example:

    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);
    }