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
Now that view scrolling works, we move on to our next topic: working with MDI menus and multiple menus. ### Working with Multiple Menus and View Types There are two usual types of menus in MDI programs: one default menu system displayed by the MDI main window if no child windows are open, and a menu system used when child windows are visible. In this next example, we deal with the two different menu systems, how to work with all the views connected to a document, and how to support multiple view types in the same MDI program. We start by working with MDI menus. #### Working with MDI Menus We start our MDI menu work by creating a new MDI project named MDIMenus. AppWizard has given us two menu systems in this project: IDR_MAINFRAME, which has File, View, and Help menus, and IDR_MENUSMTYPE, which has File, Edit, View, Window, and Help menus. The first menu system, IDR_MAINFRAME, is the default menu system for the MDI main window. If there are no child windows, that menu system is active. If there are child windows, the program uses the IDR_MENUSMTYPE menu system (the name comes from the name of the project: MDIMenus). Let’s use the menu system in our program now. In this case, we’re adding a new menu item that allows us to loop over all a document’s views. For example, to minimize MDI child windows, we add a new menu item, “Restore Views,” to the Window menu that will loop over all the views connected with the currently active document and open them if they are minimized. In this way, we are able to work with all the views connected to a document. To add the new menu item, “Restore Views,” double-click the IDR_MENUS-MTYPE menu in the Visual C++ ResourceView tab. This opens the Menu Editor, as shown in Figure 2.7. Figure 2.7 Adding a menu item. Figure 2.8 Setting a menu item’s properties. Open the Window menu in the Menu Editor now. In it is a blank menu item outlined with a dotted border, as shown in Figure 2.7. Type “Restore Views” into that menu item. When you type the menu item’s new caption, the Menu Item Properties box opens, as shown in Figure 2.8. As shown in Chapter 3, “Edit Views, HTML Views, Tree Views, Splitter Windows, and More,” you can set various menu item options here, such as displaying the menu item as disabled, checked, and so on. When you’re done typing the menu item’s caption, close the Menu Item Properties box. To connect the new menu item to our code, right-click it with the mouse in the Menu Editor and open the ClassWizard from the popup menu that appears, as shown in Figure 2.9. To connect a handler function for our new menu item, make sure CMDIMenusView is selected in the ClassWizard Class name box and double-click the ID for our new menu item, ID_WINDOWS_RESTOREVIEWS, in the object IDs box. This creates and opens the message-handling function OnWindowRestoreviews(). Figure 2.9 Connecting a menu item to code.
    void CMenusMDIView::OnWindowRestoreviews()

    {
        // TODO: Add your command handler code here

    }
In this function, we want to loop over all child windows and restore them if they’ve been minimized. #### Accessing All of a Document’s Views We want to loop over all the views connected to the current document and restore them if they’ve been minimized. We begin by getting a pointer to the current document.
    void CMenusMDIView::OnWindowRestoreviews()

    {
        CMenusMDIDoc* pDoc = GetDocument();                ⇐
        ASSERT_VALID(pDoc);                                ⇐
            .
            .
            .
Next, we use the CDocument function GetFirstPosition() to get a POSITION object referring to the first view connected to this document.
    void CMenusMDIView::OnWindowRestoreviews()

    {
        CMenusMDIDoc* pDoc = GetDocument();
        ASSERT_VALID(pDoc);

        POSITION pos = pDoc->GetFirstViewPosition();        ⇐
            .
            .
            .
Now we can loop over all views using the CDocument GetNextView() function, getting a pointer to successive views.
    void CMenusMDIView::OnWindowRestoreviews()

    {
        CMenusMDIDoc* pDoc = GetDocument();
        ASSERT_VALID(pDoc);

        POSITION pos = pDoc->GetFirstViewPosition();

        while(pos != NULL)                         ⇐
        {                                          ⇐
           CMenusMDIView* pMDIView = (CMenusMDIView*)pDoc->GetNextView(pos);      ⇐
            .
            .
            .
Now that we have successive pointers to the various views connected to this document, we want to restore each view to its original size. We do that with the MDIRestore() function. However, don’t use this function on the view object directly, use MDIRestore() on MDI child windows. The view is the child of these child windows (the view window covers the MDI child window’s client area), so we need a pointer to the current view’s parent—the MDI child window we want to restore—before restoring those windows. We do that with GetParent().
    void CMenusMDIView::OnWindowRestoreviews()

    {
        CMenusMDIDoc* pDoc = GetDocument();
        ASSERT_VALID(pDoc);

        POSITION pos = pDoc->GetFirstViewPosition();

        while(pos != NULL)
        {
            CMenusMDIView* pMDIView = (CMenusMDIView*) pDoc->GetNextView(pos);
            CMDIChildWnd* parent = (CMDIChildWnd*) pMDIView->GetParent();   ⇐
                .
                .
                .
        }

    }
All that’s left is to restore the MDI child window, which looks like this:
    void CMenusMDIView::OnWindowRestoreviews()

    {
        CMenusMDIDoc* pDoc = GetDocument();
        ASSERT_VALID(pDoc);

        POSITION pos = pDoc->GetFirstViewPosition();

        while(pos != NULL)
        {
            CMenusMDIView* pMDIView = (CMenusMDIView*) pDoc->GetNextView(pos);
            CMDIChildWnd* parent = (CMDIChildWnd*) pMDIView->GetParent();
            parent->MDIRestore();                   ⇐
        }

    }
Now the user can restore any views into the current document that have been minimized simply by selecting our Restore Views menu item. We have just seen how to work with the multiple menu systems of an MDI program and how to work with all the views connected to a document. Next, we will turn to a new topic: how to support multiple view types in the same MDI program. #### Supporting Multiple View Types So far, all our MDI programs have only supported one type of view, but we can also support other types of views in the same program. We can modify the MenusMDI program to support a new type of view based on the CEditView class, which, in turn, is based on the text box class, CEdit. This view covers the MDI child window with a multiline text box that the user can type into directly.
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.