Modifying the Toolbar and Menu

   

This hour's lesson starts with the MDIWindowsForms application you worked on in Hour 7, "Working with Windows Forms." You may also download the source code from the Sams Web site. Before getting into the printing functionality, we'll add a menu option and toolbar button to the application for the print feature.

The Print, Page Setup, and Print Preview menu items and Print toolbar button are standard for applications that have printing features. Listing 10.1 indicates which new method and members need to be added to the MDIWindowFrame class declaration. The additions that need to be added to the code from Hour 7 are shown in bold typeface.

Listing 10.1 The MDIWindowFrame Class Changes to Add the Print Menu Item and Toolbar Button
 1: __gc class MDIWindowFrame : public Form  2: {  3: protected:  4:  5:    // Printing Support  6:    MenuItem*  m_pFileSeparator1;  7:    MenuItem*  m_pFileSeparator2;  8:    MenuItem*  m_pFilePrintMenu;  9:    MenuItem*  m_pFilePrintPreviewMenu; 10:    MenuItem*  m_pFilePageSetupMenu; 11: 12:    // Created during hour 7 13:    MainMenu*  m_pMainMenu; 14:    MenuItem*  m_pFileMenu; 15:    MenuItem*  m_pFileNewMenu; 16:    MenuItem*  m_pFileExitMenu; 17:    MenuItem*  m_pWindowMenu; 18:    MenuItem*  m_pWindowCascadeMenu; 19:    MenuItem*  m_pWindowTileMenu; 20:    MenuItem*  m_pWindowCloseMenu; 21:    MenuItem*  m_pHelpMenu; 22:    MenuItem*  m_pHelpAboutMenu; 23: 24:    ToolBar* m_pMainToolbar; 25:    ToolBarButton* m_pFileNewButton; 26:    ImageList* m_pToolbarImages; 27:    System::ComponentModel::Container* m_pComponents; 28: 29:    // Printing Support 30:    void FilePrint_Click( Object* pSender, EventArgs* pArgs ); 31:    void FilePrintPreview_Click( Object* pSender, EventArgs* pArgs ); 32:    void FilePageSetup_Click( Object* pSender, EventArgs* pArgs ); 33: 34:    // Added during hour 7 35:    void FileExit_Click( Object* pSender, EventArgs* pArgs ); 36:    void FileNew_Click( Object* pSender, EventArgs* pArgs ); 37:    void HelpAbout_Click( Object* pSender, EventArgs* pArgs ); 38:    void WindowCascade_Click( Object* pSender, EventArgs* pArgs ); 39:    void WindowCloseAll_Click( Object* pSender, EventArgs* pArgs ); 40:    void WindowTile_Click( Object* pSender, EventArgs* pArgs ); 41:    void OnToolbarButtonClick( Object* pToolbar, 42:        ToolBarButtonClickEventArgs* pArgs ); 43: 44: protected: 45:    void InitForm(); 46:    void CreateMenu(); 47:    void CreateToolbar(); 48: 49:    // Printing Support 50:    void PrintForm( MDIChildForm* pChildForm ); 51: 52: public: 53:    MDIWindowFrame (); 54:    virtual ~MDIWindowFrame () {} 55: }; 

In order to enable the user to print, you have to add the appropriate menu items. In Listing 10.1, you added variables from the MenuItem class that will be used to add onto the menu already present. With these menu items, you will be able to change settings, view a print preview of the document, and print the document on the printer. Starting on line 30, you added three event handler declarations. As you'll recall from Hour 7, once the user clicks a menu item, the delegate (event handler) that is assigned to that menu item is called to perform the necessary menu functions. The last item that was added, line 50, will be the function that is called to print the document on the user's printer.

The next step is to add the new menu items to the existing main menu. Because you started with a modular design and placed all the menu-creation logic within the CreateMenu function, you will simply add onto this function to include the new items. Modifying the CreateMenu() method to add the new printing menu items is done by allocating new MenuItem classes and adding them to the File menu. For a review on how to add menu items and how to create event handlers for them, see Hour 9, "Programming with Graphics." The modifications to the File menu include the three print functions that you will add to the application, as indicated in Listing 10.2.

Listing 10.2 Modifications in the CreateMenu() Method to Add the Print Menu Item to the File Menu
 1: void MDIWindowFrame::CreateMenu()  2: {  3:    // Allocate printing support menu items  4:    m_pFileSeparator1       = new MenuItem;  5:    m_pFileSeparator2       = new MenuItem;  6:    m_pFilePrintMenu        = new MenuItem;  7:    m_pFilePrintPreviewMenu = new MenuItem;  8:    m_pFilePageSetupMenu    = new MenuItem;  9: 10:    // allocate menu items 11:    m_pMainMenu          = new MainMenu; 12:    m_pFileMenu          = new MenuItem; 13:    m_pFileNewMenu       = new MenuItem; 14:    m_pFileExitMenu      = new MenuItem; 15:    m_pWindowMenu        = new MenuItem; 16:    m_pWindowCascadeMenu = new MenuItem; 17:    m_pWindowTileMenu    = new MenuItem; 18:    m_pWindowCloseMenu   = new MenuItem; 19:    m_pHelpMenu          = new MenuItem; 20:    m_pHelpAboutMenu     = new MenuItem; 21: 22:    // 23:    // MainMenu 24:    // 25:    MenuItem* pMenuItems[] = new MenuItem*[3]; 26:    pMenuItems[0] = m_pFileMenu; 27:    pMenuItems[1] = m_pWindowMenu; 28:    pMenuItems[2] = m_pHelpMenu; 29:    m_pMainMenu->MenuItems->AddRange( pMenuItems ); 30: 31:    // 32:    // FileMenu 33:    // 34:    pMenuItems = new MenuItem*[7]; 35:    pMenuItems[0] = m_pFileNewMenu; 36:    pMenuItems[1] = m_pFileSeparator1; 37:    pMenuItems[2] = m_pFilePageSetupMenu; 38:    pMenuItems[3] = m_pFilePrintMenu; 39:    pMenuItems[4] = m_pFilePrintPreviewMenu; 40:    pMenuItems[5] = m_pFileSeparator2; 41:    pMenuItems[6] = m_pFileExitMenu; 42:    m_pFileMenu->MenuItems->AddRange( pMenuItems ); 43: 44:    m_pFileMenu->Index = 0; 45:    m_pFileMenu->Text = "&File"; 46: 47:    // FileNewMenu 48:    m_pFileNewMenu->Index = 0; 49:    m_pFileNewMenu->Text = "&New"; 50:    m_pFileNewMenu->add_Click( 51:        new EventHandler( this, FileNew_Click ) ); 52: 53:    // Separator1 54:    m_pFileSeparator1->Index = 1; 55:    m_pFileSeparator1->Text = "-"; 56: 57:    // FilePageSetupMenu 58:    m_pFilePageSetupMenu->Index = 2; 59:    m_pFilePageSetupMenu->Text = "Page &Setup..."; 60:    m_pFilePageSetupMenu->add_Click( 61:        new EventHandler( this, FilePageSetup_Click ) ); 62: 63:    // FilePrintMenu 64:    m_pFilePrintMenu->Index = 3; 65:    m_pFilePrintMenu->Text = "&Print..."; 66:    m_pFilePrintMenu->add_Click( 67:        new EventHandler( this, FilePrint_Click ) ); 68: 69:    // FilePrintPreviewMenu 70:    m_pFilePrintPreviewMenu->Index = 4; 71:    m_pFilePrintPreviewMenu->Text = "Print Pre&view..."; 72:    m_pFilePrintPreviewMenu->add_Click( 73:        new EventHandler( this, FilePrintPreview_Click ) ); 74: 75:    // Separator2 76:    m_pFileSeparator2->Index = 5; 77:    m_pFileSeparator2->Text = "-"; 78: 79:    // FileExitMenu 80:    m_pFileExitMenu->Index = 6; 81:    m_pFileExitMenu->Text = "E&xit"; 82    m_pFileExitMenu->add_Click( 83:        new EventHandler( this, FileExit_Click ) ); 84: 85:    // Window and Exit menu continued... 86: } 

Finally, a new toolbar button needs to be added for printing. This is done by adding a new toolbar button as well as a new image to the associated toolbar image list in the CreateToolbar() method. On line 5 of Listing 10.3, create a new ToolBarButton and assign it to your m_pFilePrint member variable. This member variable should be declared in your class declaration as a protected member type. The next step is to add the button to the main toolbar. This can be seen on line 12, which adds the toolbar button to the main toolbar's Buttons collection. The next step is to define which image the toolbar button is going to use and what the tooltip is when the user hovers his mouse over it. These lines were added starting at line 29. The last step is to add the printer icon image to the image list that is associated with the toolbar. The source code for this hour, which can be downloaded from the Sams Web site, contains the two icons you need. The icons for this application are 8-bit (256 color) icons that are 16 pixels wide and 16 pixels high. The code to add the icon to the image list starts on line 33 of Listing 10.3.

Listing 10.3 Modifications to the CreateToolbar() Method to Add the Print Toolbar Button
 1: void MDIWindowFrame::CreateToolbar()  2: {  3:    m_pMainToolbar   = new ToolBar;  4:    m_pFileNewButton = new ToolBarButton;  5:    m_pFilePrintButton = new ToolBarButton;  6:    m_pToolbarImages = new ImageList();  7:  8:    // MainToolbar  9:    m_pMainToolbar->Appearance = ToolBarAppearance::Flat; 10:    m_pMainToolbar->AutoSize = false; 11:    m_pMainToolbar->Buttons->Add(m_pFileNewButton); 12:    m_pMainToolbar->Buttons->Add(m_pFilePrintButton); 13: 14:    m_pMainToolbar->DropDownArrows = true; 15:    m_pMainToolbar->ImageList = m_pToolbarImages; 16:    m_pMainToolbar->Name = "MainToolbar"; 17:    m_pMainToolbar->ShowToolTips = true; 18:    m_pMainToolbar->Size = System::Drawing::Size(292, 25); 19:    m_pMainToolbar->TabIndex = 1; 20: 21:    m_pMainToolbar->add_ButtonClick( 22:    new ToolBarButtonClickEventHandler( this, OnToolbarButtonClick)); 23: 24:    // FileNewButton 25:    m_pFileNewButton->ImageIndex  = 0; 26:    m_pFileNewButton->ToolTipText = "Create new MDI Child"; 27: 28:    // FilePrintButton 29:    m_pFilePrintButton->ImageIndex  = 1; 30:    m_pFilePrintButton->ToolTipText = "Print"; 31: 32:    // ToolbarImages 33:    m_pToolbarImages->ColorDepth = ColorDepth::Depth8Bit; 34:    m_pToolbarImages->ImageSize = System::Drawing::Size(16, 16); 35:    m_pToolbarImages->Images->Add( 36:        new System::Drawing::Icon( "FileNew.ico" ) ); 37: 38:    m_pToolbarImages->Images->Add( 39:        new System::Drawing::Icon( "FilePrint.ico" ) ); 40: 41:    m_pToolbarImages->TransparentColor = 42:        System::Drawing::Color::Transparent; 43: } 

Handling the new Print toolbar and menu option is done with the FilePrint_Click() method and the modifications to the OnToolbarButtonClick() method shown in Listing 10.4. You may have noticed a different layout to OnToolbarButtonClick than the one that was presented in Hour 7. Because you are starting to add more buttons to the toolbar, you would have to create several if statements to accommodate them all. This hour, you'll consolidate the if statements into a switch statement for easier readability. Both methods call the PrintForm() method, which is where the printing logic is implemented. The Print Preview and Page Setup menu items are handled with the FilePrintPreview_Click() and FilePageSetup_Click() methods, respectively, which are defined later in the hour.

Listing 10.4 The OnToolbarButtonClick() and FilePrint_Click() Method Implementations
 1: void MDIWindowFrame::OnToolbarButtonClick  2: ( Object* pToolbar, ToolBarButtonClickEventArgs* pArgs )  3: {  4:    //Check to see which button was pressed  5:    switch( m_pMainToolbar->Buttons->IndexOf(pArgs->Button) )  6:    {  7:    case 0: // File New  8:        {  9:            MDIChildForm* pChild = new MDIChildForm; 10:            pChild->MdiParent = this; 11:            pChild->Show(); 12:        } 13:        break; 14: 15:    case 1: // File Print 16:        { 17:            PrintForm( dynamic_cast<MDIChildForm*>(get_ActiveMdiChild()) ); 18:        } 19:        break; 20: 21:    default: 22:        break; 23:    } 24: } 25: void MDIWindowFrame::FilePrint_Click ( Object* pSender, EventArgs* pArgs ) 26: { 27:    PrintForm( dynamic_cast<MDIChildForm*>(get_ActiveMdiChild()) ); 28: } 

The PrintForm() method is called with the current active MDI child window pointer, which is cast to the MDIChildForm class type. The reason a dynamic_cast is used in this case is due to it being an upcast. In other words, get_ActiveMdiChild returns a pointer to a Form object, which the MDIChildForm class derives from. So, to cast from a base class to a derived class, use the dynamic_cast operator. With a pointer to the MDI child form, the printing logic is able to retrieve the contents of the window to produce the printed text.

You may want to compile your application now to make sure everything is working correctly. The new Print menu functions and Print toolbar button, of course, do not currently do anything, but the goal here is to make sure they show up correctly.

graphics/bookpencil.gif

The first thing you should do before implementing the event handler is to first make sure it is wired correctly and receiving events. One common technique for debuggin event handling is the so-called MessageBox method. Within the event handler functions, you can use the MessageBox function to display a quick message. Here's an example of calling the MessageBox function:

 MessageBox( NULL, "In EventHandlerFunction", "My App", MB_OK ); 

   
Top


Sams Teach Yourself Visual C++. NET in 24 Hours
Sams Teach Yourself Visual C++.NET in 24 Hours
ISBN: 0672323230
EAN: 2147483647
Year: 2002
Pages: 237

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net