MFC method to create right click popup menu

  • 2020-04-02 02:57:56
  • OfStack

This article illustrates the MFC method of creating right - click popup menu. Share with you for your reference. The specific implementation method is as follows:

, add a menu resource, ID for IDM_RIGHTMENU. Because the top-level menu is not displayed when the right-click menu is displayed, you can set any text to it.

(2) add the WM_RBUTTONDOWN message to the visual class and handle the right-click popup menu in the message. Because the viewclass window always overrides the frame window, the frame window does not receive the mouse message, so the WM_RBUTTONDOWN message is captured by the viewclass.

void CMenuView::OnRButtonDown(UINT nFlags, CPoint point)
{
    //TODO: add the message handler code and/or invoke the default values here     CMenu menu;
    menu.LoadMenuW(IDM_RIGHTMENU);//Load menu resource
    CMenu *pPopup=menu.GetSubMenu(0);
    ClientToScreen(&point);//Converts the client area coordinates to screen coordinates     //Displays the right-click menu, owned by the viewclass window. < br / >     pPopup->TrackPopupMenu(TPM_LEFTALIGN|TPM_RIGHTBUTTON,point.x,point.y,this);
    //The last parameter for the TrackPopupMenu is
, which allows the parent window to have a right-click menu for GetParent's frame window     //The frame class window gets the command response to the menu item in the right-click menu,
    CView::OnRButtonDown(nFlags, point);
}

, add command message response function (omitted) for the right menu item.

Hope that this article based on everyone's MFC VC++ program design is helpful.


Related articles: