Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

Microsoft Visual C++ .NET Professional Projects - Premier Press

.pdf
Скачиваний:
180
Добавлен:
24.05.2014
Размер:
26 Мб
Скачать

360

Project 2

CREATING AN APPLICATION USING MANAGED C++

 

f->StartPosition = FormStartPosition::CenterScreen;

 

f->Controls->Add(button1);

 

 

 

f->Controls->Add(button2);

 

 

 

f->Controls->Add(label1);

 

 

 

f->Controls->Add(label2);

 

 

 

f->Controls->Add(textBox1);

 

Y

 

f->Controls->Add(textBox2);

 

 

L

 

 

 

 

System::Windows::Forms::DialogResult res = f->ShowDialog();

 

 

 

F

 

if(res == System::Windows::Forms::DialogResult::OK )

 

{

 

M

 

 

}

 

A

 

 

 

pActiveDoc->nThinWidth = UInt32::Parse(textBox1->Text);

 

 

pActiveDoc->nThickWidth = UInt32::Parse(textBox2->Text);

 

 

E

 

pActiveDoc->ReplacePen(); f->Close();T

Having discussed the user interface for the application, I will now discuss another important aspect of the application — the menu.

Creating the Menu for the Application

Since the Easy Tool application is a Managed C++ project, we need to programmatically add all the menu items discussed in the previous section. The following are the steps to add the menu items programmatically:

1.Create an instance of the MenuItem class. This class is used to represent each element that is displayed in the main menu or context menu.

2.Add all the elements that are part of the main menu options.

3.Add all the MenuItem instances to an instance of the MainMenu class. The MainMenu class defines the structure of the menu for a form.

Let me first discuss the MenuItem class and how you can use it to create the menu items.

MenuItem Class

The MenuItem class contains all the elements that are part of a menu option. The constructor of the MenuItem class is overloaded and has a number of variations.

Team-Fly®

CREATING A MANAGED EXTENSIONS APPLICATION Chapter 12 361

The following are the important ones:

MenuItem(). The default constructor.

MenuItem(String *str). The parameter str represents the caption of the menu option. Consider the following example:

pFileMenuItem = new Windows::Forms::MenuItem(“File”);

MenuItem(String *str, EventHandler *ev). The first parameter of the constructor is the caption of the menu item, and the second parameter is the event handler that will be called when the Click event is generated. Consider the following example:

pPenMenuItem = new Windows::Forms::MenuItem(S”Pen”, rPenItems);

MenuItem(String *str, MenuItem[]). The first parameter of the constructor is the caption of the menu item, and the second parameter is an array that contains the subitems that are part of this menu option. Consider the following example:

Windows::Forms::MenuItem *rFileItems[] = new Windows::Forms::MenuItem *[10]; rFileItems[0] = pNewMenuItem;

rFileItems[1] = pOpenMenuItem; rFileItems[2] = pCloseMenuItem; rFileItems[3] = pSaveMenuItem; rFileItems[4] = pSaveAsMenuItem; rFileItems[5] = pLine1MenuItem; rFileItems[6] = pPrintMenuItem; rFileItems[7] = pPrintPreviewMenuItem; rFileItems[8] = pLine2MenuItem; rFileItems[9] = pExitMenuItem;

pFileMenuItem = new Windows::Forms::MenuItem(S”&File”, rFileItems);

After you have created the elements of the menu, the next important step is to add these elements to the main menu. You can do this using the MainMenu class.

MainMenu Class

The MainMenu class is the parent for all the submenu options in your application. This class has two constructors of importance:

MainMenu(). The default constructor of the class.

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

CREATING A MANAGED EXTENSIONS APPLICATION Chapter 12 363

rFileItems[0] = pNewMenuItem; rFileItems[1] = pOpenMenuItem; rFileItems[2] = pCloseMenuItem; rFileItems[3] = pSaveMenuItem; rFileItems[4] = pSaveAsMenuItem; rFileItems[5] = pLine1MenuItem; rFileItems[6] = pPrintMenuItem; rFileItems[7] = pPrintPreviewMenuItem; rFileItems[8] = pLine2MenuItem; rFileItems[9] = pExitMenuItem;

