MFC List Control: How to use a context menu in a list control?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 1 of 1

Thread: MFC List Control: How to use a context menu in a list control?

  1. #1
    Join Date
    Jun 2001
    Location
    Switzerland
    Posts
    4,443

    MFC List Control: How to use a context menu in a list control?

    Q: How to use a context menu in a list control?

    A:

    • Create a menu in the resource editor.


    • Derive your own class from CListBox and insert the WM_CONTEXTMENU handler in the class.


    • Insert the following code in your 'OnContextMenu()' function:

      Code:
      void CMyListBoxCtl::OnContextMenu(CWnd*, CPoint point)
      {
        if((point.x == -1) && (point.y == -1))
        {
          // Keystroke invocation
          CRect rect;
      
          GetClientRect(rect);
          ClientToScreen(rect);
      
          point = rect.TopLeft();
          point.Offset(5, 5);
        }
      
        CMenu menu;
        VERIFY(menu.LoadMenu(MY_CREATED_MENU));
      
        CMenu* pPopup = menu.GetSubMenu(0);
        ASSERT(pPopup != NULL);
        CWnd* pWndPopupOwner = this;
      
        while(pWndPopupOwner->GetStyle() & WS_CHILD)
          pWndPopupOwner = pWndPopupOwner->GetParent();
      
        pPopup->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON, 
                                  point.x, 
                                  point.y,
                                  pWndPopupOwner);
      }
    • Create a member variable from your List Control and change the class name with your derived class.



    Last edited by Andreas Masur; July 25th, 2005 at 04:47 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center