我想在用户右键单击 CListCtrl 中的项目时显示上下文菜单。我的代码如下:
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)
}
}
}
当我实际运行代码时,无论我点击哪里;在一个项目上,在 CListCtrl 内的空白空间中,在对话框的其他任何地方(通过删除第一个 if 语句);hitTestInfo.flags
设置为 48,如果我没看错的话,它的意思是“在整个 CListCtrl 的下方和右侧”。当我第一次检查它是否在 CListCtrl 中时,这真的没有意义。
那么我在某处有不正确的假设吗?我的代码不正确吗?我错过了什么吗?
作为一个可能相关的,或者可能不相关的,BONUS QUESTION,两者LVHT_ONITEMSTATEICON
和LVHT_ABOVE
都是#define
0x08 - 为什么会这样?这可能是我误解的关键。