Visual C++ 6 Unleashed

Visual C++ 6 Unleashed

By MICKEY WILLIAMS and David Bennett

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);

Share ThisShare This

Informit Network