2012-10-01 62 views
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函數來創建新的菜單項。我做錯了什麼?

回答

2

您正在提供GetParent()作爲TrackPopupMenu API函數的參數。所以WM_COMMAND被髮送到列表視圖父,而不是列表視圖本身。

將它發送到列表視圖,還是將父命令轉發到列表視圖。您還可以使用Spy ++工具來檢查調試過程中的消息,以查看究竟發送了什麼以及在哪裏。

+0

Yeeey!它的工作原理非常感謝。 –