下面的代码创建了一个窗口,实现了一个 CListViewCtrl 并尝试创建一个具有 3 个选项的上下文菜单:新建、编辑和删除。
class CGuiView : public CWindowImpl<CGuiView, CListViewCtrl>
{
HMENU hPopupMenu;
MENUINFO m_ContextMenuInfo;
public:
DECLARE_WND_CLASS(NULL)
BOOL PreTranslateMessage(MSG* pMsg)
{
pMsg;
return FALSE;
}
BEGIN_MSG_MAP(CGuiView)
MESSAGE_HANDLER(WM_PAINT, OnPaint)
// MESSAGE_HANDLER(WM_LBUTTONUP,)
MSG_WM_CONTEXTMENU(OnContextMenu)
COMMAND_ID_HANDLER(ID_CTXMENU_NEW, OnNewTask)
END_MSG_MAP()
LRESULT OnPaint(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
{
CPaintDC dc(m_hWnd);
//TODO: Add your drawing code here
return 0;
}
void OnContextMenu ( HWND hwndCtrl, CPoint ptClick )
{
hPopupMenu = CreatePopupMenu();
InsertMenu(hPopupMenu, 0, MF_BYCOMMAND | MF_STRING, ID_CTXMENU_DELETE, TEXT("Delete"));
InsertMenu(hPopupMenu, ID_CTXMENU_DELETE, MF_BYCOMMAND | MF_STRING, ID_CTXMENU_EDIT, TEXT("Edit"));
InsertMenu(hPopupMenu, ID_CTXMENU_EDIT, MF_BYCOMMAND | MF_STRING | MF_ENABLED, ID_CTXMENU_NEW , TEXT("New"));
TrackPopupMenu(hPopupMenu, TPM_TOPALIGN | TPM_LEFTALIGN, ptClick.x, ptClick.y, 0,GetParent(), NULL);
}
LRESULT OnNewTask(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
CAboutDlg dlg;
dlg.DoModal();
return 0;
}
};
问题是当我点击我在 OnContextMenu 函数中创建的 New 菜单项时,上面的代码没有进入 OnNewTask。我做错了什么?