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.
MESSAGE_HANDLER(WM_CONTEXTMENU,OnContextMenu)
LRESULT OnContextMenu(UINT /*uMsg*/, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
int iSelected = -1;
int iFocusGroup = -1;
iSelected = SendMessage((HWND)wParam, LVM_GETNEXTITEM, -1,LVNI_SELECTED);
iFocusGroup = ListView_GetFocusedGroup((HWND)wParam);
if( iSelected != -1 && 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;
}
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 ?
EDIT :
Here is another alternative that I've worked out based on your responses:
LRESULT OnNotifyRClick(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
switch (uMsg)
{
case WM_NOTIFY:
switch (((LPNMHDR)lParam)->code)
{
case NM_RCLICK:
if (((LPNMHDR)lParam)->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;
}
NM_RCLICK is your friend.
But it doesn't solve the whole problem, such as displaying a context menu when user hits the Windows menu key on his keyboard. This KB article shows how to combine NM_RCLICK and WM_CONTEXTMENU. (It's for the CTreeCtrl but adapting the code to CListView is trivial).