tags:

views:

70

answers:

1

I am wanting to display a context menu when a user right-clicks on an item within a CListCtrl. My code is as follows:

void DatastoreDialog::OnContextMenu(CWnd *pWnd, CPoint pos)
{
    // Find the rectangle around the list control
    CRect rectMainArea;
    m_itemList.GetWindowRect(&rectMainArea);
    // Find out if the user right-clicked the list control
    if( rectMainArea.PtInRect(pos) )
    {
        LVHITTESTINFO hitTestInfo;
        hitTestInfo.pt = pos;
        hitTestInfo.flags = LVHT_ONITEM;
        m_itemList.HitTest(&hitTestInfo);
        if (hitTestInfo.flags & LVHT_NOWHERE)
        {
             // No item was clicked
        }
        else 
        {
            MyContextHandler(hitTestInfo)
        }
    }
}

When I actually run the code, regardless of where I click; on an item, in empty space within the CListCtrl, anywhere else on the dialog (by removing the first if statement); hitTestInfo.flags is set to 48, which, if I'm reading this correctly, means "Below, and to the right of the whole CListCtrl". Which doesn't really make sense when I'm first checking if it's within the CListCtrl.

So do I have an incorrect assumption somewhere? Is my code incorrect? Am I missing something?

As a possibly-related, or maybe not, BONUS QUESTION, both LVHT_ONITEMSTATEICON and LVHT_ABOVE are #defined as 0x08 - why is this? This may be key to my misunderstanding.

+2  A: 

I think HitTest() needs a position in client coordinates. It's been a while since I last did this, but it doesn't make sense to me to pass screen coordinates into a client window hit testing routine. Add m_itemList.ScreenToClient(&pos); before hitTestInfo.pt = pos; and see if that helps.

Furthermore, note that OnContextMenu() may not be the call you're looking for. It is called in response to (by default) shift-f10 as well. The documentation of WM_CONTEXTMENU is (on reading it diagonally, I don't remember how it works from when I last did this) not very clear on what the content of 'pos' will be in that case; you may need to do an explicit GetCursorPos() to handle that case. Or just show your context in WM_RBUTTONDOWN.

Roel
Thanks so much! I'll also look into the WM_RBUTTONDOWN thing - you're right, it is causing the context menu to appear incorrectly.
Smashery
If only I could vote you up twice :-)
Smashery