pFileMenuItem = new Windows::Forms::MenuItem(S”&File”, rFileItems); pClearAllMenuItem = new Windows::Forms::MenuItem(S”Clear &All”,new EventHandler(this, &CMainWindow::MenuItemHandler));

Windows::Forms::MenuItem *rEditItems[] = new Windows::Forms::MenuItem *; rEditItems[0] = pClearAllMenuItem;

pEditMenuItem = new Windows::Forms::MenuItem(S”&Edit”, rEditItems);

//Pen

pThickLineMenuItem = new Windows::Forms::MenuItem(S”Thick Line”,new EventHandler(this, &CMainWindow::MenuItemHandler));

pPenWidthMenuItem = new Windows::Forms::MenuItem(S”Pen Width”,new EventHandler(this, &CMainWindow::MenuItemHandler));

Windows::Forms::MenuItem *rPenItems[] = new Windows::Forms::MenuItem *[2]; rPenItems[0] = pThickLineMenuItem; rPenItems= pPenWidthMenuItem;

pPenMenuItem = new Windows::Forms::MenuItem(S”Pen”, rPenItems);

//View

pToolbarMenuItem = new Windows::Forms::MenuItem(S”Toolbar”,new EventHandler(this, &CMainWindow::MenuItemHandler));

pToolbarMenuItem->Checked = true;

pStatusbarMenuItem = new Windows::Forms::MenuItem(S”Statusbar”,new

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

CREATING A MANAGED EXTENSIONS APPLICATION Chapter 12 365

pCascadeMenuItem = new Windows::Forms::MenuItem(S”Cascade”,new EventHandler(this, &CMainWindow::MenuItemHandler));

pTileMenuItem = new Windows::Forms::MenuItem(S”Tile”,new EventHandler(this, &CMainWindow::MenuItemHandler));

Windows::Forms::MenuItem *rwindowItems[] = new Windows::Forms::MenuItem *[3]; rwindowItems[0] = pNewWindowMenuItem; rwindowItems[1] = pCascadeMenuItem; rwindowItems[2] = pTileMenuItem;

pWindowMenuItem = new Windows::Forms::MenuItem(S”Window”, rwindowItems);

//Help

pHelpTopicsMenuItem = new Windows::Forms::MenuItem(S”Help Topics”,new EventHandler(this, &CMainWindow::MenuItemHandler));

pHelpAboutMenuItem = new Windows::Forms::MenuItem(S”About GDIPlus”,new EventHandler(this, &CMainWindow::MenuItemHandler));

Windows::Forms::MenuItem *rhelpItems[] = new Windows::Forms::MenuItem *[2];

rhelpItems[0] = pHelpTopicsMenuItem; rhelpItems[1] = pHelpAboutMenuItem;

pHelpMenuItem = new Windows::Forms::MenuItem(S”Help”, rhelpItems);

//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; pMainMenu = new MainMenu(rMainItems); Menu = pMainMenu;

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.

CREATING A MANAGED EXTENSIONS APPLICATION Chapter 12 367

ImageList Class

The ImageList class is used to store a collection of images of the same size. These images can be icons or bitmaps and can be created using any editor. The following are some of the important functions of this class:

ImageList(). The default constructor for the class; used to initialize an image list object.

Add(Path). Adds an icon or bitmap to the image list.

Size(x,y). Sets the dimensions of the bitmaps or icons in the image list.

The following code snippet adds a toolbar to the Easy Tool application:

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

After adding the bitmaps and icons to the image list, the next step is to associate these images with the corresponding toolbar buttons. This is done using the Tool-

BarButton class.

ToolBarButton Class

The ToolBarButton class encapsulates the functionality of the toolbar buttons. Some of the important methods and properties of this class are the following:

ToolBarButton(). The default constructor for the class.

ImageIndex property. Sets the index value of the image in the image list that is associated with this button.

ToolTipText property. Sets the tooltip text that is displayed for the toolbar button.

The following code snippet creates the toolbar buttons for the Easy Tool application:

pHelpTBButton = new Windows::Forms::ToolBarButton();

pHelpTBButton->ImageIndex = 5;

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.