2
下面的代碼創建一個窗口,實現一個CListViewCtrl並嘗試使用3個選項創建一個上下文菜單:new,編輯&刪除。WinApi,ATL創建上下文菜單功能處理程序
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;
}
};
的問題是,上面的代碼不進入OnNewTask當我點擊我的OnContextMenu函數來創建新的菜單項。我做錯了什麼?
Yeeey!它的工作原理非常感謝。 –