问题描述
我有一个带有列表框的窗口,我想右键单击列表框中的条目并在上下文菜单中显示某些选项.我在网上看过,但似乎我只能在 MFC C++ 或 C# 中获得示例.
I have a window with a listbox which I would like to right click an entry in the list box and have certain options displayed in a context menu. I've looked online but it seems as if I can only get examples in MFC C++, or simply c#.
这在标准的 Win32 C++ 中是可能的吗?我可以通过 WM_CONTEXTMENU 消息处理对列表框的右键单击,但是如何显示菜单?
Is this possible in standard Win32 c++? I can handle a right click on the listbox via the WM_CONTEXTMENU message, but how can I make a menu appear?
推荐答案
我想通了,感谢 Bhargav Bhat 对相关问题的评论.
I figured it out, thanks to Bhargav Bhat's comment about the related question.
通过检查 WndProc 中的 WM_CONTEXTMENU 处理列表框的右键单击.
Handle right click on the listbox via checking for WM_CONTEXTMENU in your WndProc.
通过 wParam 参数获取窗口句柄,将其与您的列表框进行比较,以查看用户是否右键单击了列表框.
Grab the handle to the window via the wParam parameter, compare it to your listbox to see if the user right clicked the listbox.
从那里,通过 CreatePopupMenu() 创建弹出菜单.
From there, create the popupmenu via CreatePopupMenu().
通过 InsertMenu()/AppendMenu() 在菜单中插入/追加.
Insert/Append into the menu via InsertMenu()/AppendMenu().
最后,调用 TrackPopupMenu().
Finally, call TrackPopupMenu().
case WM_CONTEXTMENU:
if ((HWND)wParam == m_hListBox) {
m_hMenu = CreatePopupMenu();
InsertMenu(m_hMenu, 0, MF_BYCOMMAND | MF_STRING | MF_ENABLED, 1, "Hello");
TrackPopupMenu(m_hMenu, TPM_TOPALIGN | TPM_LEFTALIGN, GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam), 0, m_hWnd, NULL);
}
这篇关于如何在列表框右键单击上创建上下文菜单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!