我只是想验证一下通过单击鼠标右键来弹出已经用ClassWized做好了的菜单IDR_MENU1(资源号),我是直接在对话框类加入的OnContextMenu(CWnd* pWnd, CPoint point)这个函数。但是运行后,单击鼠标右键的时候,会出现错误:
错误出现在WINCORE.CPP里的红色部分:
BOOL CMenu::TrackPopupMenu(UINT nFlags, int x, int y,
CWnd* pWnd, LPCRECT lpRect)
{
ASSERT(m_hMenu != NULL);
_AFX_THREAD_STATE* pThreadState = AfxGetThreadState();
HWND hWndOld = pThreadState->m_hTrackingWindow;
HMENU hMenuOld = pThreadState->m_hTrackingMenu;
pThreadState->m_hTrackingWindow = pWnd->GetSafeHwnd();
pThreadState->m_hTrackingMenu = m_hMenu;
BOOL bOK = ::TrackPopupMenu(m_hMenu, nFlags, x, y, 0,
pThreadState->m_hTrackingWindow, lpRect);
pThreadState->m_hTrackingWindow = hWndOld;
pThreadState->m_hTrackingMenu = hMenuOld;
return bOK;
}
我写的函数如下:
void CMediaDlg::OnContextMenu(CWnd* pWnd, CPoint point)
{
// TODO: Add your message handler code here
CMenu m_popMenu;
m_popMenu.LoadMenu(IDR_MENU1);
CMenu* pSubMenu=m_popMenu.GetSubMenu(0)
pSubMenu->TrackPopupMenu(TPM_LEFTALIGN|TPM_LEFTBUTTON,point.x,point.y,this);
}
我自己在网上找了好久也没找到答案
我也是才学VC,有哪位好心人能帮我指点一下吗?
------最佳解决方案--------------------------------------------------------
m_hMenu为null。
你load的是m_popMenu。
------其他解决方案--------------------------------------------------------
CMenu m_popMenu;
ASSERT(m_popMenu.LoadMenu(IDR_MENU1));
CMenu* pSubMenu=m_popMenu.GetSubMenu(0);
ASSERT(pSubMenu!=NULL);
pSubMenu->TrackPopupMenu(TPM_LEFTALIGN
------其他解决方案--------------------------------------------------------
TPM_LEFTBUTTON,point.x,point.y,this);