Brief Full Advanced Search Search Tips
|  |  | To access the contents, click the chapter and section titles. Fast Track Visual C++ 6.0 Programming (Publisher: John Wiley & Sons, Inc.) Author(s): Steve Holzner ISBN: 0471312908 Publication Date: 09/01/98 Search this book:
Previous | Table of Contents | Next
To begin, we set both m_box and m_circle to false. When the user selects the new menu item for the first time, we want to set m_circle true so the program draws a circle. In addition, we want to create the box bitmap for the menu item and install it if this is the first time the user has selected this item. To draw the bitmap, we need a new device context set up in memory, and we name that device context memoryDC. This device context has to be compatible with the current screen settings, so we make this device context compatible with the screen by creating a client device context and using CreateCompatibleDC().
    void CMenusView::OnFigure()
    {

        m_circle = !m_box;                          ⇐

        if(!m_box){                                 ⇐
         CClientDC DC(this);                        ⇐
         CDC memoryDC;                              ⇐
         memoryDC.CreateCompatibleDC(&DC);          ⇐
             .
             .
             .
We then create a bitmap, making it compatible with the screen also with CreateCompatibleBitmap() and we install that bitmap in the memory device context. We’re free to draw the box we want to use as our new menu item, and we do that with the Rectangle function.
    void CMenusView::OnFigure()
    {

        m_circle = !m_box;

        if(!m_box){
         CClientDC DC(this);
         CDC memoryDC;
         memoryDC.CreateCompatibleDC(&DC);
         CBitmap* bitmap = new CBitmap;                  ⇐
         bitmap->CreateCompatibleBitmap(&DC, 20, 15);    ⇐
         memoryDC.SelectObject(bitmap);                  ⇐
         memoryDC.Rectangle(0, 0, 20, 15);               ⇐
               .
               .
               .
    }
The bitmap we want to install in our menu is ready to use. All that remains is to install it. ### Modifying Menu Items at Runtime To modify the Draw menu, we use the MFC CMenu ModifyMenu() function. To be able to use that function, we first have to be able to reach the program’s menu system. How do we do that from the view class? In an SDI program, the menu is attached to the main frame window, so we reach that window with the GetParent() function. We get the main window’s menu system with a call to the main window’s GetMenu() function. Now we’re ready to use ModifyMenu(). This function takes the ID of the item to modify (ID_FIGURE) or its position, then a parameter that can be either of MF_BYCOMMAND or MF_BYPOSITION to indicate whether we’ve referred to the menu item by command ID or by position, the new ID for the modified menu item, and a string for the new caption or a pointer to a bitmap. We install our new bitmap in the menu item as shown in the following example, then we set m_box to true to indicate that we should draw boxes from now on, and invalidate the view to draw the current figure:
    void CMenusView::OnFigure()
    {

        m_circle = !m_box;

        if(!m_box){
         CClientDC DC(this);
         CDC memoryDC;
         memoryDC.CreateCompatibleDC(&DC);
         CBitmap* bitmap = new CBitmap;
         bitmap->CreateCompatibleBitmap(&DC, 20, 15);
         memoryDC.SelectObject(bitmap);
         memoryDC.Rectangle(0, 0, 20, 15);
         (GetParent()->GetMenu())->ModifyMenu(ID_FIGURE, MF_BYCOMMAND, ID_FIGURE,
    bitmap);                                 ⇐
         m_box = true;                   ⇐
        }
        Invalidate();
    }
We have successfully added our bitmapped menu item to the Draw menu. Run the program now, and select the Circle menu item. When you open the Draw menu again, that item has changed to a box. From then on, it only draws boxes, as shown in Figure 4.9. We are now able to use bitmapped menu items and modify menu items on the fly, which adds a lot of menu power (and professionalism) to our programs. Our next, and last, menu topic is an exciting one: popup menus. Figure 4.9 Creating bitmapped menu items. #### Popup Menus In this menu example, we create a popup menu. When the user right-clicks the client area of our program (that is, the view), we display a popup menu detached from the menu bar.
           ------------------------------------------------------
         |File  Edit  View  Choices  Draw  Help          |
         |------------------------------------------------------|
         |                                               |
         |                    ------------               |
         |                   | Popup menu                |       |
         |                   | Beep                      |       |
         |                    ------------                       |
         |                                               |
         |                                               |
         |                                               |
          ------------------------------------------------------
This menu functions like any other menu. The user can make selections from it or dismiss it by clicking the client area outside the menu. The Popup menu item functions as a menu title here, and the Beep item causes the computer to beep. To see how this works, we start by creating a new popup menu. You might think we could do that in the view class’s constructor, but in fact, the view hasn’t been created yet in the constructor (many MFC objects need a two-step initialization process—creating the object and then calling its Create() function). It’s safer to use OnInitialUpdate() for operations such as attaching new menus to views, so use ClassWizard to override the view class’s OnInitialUpdate() function.
    void CMenusView::OnInitialUpdate()
    {
        CView::OnInitialUpdate();

        // TODO: Add your specialized code here and/or call the base class

    }
Then, we create a new popup menu from the CMenu class. We do that by creating a new CMenu object and calling that object’s CreatePopupMenu() function.
    void CMenusView::OnInitialUpdate()
    {
        CView::OnInitialUpdate();

        m_menu = new CMenu();                       ⇐
        m_menu->CreatePopupMenu();                  ⇐
         .
         .
         .
Once the new popup menu has been created, we can add the items we want in that menu. We place two items in this menu: “Popup menu” and “Beep,” which causes the computer to beep when the user selects the Beep item. To make use of these items, we need an ID value for both of them. Use the String Editor to create two new IDs: ID_POPUP and ID_BEEP. After that is done, we can add the new menu items to the menu using AppendMenu(). This function takes a parameter set to one of the values in Table 4.1 (we use MF_STRING), the ID of the new item, and the new caption of the item.
Previous | Table of Contents | Next

| Products | Contact Us | About Us | Privacy | Ad Info | Home Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.