Home

MFC Topics: Maintenance of Menu Items

 

Menu Enabled or Disabled

If you have just created a menu and has not yet mapped it to a method that would process its message, the menu is disabled and appears gray. If you map a menu item to a method, it becomes enabled, indicating that the user can use it. In the following window, the New and the Open menu items are disabled while the Exit menu item is enabled:

You also can enable or disable a menu item any time you judge this necessary. To visually enable a menu item when designing it, in the Properties window, set its Enabled property to False. To visually enable a menu item, in the Properties window, set its Enabled property to True.

To programmatically enable or disable a menu, call the CMenu::EnableMenuItem() method. 

Menu Deletion

If you visually create a menu,, when the application exists, it would delete it and remove its resources from the memory. If you dynamically create a menu, to delete it after using it, you can call the CMenu::DestroyMenu() method. Its syntax is:

BOOL DestroyMenu();

As you can see, the method doesn’t take any argument. 

Practical Learning Practical Learning: Deleting a Menu

  1. Access the MainFrm.cpp file and implement the destructor as follows:
    CMainFrame::~CMainFrame()
    {
    	// If the pointer to CMenu exists, destroy it
    	if( pCurrentMenu )
    		pCurrentMenu->DestroyMenu();
    }
  2. Save all 

Using Different Menus

Because an application can deal with more than one type of document, using the same menu for different documents is not useful and can be confusing. To address such an issue, you can perform various continuous operations of adding, deleting, enabling, and disabling some menu items. If the documents are significantly different, a better alternative could consist of using different menus in the same application. At one time, you can display one menu. At another time you can, at least temporarily, replace one menu with another. This is made possible by using a combination of the CMenu methods we have reviewed so far.

Practical Learning Practical Learning: Using Different Menus

  1. To add another menu, in the Resource View, right-click IDR_FIRST and click Insert Menu
  2. In the Resource View, click the new menu and, in the Properties window, change its ID to IDR_SECOND
  3. In the menu designer, click Type Here and type File
  4. Press the down arrow key and type Exit
  5. In the Properties window, click ID and type IDM_FILE_EXIT
  6. On the right side of File, click Type Here and type View
  7. Click the down arrow key and type Customers Orders
  8. Press the down arrow key and type Employees Issues
  9. On the right side of View, click Type Here and type Processes
  10. Press the down arrow key and type Time Sheets
  11. Press the down arrow key and type Request for Time Off
  12. Press the down arrow key and type Leave/Absence
  13. Press the down arrow key and type Benefits
  14. Right-click View and click Copy
  15. In the Resource View, double-click IDR_FIRST
  16. Click Help and press Ins (or Insert)
  17. Press Ctrl + V to paste the menu
  18. Access the MainFrm.h header file and change it as follows:
     
    #include "stdafx.h"
    
    class CMainFrame : public CFrameWnd
    {
    public:
    	CMainFrame();
    	virtual ~CMainFrame();
    
    	afx_msg void Close();
    	afx_msg void ShowFirstMenu();
    	afx_msg void ShowSecondMenu();
    
    	DECLARE_MESSAGE_MAP()
    
    private:
    	CMenu *pCurrentMenu;
    };
  19. Access the MainFrm.cpp source file and change it as follows:
     
    #include "MainFrm.h"
    
    CMainFrame::CMainFrame()
    {
    	. . .
    }
    
    CMainFrame::~CMainFrame()
    {
    
    }
    
    BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
    	ON_COMMAND(IDM_FILE_EXIT, Close) 
    	ON_COMMAND(ID_VIEW_CUSTOMERSORDERS, ShowFirstMenu)
    	ON_COMMAND(ID_VIEW_EMPLOYEESISSUES, ShowSecondMenu)
    END_MESSAGE_MAP()
    
    void CMainFrame::ShowFirstMenu()
    {
    	this->SetMenu(NULL);
    	this->pCurrentMenu->DestroyMenu();
    	this->pCurrentMenu->LoadMenuW(IDR_FIRST);
    	this->SetMenu(this->pCurrentMenu);
    	this->DrawMenuBar();
    }
    
    void CMainFrame::ShowSecondMenu()
    {
    	this->SetMenu(NULL);
    	this->pCurrentMenu->DestroyMenu();
    	this->pCurrentMenu->LoadMenuW(IDR_SECOND);
    	this->SetMenu(this->pCurrentMenu);
    	this->DrawMenuBar();
    }
    
    void CMainFrame::Close()
    {
    	PostQuitMessage(WM_QUIT);
    }
  20. Execute the application
  21. Close the application and return to MSVC

Introduction to Context-Sensitive Menu

So far, we have treated the menu in a general concept mostly applied to the main menu. Another type of menu, probably less popular but not uncommon is one that appears when the user clicks an item somewhere in a window. This type of menu is referred to as context-sensitive because it depends on the item that would display it. Just like the main menu, before using a context-sensitive menu, you must first create it and you and you must decide when and where it would be applied. Because an application can be made of various objects, it is up to you to decide what object would use context-sensitive menu and how.

Although a context-sensitive menu can display when the user clicks the defined item, to allow the user to perform regular mouse-click actions, a context-sensitive menu is usually made available when the user right-clicks the designated item.

Implementation of a Context-Sensitive Menu

While a main menu usually appears as more than one group of items, a context-sensitive menu usually displays one column. The menu itself is primarily created like any other menu: you first define a popup item and then add the other sub-items.

 

Previous Copyright © 2006-2007 FunctionX, Inc.