列表框上下文菜单
如何在 MFC 的列表框中添加上下文菜单?我在列表框的属性中没有看到任何 WM_CONTEXTMENU 处理程序。有什么想法吗?
编辑:我按照本教程MFC列表控件:如何在列表控件中使用上下文菜单?。本教程说要从我所做的 CListBox 派生我自己的类,但现在如何将派生类的列表框添加到对话框中?
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?
EDIT: 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
由于我的编辑被拒绝,理由是“改变太多”,我将把我的建议放在这里,因为在我看来,原始代码会促进不良的编码实践。
Since my edit was rejected with the rationale of "changing too much", I will put my proposal here because in my opinion the original code promotes bad coding practices.
将
OnContextMenu
处理程序放入父类中。然后添加弹出菜单编辑要添加
OnContextMenu
处理程序,请将事件处理程序添加到父窗口(即不是列表类)。通过资源编辑器可以最轻松地完成此操作。转到属性页面,然后转到消息部分。然后为WM_CONTEXTMENU
添加一个函数。Put an
OnContextMenu
handler in the parent class. Then add a popup menuEdit 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 forWM_CONTEXTMENU
.您需要执行以下步骤:
到
看到类似 Add the Context Menu function 的内容
You need to take the following steps:
to
So you will have something like
为您的对话框窗口添加一个处理程序。这将生成:
pWnd
将指向用户右键单击鼠标的窗口/控件。Add a handler for your dialog window. That will generate this:
pWnd
will point to the window/control in which the user right clicked the mouse.如果您按照教程派生了自己的类,请确保将
ON_WM_CONTEXTMENU()
添加到新类消息映射中。要添加派生类的列表框,只需为 ListBox 控件添加一个变量并将变量类指定为派生类即可。
不过我认为@Goz 的答案也是一个有效的解决方案,而且是一个更简单的解决方案。
If you followed the tutorial to derive you own class, make sure
ON_WM_CONTEXTMENU()
is added to the new class message map.To add a list box of your derived class, you simply add a variable for your ListBox control and specify the variable class as your derived class.
However I think @Goz's answer is also a valid solution, and a simpler one.