- Table of Contents
- Copyright
- About the Authors
- About the Contributors
- Acknowledgments
- Tell Us What You Think!
- Introduction
- How to Use This Book
- What You Need to Use This Book
- What's New in Visual C++ 6.0
- Contacting the Main Author
- Part I: Introduction
- Chapter 1. The Visual C++ 6.0 Environment
- Part II: MFC Programming
- Chapter 2. MFC Class Library Overview
- Chapter 3. MFC Message Handling Mechanism
- Chapter 4. The Document View Architecture
- Chapter 5. Creating and Using Dialog Boxes
- Chapter 6. Working with Device Contexts and GDI Objects
- Chapter 7. Creating and Using Property Sheets
- Chapter 8. Working with the File System
- Chapter 9. Using Serialization with File and Archive Objects
- Part III: Internet Programming with MFC
- Chapter 10. MFC and the Internet Server API (ISAPI)
- Chapter 11. The WinInet API
- Chapter 12. MFC HTML Support
- Part IV: Advanced Programming Topics
- Chapter 13. Using the Standard C++ Library
- Chapter 14. Error Detection and Exception Handling Techniques
- Chapter 15. Debugging and Profiling Strategies
- Chapter 16. Multithreading
- Chapter 17. Using Scripting and Other Tools to Automate the Visual C++ IDE
- Part V: Database Programming
- Chapter 18. Creating Custom AppWizards
- Chapter 19. Database Overview
- Chapter 20. ODBC Programming
- Chapter 21. MFC Database Classes
- Chapter 22. Using OLE DB
- Chapter 23. Programming with ADO
- Part VI: MFC Support for COM and ActiveX
- Chapter 24. Overview of COM and Active Technologies
- Chapter 25. Active Documents
- Chapter 26. Active Containers
- Chapter 27. Active Servers
- Chapter 28. ActiveX Controls
- Part VII: Using the Active Template Library
- Chapter 29. ATL Architecture
- Chapter 30. Creating COM Objects Using ATL
- Chapter 31. Creating ActiveX Controls Using ATL
- Chapter 32. Using ATL to Create MTS and COM+ Components
- Part VIII: Finishing Touches
- Chapter 33. Adding Windows Help
- Part IX: Appendix
More on Working with Menus
In your Windows application, menus undoubtedly will make up a substantial part of your user interface. You looked at how to create menu resources in Chapter 1, and in this chapter, you learned how you can use document templates to assign a menu to a frame window. In this section, you will see how to update the user interface for your menus, implement pop-up menus, and create menus dynamically.
Updating the User Interface
MFC provides mechanisms for automatically updating the status of the command- generating controls of your user interface—namely, menus and toolbar buttons. This is done by implementing handlers for the UPDATE_COMMAND_UI message, which can be done from the ClassWizard when you select a command object. Alternatively, you could add ON_COMMAND_UPDATE_UI macros to your message map by hand:
ON_UPDATE_COMMAND_UI(ID_APP_EXIT, OnUpdateAppExit)
You then implement a handler function. Here, I have decided that I may want to disable the File | Exit command, so I have created a handler for the UPDATE_COMMAND_UI message:
void CMainFrame::OnUpdateAppExit(CCmdUI* pCmdUI)
{
if(m_bTrapUser)
pCmdUI->Enable(FALSE);
}
The handler is passed a pointer to a CCmdUI object, which should be used for updating the interface item. This will update both the menu item and toolbar button for the given command.
Pop-Up Menus
You may have seen several different applications that use pop-up menus. It has become increasingly popular for applications to generate pop-up menus after the user clicks the right mouse button, such as the context menus found throughout Visual C++.
You actually can add context menu support simply by adding the PopUpMenu component from the component gallery; however, let's look at how you can do this yourself. To create your own pop-up menu, you first should create a menu resource in the Resource Editor, although you will learn how to create menus in your code in just a bit. When you are creating a pop-up menu in the Menu Editor, the caption at the top of your menu will not actually be displayed, so use any placeholder you want.
Next you declare a CMenu object and load it with the resource you created:
CMenu myPopupMenu; myPopupMenu.LoadMenu(IDR_MYPOPUPMENU);
Note that you probably will want to do this a bit differently in your application. That is, you may want to declare your CMenu object in your frame class declaration or create it on the heap with the new operator. Now you want to allow the user to use the pop-up menu. This is done best by creating a handler for the WM_CONTEXTMENU message with the ClassWizard. Then when you want to display your pop-up menu, you should add something like this:
void CMainFrame::OnContextMenu(CWnd* pWnd, CPoint point)
{
POINT curPos;
GetCursorPos(&curPos);
CMenu* pSubMenu = myPopupMenu.GetSubMenu(0);
pSubMenu->TrackPopupMenu(TPM_LEFTALIGN | TPM_LEFTBUTTON,
curPos.x, curPos.y, this);
}
Here you use GetSubMenu() to reach the first submenu that contains your pop-up. You then call TrackPopupMenu() to present the menu to the user. This call accepts a few styles—in this case, dictating that the menu's left edge is aligned with the position you give in the second and third parameters, and that the menu will respond to the left mouse button. The last parameter is a pointer to the parent window; here, it is the main frame.
After you call TrackPopupMenu(), it handles itself until the user chooses a command from the menu or dismisses it. Commands from pop-up menus can be handled just like any other menu command.
Creating Menus Dynamically
If you want to create the menu used in the previous example within your code, this is actually quite simple. First declare your CMenu object as in the example. Then call CMenu::CreatePopupMenu() to get a valid Windows menu, and add your menu items to it with CMenu::AppendMenu():
m_MyMenu.CreatePopupMenu();
m_MyMenu.AppendMenu(MF_ENABLED, ID_FILE_NEW, _T("&New File"));
m_MyMenu.AppendMenu(MF_ENABLED, ID_FILE_CLOSE, _T("&Close File"));
You also may use the InsertMenu() function to place menu items anywhere in the menu. You also can dynamically modify the menu for any window by first getting a pointer to the window's CMenu object with a call to CWnd::GetMenu();.
One advantage of creating your own menus this way is that you no longer have to monkey around with submenus to create the pop-up. In the previous example, you then could replace the call to TrackPopupMenu() with something like this:
m_MyMenu.TrackPopupMenu(TPM_LEFTALIGN | TPM_LEFTBUTTON,
curPos.x, curPos.y, this);
Adding Splitters to Your Application | Next Section

Account Sign In
View your cart