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( rectMain开发者_如何学CArea.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.
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.
I had similar problem with HitTest
for list control.
It has obscure effect of returning item 0 and LVHT_ONITEM
flag
even if click occurs on the header. One would expect -1 for item
index and LVHT_NOWHERE
for a flag.
I resolved this by using HitTest
of header control. Here is how:
UINT uFlags = 0;
CHeaderCtrl* pHdr = m_list.GetHeaderCtrl();
if (!pHdr) return; // sanity
HDHITTESTINFO hitTestInfo = {0};
hitTestInfo.pt = ptClient;
int iItem = pHdr->HitTest(&hitTestInfo);
if ((iItem != -1) && ((HHT_ONHEADER | HHT_ONDIVIDER) & hitTestInfo.flags)) {
// this is header control menu
CWnd::OnContextMenu(pWnd, point);
}
else if (HHT_BELOW & hitTestInfo.flags) {
CXTMenu Menu;
// this is list view control menu
if (Menu.LoadMenu(IDR_LIST_CONTEXT)) {
...
}
}
精彩评论