重载PreTranslateMessage函数用于相应右键,并弹出预定义好的Menu
我的界面上有CTreeCtrl和CListCtrl,在CListCtrl和CTreeCtrl上点击右键第一次弹出菜单,第二次菜单消失。
出问题的代码如下
if(WM_RBUTTONDOWN==pMsg->message)
{
DWORD dwPos = GetMessagePos();
CPoint point(LOWORD(dwPos), HIWORD(dwPos));
CMenu menu;
VERIFY(menu.LoadMenu(IDR_RCLKMENU));
CMenu *popup=menu.GetSubMenu(0);
ASSERT(popup!=NULL);
popup->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON, point.x, point.y, this);
}
return CDialogEx::PreTranslateMessage(pMsg);
修正后的代码如下:
if(WM_RBUTTONDOWN==pMsg->message)
{
DWORD dwPos = GetMessagePos();
CPoint point(LOWORD(dwPos), HIWORD(dwPos));
CMenu menu;
VERIFY(menu.LoadMenu(IDR_RCLKMENU));
CMenu *popup=menu.GetSubMenu(0);
ASSERT(popup!=NULL);
popup->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON, point.x, point.y, this);
return true; // Big Qu
}
return CDialogEx::PreTranslateMessage(pMsg);
暂时没有完全分析清楚,先贴到这里,希望高人讲解机制。