The code below creates a window, implements a CListViewCtrl and tries to make a context menu with 3 options: new, edit & delete.
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;
}
};
The problem is that the code above does not enter OnNewTask when I click on the New menu item that I create in the OnContextMenu function. What I am doing wrong?
You are providing
GetParent()as an argument to theTrackPopupMenuAPI function. SoWM_COMMANDis sent to the list view parent, not list view itself.Have it sent to the list view, or forward commands from parent to list view. You can also use Spy++ tool to check the messages in the debugged process to see what exactly is sent and where.