2

下面的代码创建了一个窗口,实现了一个 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。我做错了什么?

4

1 回答 1

2

您将GetParent()作为参数提供给TrackPopupMenuAPI 函数。所以WM_COMMAND发送到列表视图父级,而不是列表视图本身。

将其发送到列表视图,或将命令从父级转发到列表视图。您还可以使用 Spy++ 工具检查被调试进程中的消息,以查看确切发送的内容和发送位置。

于 2012-10-01T13:17:53.633 回答