列表框上下文菜单

发布于 2024-08-06 08:32:36 字数 275 浏览 2 评论 0原文

如何在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

短叹 2024-08-13 08:32:36

由于我的编辑被拒绝,理由是“改变太多”,我将把我的建议放在这里,因为在我看来,原始代码会促进不良的编码实践。

void CYourDialog::OnContextMenu(CWnd* pWnd, CPoint point)
{
    int CtrlID = pWnd->GetDlgCtrlID();

    if (CtrlID == ID_YOUR_LIST) {
        CMenu menu;
        // Create your menu items...
        int retVal = menu.TrackPopupMenu(TPM_LEFTALIGN | TPM_TOPALIGN | TPM_NONOTIFY | TPM_RETURNCMD, point.x, point.y, this);
        // Handle selected options here...
    }
}

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.

void CYourDialog::OnContextMenu(CWnd* pWnd, CPoint point)
{
    int CtrlID = pWnd->GetDlgCtrlID();

    if (CtrlID == ID_YOUR_LIST) {
        CMenu menu;
        // Create your menu items...
        int retVal = menu.TrackPopupMenu(TPM_LEFTALIGN | TPM_TOPALIGN | TPM_NONOTIFY | TPM_RETURNCMD, point.x, point.y, this);
        // Handle selected options here...
    }
}
小兔几 2024-08-13 08:32:36

OnContextMenu 处理程序放入父类中。然后添加弹出菜单

编辑要添加OnContextMenu 处理程序,请将事件处理程序添加到父窗口(即不是列表类)。通过资源编辑器可以最轻松地完成此操作。转到属性页面,然后转到消息部分。然后为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.
    }
}

Put an OnContextMenu handler in the parent class. Then add a popup menu

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.
    }
}
最近可好 2024-08-13 08:32:36

您需要执行以下步骤:

  1. 添加
ON_WM_CONTEXTMENU()

BEGIN_MESSAGE_MAP()

看到类似 Add the Context Menu function 的内容

BEGIN_MESSAGE_MAP(CMyDialog, CDialogEx)
    ON_WM_SYSCOMMAND()
    ON_WM_QUERYDRAGICON()
    ON_WM_CONTEXTMENU()
END_MESSAGE_MAP()
  1. 因此您将在头文件中
afx_msg void OnContextMenu(CWnd* pWnd, CPoint point)
  1. :然后按照文章中的建议添加 Context Menu 函数:
void CMyDialog::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.
    }
}

You need to take the following steps:

  1. Add
ON_WM_CONTEXTMENU()

to

BEGIN_MESSAGE_MAP()

So you will have something like

BEGIN_MESSAGE_MAP(CMyDialog, CDialogEx)
    ON_WM_SYSCOMMAND()
    ON_WM_QUERYDRAGICON()
    ON_WM_CONTEXTMENU()
END_MESSAGE_MAP()
  1. Add the Context Menu function in the header file:
afx_msg void OnContextMenu(CWnd* pWnd, CPoint point)
  1. Then add the Context Menu function, as suggested in the article:
void CMyDialog::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.
    }
}

北恋 2024-08-13 08:32:36

为您的对话框窗口添加一个处理程序。这将生成:

void YourDialogClass::OnContextMenu(CWnd* pWnd, CPoint point) {
  ...
}

pWnd 将指向用户右键单击鼠标的窗口/控件。

Add a handler for your dialog window. That will generate this:

void YourDialogClass::OnContextMenu(CWnd* pWnd, CPoint point) {
  ...
}

pWnd will point to the window/control in which the user right clicked the mouse.

我家小可爱 2024-08-13 08:32:36

如果您按照教程派生了自己的类,请确保将 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文