Microsoft Visual C++ .NET Professional Projects - Premier Press
.pdf
362Project 2 CREATING AN APPLICATION USING MANAGED C++
MainMenu(MenuItem *[]). This constructor initializes a main menu option with the contents of a MenuItem array. Consider the following code snippet:
//MainMenu
Windows::Forms::MenuItem *rMainItems[] = new Windows::Forms::MenuItem *[7]; rMainItems[0] = pFileMenuItem;
rMainItems[1] = pEditMenuItem; rMainItems[2] = pPenMenuItem; rMainItems[3] = pViewMenuItem; rMainItems[4] = pDrawMenuItem; rMainItems[5] = pWindowMenuItem; rMainItems[6] = pHelpMenuItem;
By now, you have a fair idea about how you can create menu items. Now, look at the code to create the menu for the Easy Tool application, which has the menu options File, Edit, Pen, View, Draw, Window, and Help:
//File Menu options
pNewMenuItem = new Windows::Forms::MenuItem(S”&New”, new EventHandler(this, &CMainWindow::MenuItemHandler));
pOpenMenuItem = new Windows::Forms::MenuItem(S”&Open”, new EventHandler(this, &CMainWindow::MenuItemHandler));
pCloseMenuItem = new Windows::Forms::MenuItem(S”&Close”, new EventHandler(this, &CMainWindow::MenuItemHandler));
pSaveMenuItem = new Windows::Forms::MenuItem(S”&Save”, new EventHandler(this, &CMainWindow::MenuItemHandler));
pSaveAsMenuItem = new Windows::Forms::MenuItem(S”Save &As”, new
EventHandler(this, &CMainWindow::MenuItemHandler)); pLine1MenuItem = new Windows::Forms::MenuItem(S”-”);
pPrintMenuItem = new Windows::Forms::MenuItem(S”&Print”,new EventHandler(this, &CMainWindow::MenuItemHandler));
pPrintPreviewMenuItem = new Windows::Forms::MenuItem(S”P&rintPreview”,new EventHandler(this, &CMainWindow::MenuItemHandler));
pLine2MenuItem = new Windows::Forms::MenuItem(S”-”); pLine3MenuItem = new Windows::Forms::MenuItem(S”-”);
pExitMenuItem = new Windows::Forms::MenuItem(S”E&xit”,new EventHandler(this, &CMainWindow::MenuItemHandler));
Windows::Forms::MenuItem *rFileItems[] = new Windows::Forms::MenuItem *[10];
364 Project 2 CREATING AN APPLICATION USING MANAGED C++
EventHandler(this, &CMainWindow::MenuItemHandler)); pStatusbarMenuItem->Checked = true;
Windows::Forms::MenuItem *rviewItems[] = new Windows::Forms::MenuItem *[2];
rviewItems[0] = pToolbarMenuItem; rviewItems= pStatusbarMenuItem;
pViewMenuItem = new Windows::Forms::MenuItem(S”View”, rviewItems);
pDrawLine = new Windows::Forms::MenuItem(S”Line”,new EventHandler(this, &CMainWindow::MenuItemHandler));
pDrawEllipse = new Windows::Forms::MenuItem(S”Ellipse”,new EventHandler(this, &CMainWindow::MenuItemHandler));
pDrawRect = new Windows::Forms::MenuItem(S”Rectangle”,new EventHandler(this, &CMainWindow::MenuItemHandler));
pDrawText = new Windows::Forms::MenuItem(S”Text”,new EventHandler(this, &CMainWindow::MenuItemHandler));
pDrawFilledEllipse = new Windows::Forms::MenuItem(S”Filled Ellipse”,new EventHandler(this, &CMainWindow::MenuItemHandler));
pDrawFilledRect = new Windows::Forms::MenuItem(S”Filled Rect”,new EventHandler(this, &CMainWindow::MenuItemHandler));
pDelegateHandler = new Windows::Forms::MenuItem(S”Calculator”,new EventHandler(this, &CMainWindow::MenuItemHandler));
Windows::Forms::MenuItem *rDrawItems[] = new Windows::Forms::MenuItem *[7]; rDrawItems[0] = pDrawLine;
rDrawItems[1] = pDrawEllipse; rDrawItems[2] = pDrawRect; rDrawItems[3] = pDrawText; rDrawItems[4] = pDrawFilledEllipse; rDrawItems[5] = pDrawFilledRect; rDrawItems[6] = pDelegateHandler;
pDrawMenuItem = new Windows::Forms::MenuItem(S”Draw”, rDrawItems);
//Windows
pNewWindowMenuItem = new Windows::Forms::MenuItem(S”New Window”,new EventHandler(this, &CMainWindow::MenuItemHandler));
366 Project 2 CREATING AN APPLICATION USING MANAGED C++
Having designed the menu for the application, the next step is to design the toolbar and status bar for the application, as discussed in the following section.
Creating the Toolbar and Status Bar for the Application
A status bar is a control that displays a set of text output panes or indicators. These output panes are used as status indicators. The status bar can be used to display the status of keys such as the Scroll and Num Lock keys. It is typically docked at the bottom of the screen. The StatusBar class is derived from the Control class and is used to create a status bar programmatically in an application. Some of the important members of this class are the following:
StatusBar(). The default constructor that creates the status bar.
Size(). Sets the size of the control.
TabIndex(). Sets the tab order within the control.
Text(). Sets the text to be displayed in the status bar pane.
The following code snippet adds a status bar to the Easy Tool application:
//Status bar
pStatusBar = new Windows::Forms::StatusBar();
pStatusBar->TabIndex = 2;
pStatusBar->Text = “For Help, press F1”;
The toolbar is a control that displays a row of bitmapped buttons that activate commands. It is a quick and easy way of activating menu options. Selecting a toolbar option is similar to selecting a menu option. The following are the steps to programmatically add a toolbar to the application:
1.Create an object of the ToolBar class.
2.Create an image list that will hold all the bitmap images that need to be associated with the various toolbar buttons.
3.Create an instance of the ToolBarButton class for each toolbar button that needs to be added.
4.Associate the images in the image list with the ToolBarButton objects and assign tooltips if required.
In the following section, I will discuss the important classes that are involved in the creation of the toolbar.
368 Project 2 CREATING AN APPLICATION USING MANAGED C++
pHelpTBButton->ToolTipText = “Help”;
pPrintTBButton = new Windows::Forms::ToolBarButton(); pPrintTBButton->ImageIndex = 4; pPrintTBButton->ToolTipText = “Print”;
pPreviewTBButton = new Windows::Forms::ToolBarButton(); pPreviewTBButton->ImageIndex = 3; pPreviewTBButton->ToolTipText = “Print Preview”;
pSaveTBButton = new Windows::Forms::ToolBarButton(); pSaveTBButton->ImageIndex = 2; pSaveTBButton->ToolTipText = “Save”;
pOpenTBButton = new Windows::Forms::ToolBarButton(); pOpenTBButton->ImageIndex = 1; pOpenTBButton->ToolTipText = “Open”;
pNewTBButton = new Windows::Forms::ToolBarButton(); pNewTBButton->ImageIndex = 0; pNewTBButton->ToolTipText = “New”;
pImageList = new Windows::Forms::ImageList(); pImageList->ImageSize = System::Drawing::Size(16, 16);
pImageList->TransparentColor = System::Drawing::Color::Transparent; pImageList->Images->Add(Image::FromFile(S”Res\\new.bmp”)); pImageList->Images->Add(Image::FromFile(S”Res\\open.bmp”)); pImageList->Images->Add(Image::FromFile(S”Res\\save.bmp”)); pImageList->Images->Add(Image::FromFile(S”Res\\preview.bmp”)); pImageList->Images->Add(Image::FromFile(S”Res\\print.bmp”)); pImageList->Images->Add(Image::FromFile(S”Res\\help.bmp”));
ToolBar Class
The ToolBar class is used to display toolbar buttons. Some of the important methods and properties of this class are listed here:
ToolBar(). The default constructor of the class.
CREATING A MANAGED EXTENSIONS APPLICATION Chapter 12 369
ImageList property. Specifies the name of the image list associated with the toolbar.
ShowToolTips property. Specifies whether the toolbar displays tooltips.
Buttons property. Retrieves the toolbar button controls associated with the toolbar.
The following code snippet creates the toolbar for the Easy Tool application and associates an image list with the required images:
pToolBar = new Windows::Forms::ToolBar(); pToolBar->ImageList = pImageList; pToolBar->DropDownArrows = true; pToolBar->ShowToolTips = true;
System::Windows::Forms::ToolBarButton* rToolbarButtons[]=new System::Windows::Forms::ToolBarButton*[6]; rToolbarButtons[0]=pNewTBButton; rToolbarButtons[1]=pOpenTBButton; rToolbarButtons[2]=pSaveTBButton; rToolbarButtons[3]=pPreviewTBButton; rToolbarButtons[4]=pPrintTBButton; rToolbarButtons[5]=pHelpTBButton;
pToolBar->Buttons->AddRange(rToolbarButtons); pToolBar->ButtonClick += new
System::Windows::Forms::ToolBarButtonClickEventHandler(this,&CMainWindow::Toolbar-
Handler);
The previous sections of this chapter discussed the creation of the various elements of the user interface. I will now move on to discuss the lifeblood of this application
— the code behind the working of the application. The following sections discuss two important aspects of the application: event handling and delegates.
Event Handling in the Application
You are aware that the functionality of event handling in the .NET Framework revolves around an event delegate and its handler. You require a class to hold event data and a delegate to point to the event handler. The EventHandler delegate represents the method to be used to handle events that have no data.
