我有一个作为资源创建的列表视图并加载到对话框窗口中。我只想在单击列表视图中的项目时检测并显示上下文菜单。
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;
}
好的,我已经编辑了它,它的工作方式与此处显示的方式相同,但问题仍然存在,有人可以向我解释这里的焦点小组是什么以及为什么如果我在对话框中发送 LVM_GETNEXTITEM 消息它返回!= -1?不只是为了 Listviews 吗?
编辑 :
这是我根据您的回答制定的另一种选择:
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;
}