在对话框中右击鼠标,判断鼠标位置,如果在ListCtrl里,则弹出快捷菜单。
使用PtInRect()判断,程序如下。运行结果,它有时能弹出来,有时又弹不出来,为什么?void CPsgDlg::OnContextMenu(CWnd* pWnd, CPoint point)
{
CMenu menu;
CRect rc;
CWnd* wndList = GetDlgItem(IDC_LIST1);
wndList->GetClientRect(&rc); if(rc.PtInRect(point))
{
menu.LoadMenu(IDR_MENU1);
menu.GetSubMenu(0)->TrackPopupMenu(TPM_LEFTALIGN|TPM_RIGHTBUTTON,point.x,point.y,this);
}
}
使用PtInRect()判断,程序如下。运行结果,它有时能弹出来,有时又弹不出来,为什么?void CPsgDlg::OnContextMenu(CWnd* pWnd, CPoint point)
{
CMenu menu;
CRect rc;
CWnd* wndList = GetDlgItem(IDC_LIST1);
wndList->GetClientRect(&rc); if(rc.PtInRect(point))
{
menu.LoadMenu(IDR_MENU1);
menu.GetSubMenu(0)->TrackPopupMenu(TPM_LEFTALIGN|TPM_RIGHTBUTTON,point.x,point.y,this);
}
}
menu.LoadMenu(IDR_MENU1);
menu.GetSubMenu(0)->TrackPopupMenu(TPM_LEFTALIGN|TPM_RIGHTBUTTON,point.x,point.y,this);
CWnd* pWnd ;
pWnd = (CListCtrl* )GetDlgItem(IDC_LIST2 );
pWnd ->GetWindowRect( &rect );
//
//
if (!rect.PtInRect( point ) )
{
return ;
}
//
// CG: This block was added by the Pop-up Menu component
{
if (point.x == -1 && point.y == -1){
//keystroke invocation
point = rect.TopLeft();
point.Offset(5, 5);
} CMenu menu;
VERIFY(menu.LoadMenu( CG_IDR_POPUP_DIALOG_USER_TO_BED) );
CMenu* pPopup = menu.GetSubMenu(0);
ASSERT(pPopup != NULL);
CWnd* pWndPopupOwner = this; while (pWndPopupOwner->GetStyle() & WS_CHILD)
pWndPopupOwner = pWndPopupOwner->GetParent();
pPopup ->EnableMenuItem( ID_EDIT_POPU_CUT , MF_BYCOMMAND |
MF_DISABLED | MF_GRAYED ) ;
pPopup->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON, point.x, point.y,
pWndPopupOwner);
}
这段代码是我从自己做过的项目里摘下来的,是好用的。
menu.GetSubMenu(0)->TrackPopupMenu(TPM_LEFTALIGN|TPM_RIGHTBUTTON,point.x,point.y,this);
中的point.x,point.y,怎么获得呢?
怎么获得当前鼠标坐标?
GetCursorPos(&pt);