Solved

Dynamic Menu Options

Posted on 1997-02-21
1
324 Views
Last Modified: 2013-11-20
In the following snippet of code I have created a new popup menu and added a couple menu items with AppendMenu().  The popup is then appended to the menu associated with the CFrame window of the program.  The function is called from the OnCreate() message handler of the CView class of the
program:
      
int CSigmaView::OnCreate( LPCREATESTRUCT lpCreateStruct)
{
      ...

      cCWReport.SetRptMenu(this->GetParent());
            
      ...

      return 0;
}

  When the program runs, the top-level menu option "Reports" is apparently created OK because it appears at the end of the menu bar for the frame window. When the new menu option is selected it becomes highlighted and the program appears to hang for a few seconds as though it is processing something but the popup menu never appears and no error is generated.  I have used standard command ID's for the added menu items just for the sake of creating and testing the function.

void CSigmaReport::SetRptMenu(CWnd *pWnd)
{
      CMenu *pMenu;
      CMenu cPopup;

      cPopup.CreatePopupMenu();

      cPopup.AppendMenu(MF_ENABLED|MF_STRING,                           ID_FILE_OPEN, "&Bay Details");
      cPopup.AppendMenu(MF_ENABLED|MF_STRING,                           ID_FILE_OPEN, "&Vac Details");
      pMenu = pWnd->GetMenu();

      pMenu->AppendMenu(MF_POPUP|MF_STRING,                           (UINT)cPopup.GetSafeHmenu(),
                  "&Reports");
      pWnd->DrawMenuBar();
};
0
Comment
Question by:armg
1 Comment
 
LVL 23

Accepted Solution

by:
chensu earned 70 total points
ID: 1300304
In your program, cPopup is created on the stack. As soon as it goes out of the function (SetRptMenu) scope, it is destroyed automatically (the destructor CMenu::~CMenu() is called).

You should either put cPopup as a member varible of CSigmaView or allocate it dynamically by using new and delete.

Method 1:
CMenu cPopup; // member varible of CSigmaView

Method 2:
CMenu *pcPopup; // member varible of CSigmaView

pcPopup = new CMenu;
pcPopup->CreatePopupMenu();

void CSigmaView::OnDestroy()
{
    delete pcPopup;
    ...
}

0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now