列表框上下文菜单 [英] List box context menu
问题描述
如何在MFC的列表框中添加上下文菜单?我在列表框的属性中看不到任何WM_CONTEXTMENU处理程序.有什么想法吗?
How do I add a context menu in a list box in MFC? I don't see any WM_CONTEXTMENU handler in list box's properties. Any ideas?
编辑:我遵循了本教程 MFC列表控件:如何在列表控件中使用上下文菜单?.教程说要从CListBox派生我自己的类,但是现在如何将派生类的列表框添加到对话框中?
I followed this tutorial MFC List Control: How to use a context menu in a list control?. The tutorial says to derive my own class from CListBox which I did, but now how do I add list box of my derived class to the dialog?
推荐答案
在父类中放置一个 OnContextMenu
处理程序.然后添加一个弹出菜单
Put an OnContextMenu
handler in the parent class. Then add a popup menu
编辑要添加 OnContextMenu
处理程序,请将事件处理程序添加到PARENT窗口(即不是列表类).通过资源编辑器最容易做到这一点.转到属性页面,然后转到消息部分.然后为 WM_CONTEXTMENU
添加一个功能.
Edit To add the OnContextMenu
handler, add an event handler to the PARENT window (ie not the list class). This is most easily done through the resource editor. Go to the properties page then go to the messages section. Then add a function for WM_CONTEXTMENU
.
void CYourDialog::OnContextMenu(CWnd* pWnd, CPoint point)
{
CListCtrl* pList = (CListCtrl*)GetDlgItem( ID_YOUR_LIST );
if ( (CWnd*)pList == pWnd )
{
CMenu menu;
// Create your menu items.
int retVal = menu.TrackPopupMenu( TPM_LEFTALIGN | TPM_TOPALIGN | TPM_NONOTIFY | TPM_RETURNCMD, point.x, point.y, this );
// Handle your returns here.
}
}
这篇关于列表框上下文菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!