我正在寻找一种方法来确定右键单击列表ctrl的标题以显示上下文菜单。我有一个代码示例,但它不起作用。
void ExDialog::OnContextMenu(CWnd* pWnd, CPoint point)
{
CListCtrl* pLC = (CListCtrl*) GetDlgItem(IDC_EXAMPLE);
CHeaderCtrl* pHC = pLC->GetHeaderCtrl();
if (pWnd->GetSafeHwnd()==pHC->GetSafeHwnd())
{
CMenu menu;
VERIFY(menu.LoadMenu(IDR_HEADERMENU));
CMenu* pPopup = menu.GetSubMenu(0);
ASSERT(pPopup != NULL);
CWnd* pWndPopupOwner = pHC;
pPopup->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON, point.x, point.y, pWndPopupOwner);
}
}
答案 0 :(得分:1)
你得到listview控件作为参数,然后你需要自己找到标题:命中测试和/或比较坐标:
CListCtrl* pLC = (CListCtrl*) GetDlgItem(IDC_LIST1);
CHeaderCtrl* pHC = pLC->GetHeaderCtrl();
if (pWnd->GetSafeHwnd() == pLC->GetSafeHwnd()) // <<--- Not pHC!
{
UINT nFlags = 0;
pLC->HitTest(point, &nFlags);
if(nFlags & LVHT_NOWHERE) // <<--- Header hits "nowhere"
{
CRect Position;
pHC->GetWindowRect(Position);
if(Position.PtInRect(point)) // <<--- point check
{
CMenu menu;
VERIFY(menu.LoadMenu(IDR_HEADERMENU));
CMenu* pPopup = menu.GetSubMenu(0);
ASSERT(pPopup != NULL);
CWnd* pWndPopupOwner = pHC;
pPopup->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON, point.x, point.y, pWndPopupOwner);
}
}
}
答案 1 :(得分:0)
对我来说很好看。您是否忘记将ON_WM_CONTEXTMENU()放入ExDialog()的消息映射中?
如果这没有帮助,当您单步执行此功能时,您认为发生了什么?