弹出菜单不发送 WM_COMMAND 消息
Posted
技术标签:
【中文标题】弹出菜单不发送 WM_COMMAND 消息【英文标题】:POPUP menu not send WM_COMMAND message 【发布时间】:2014-04-19 09:09:30 【问题描述】:我使用的是vc6.0。右键单击列表框项目时,我会加载一个弹出菜单。但是当我单击弹出菜单项时,它似乎没有发送 WM_COMMAND 消息。在谷歌搜索后,我对此一无所知。有谁知道吗?
void PT_OnContextMenu(HWND hwnd, HWND hwndContext, UINT xPos, UINT yPos)
HWND hList = GetDlgItem(hwnd,IDC_LIST_PRESTYPE);
if (hList == hwndContext)
if(-1!=indexLB)
RECT rect;
POINT pt;
pt.x = xPos;
pt.y = yPos;
ListBox_GetItemRect(hwndContext, indexLB, &rect);
ScreenToClient(hwndContext, &pt);
if(PtInRect(&rect, pt))
HMENU hMenu = LoadMenu((HINSTANCE)GetWindowLong(hwnd, GWL_HINSTANCE), MAKEINTRESOURCE(IDR_MENU_RDELTYPE));
if(hMenu)
HMENU hpop = GetSubMenu(hMenu,0);
ClientToScreen(hwndContext, &pt);
TrackPopupMenu(hpop,
TPM_LEFTALIGN | TPM_TOPALIGN | TPM_RIGHTBUTTON,
pt.x,
pt.y,
0,
hwndContext,
NULL);
DestroyMenu(hMenu);
在下面的代码中没有得到消息框。
void PT_OnCommand(HWND hwnd, int id, HWND hwndCtl, UINT codeNotify)
switch(id)
case ID_MENUITEM_RDELTYPE:
MessageBox(hwnd,TEXT("dddd!"),TEXT("dddd"),MB_OK);
break;
default:
break;
【问题讨论】:
【参考方案1】:解决了。在MSDN找到它
A handle to the window that owns the shortcut menu. This window receives all messages
from the menu. The window does not receive a WM_COMMAND message from the menu until the
function returns. If you specify TPM_NONOTIFY in the uFlags parameter, the function does
not send messages to the window identified by hWnd. However, you must still pass a window
handle in hWnd. It can be any window handle from your application.
我将hwnd
设置为列表框而不是对话框。
【讨论】:
以上是关于弹出菜单不发送 WM_COMMAND 消息的主要内容,如果未能解决你的问题,请参考以下文章
MFC对话框中如何响应弹出式菜单事件 点击按钮 弹出菜单 菜单消息事件响应不了