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 #define
d as 0x08 - why is this? This may be key to my misunderstanding.