title: Detect clicking inside listview and show context menu
body: <p>I have a listview created as a resource and loaded on a dialog window.  I want to detect and show a context menu only when items within the listview have been clicked.</p>
  
  <pre><code> MESSAGE_HANDLER(WM_CONTEXTMENU,OnContextMenu)
  
          LRESULT OnContextMenu(UINT /*uMsg*/, WPARAM wParam, LPARAM lParam, BOOL&amp; bHandled)
              {
                  int iSelected = -1;
      int iFocusGroup = -1;
      iSelected = SendMessage((HWND)wParam, LVM_GETNEXTITEM, -1,LVNI_SELECTED);
      iFocusGroup = ListView_GetFocusedGroup((HWND)wParam);
      if( iSelected != -1 &amp;&amp; iFocusGroup == -1) {
                      hPopupMenu = CreatePopupMenu();
                      Insert
  
  Menu(hPopupMenu,  0,     MF_BYCOMMAND | MF_STRING | MF_ENABLED, ID_SHREDTASK_CTXMENU_DELETE, TEXT("Delete"));
                  TrackPopupMenu(hPopupMenu, TPM_TOPALIGN | TPM_LEFTALIGN, GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam), 0, m_hWnd, NULL); 
              }
              return 0;
          }
  </code></pre>
  
  <p>OK, I've edited this and it works the way it is presented here but the question still stands and can someone explain to me what's the thing with focus group here and why if I send the LVM_GETNEXTITEM message while in the dialog it returns != -1 ? isn't it solely for Listviews ?</p>
  
  <p>EDIT :</p>
  
  <p>Here is another alternative that I've worked out based on your responses:</p>
  
  <pre><code>LRESULT OnNotifyRClick(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL&amp; bHandled)
      {
            switch (uMsg)
          {
              case WM_NOTIFY:
                  switch (((LPNMHDR)lParam)-&gt;code)
                  {
                  case NM_RCLICK:
                      if (((LPNMHDR)lParam)-&gt;idFrom == IDC_LISTTASKFILES)
                      {                       
                           int iSelected = -1;
                           iSelected = SendMessage(GetDlgItem(IDC_LISTTASKFILES), LVM_GETNEXTITEM, -1,LVNI_SELECTED);
  
                          if( iSelected != -1 ) {
                              hPopupMenu = CreatePopupMenu();
                              InsertMenu(hPopupMenu,  0,     MF_BYCOMMAND | MF_STRING | MF_ENABLED, ID_SHREDTASK_CTXMENU_DELETE, TEXT("Delete"));
                              TrackPopupMenu(hPopupMenu, TPM_TOPALIGN | TPM_LEFTALIGN, ((CPoint)GetMessagePos()).x, ((CPoint)GetMessagePos()).y, 0, m_hWnd, NULL); 
                          }
                           bHandled = true;
  
                          return TRUE;
                      }
                      break; 
  
                  break;
                  }
  
          }
            return false;
      }
  </code></pre>