title: Right Click on ListView WinAPI C++
body: <p>I have a window with a listview which I would like to right click an entry in the listview and have certain options displayed in a context menu. I cant find any examples for C++. The similar question is <a href="https://stackoverflow.com/questions/14001398/how-to-create-a-context-menu-on-listbox-right-click">here</a> but this is for listbox and i cant make it work in my program</p>
  
  <p>So far I could not find any examples for c++ online. I could make it work for WM_LBUTTONDOWN on my main window but not on listview. Anyway, this is my code which i cant make it work:</p>
  
  <pre><code>case WM_CONTEXTMENU:
          if ((HWND)wParam == hWndListView) {
              POINT cursor;
              GetCursorPos(&amp;cursor);
              TrackPopupMenu((HMENU)GetSubMenu(LoadMenu(hInst, MAKEINTRESOURCE(IDR_CONTEXT)), 0), TPM_LEFTALIGN | TPM_RIGHTBUTTON, cursor.x, cursor.y, 0, hWnd, NULL);
          }
          break;
  </code></pre>
  
  <p>Any advise? Thanks</p>
  
  <p>Answer:</p>
  
  <pre><code>case WM_NOTIFY:
      // When right button clicked on mouse
      if ((((LPNMHDR)lParam)-&gt;hwndFrom) == hWndListView)
      {
          switch (((LPNMHDR)lParam)-&gt;code)
          {
          case NM_RCLICK:
          {
              POINT cursor; // Getting the cursor position
              GetCursorPos(&amp;cursor);
              // Creating the po-up menu list
              TrackPopupMenu((HMENU)GetSubMenu(LoadMenu(hInst, MAKEINTRESOURCE(IDR_CONTEXT)), 0), TPM_LEFTALIGN | TPM_RIGHTBUTTON, cursor.x, cursor.y, 0, hWnd, NULL);
          }
              break;
          }
              break;
      }
      break;
  </code></pre>
  
  <p>This works under WndProc</p>