做VC ListControl的时候发现一个问题,右击菜单的时候,在没有列表数据的空白处也会出现菜单,这可够纠结的,于是用了点方法解决了,希望分享同样不爽的朋友们。代码如下:
void CXXXDlg::OnRclickList1(NMHDR* pNMHDR, LRESULT* pResult) { // TODO: Add your control notification handler code here CRect rl,r; CMenu menu, *pSubMenu; CPoint pos; GetCursorPos(&pos); GetDlgItem(IDC_LIST1)->GetWindowRect(&rl); m_ListFile.GetItemRect(m_ListFile.GetNextItem (-1, LVNI_SELECTED), &r, LVIR_LABEL); r.top = rl.top+r.top; r.bottom = rl.top+r.bottom; r.left = rl.left+r.left; r.right = rl.right+r.right; if (!menu.LoadMenu(IDR_MENU1)) { return; } if (!(pSubMenu=menu.GetSubMenu(0))) { return; } if (!r.PtInRect(pos)) { return; } SetForegroundWindow(); pSubMenu-> TrackPopupMenu( TPM_LEFTALIGN | TPM_RIGHTBUTTON, pos.x, pos.y, this); menu.DestroyMenu(); *pResult = 0; }