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

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

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

380Project 2 CREATING AN APPLICATION USING MANAGED C++

View header and CPP files

Document header and CPP files

You are already aware of the workings and mechanics of a Document/View application, and therefore I will not get into the details of the same. You will find the code for these files in the following pages of this chapter. Before you look at those files, the following is an overview of the application and how it works:

1.

 

 

 

Y

The entry point of the application is the main() method. This method in

 

 

 

L

 

turn calls the Run method and creates an object of the CMainWindow class.

2.

 

 

F

The CMainWindow class is in turn derived from the Form class.

3.

 

 

M

 

The constructor of the CMainWindow class calls the InitializeComponent

 

method. This function creates and displays the user interface elements.

4.

 

A

 

The CreateDocument() function is called, which creates a new docu-

 

ment.

 

 

 

5.

Based on the menu option selected by the user, the appropriate menu

 

handler is called. Note that when you choose the Save option again, the

 

Document classEplays the role of the archiver.

Listing

 

T

 

 

Listing 12-1 Complete Code for the Easy Tool Application

//Application Header File

#ifndef __GDIPLUS_H__ #define __GDIPLUS_H__

#using <mscorlib.dll>

#using <System.DLL>

#using <System.Drawing.DLL> #using <System.Windows.Forms.DLL>

using namespace System;

using namespace System::ComponentModel; using namespace System::Windows::Forms; using namespace System::Collections; using namespace System::IO;

Team-Fly®

CREATING A MANAGED EXTENSIONS APPLICATION Chapter 12 381

using namespace System::Drawing;

using namespace System::Drawing::Printing;

__gc class CGDIPlusDoc; //Forward declaration

__gc class CMainWindow : public Form

{

public:

CMainWindow();

virtual ~CMainWindow();

static

int nDocCount;

// Keeps track of the opened doc count

static

CMainWindow* pParentWindow;

public:

//Methods

void EnableItems(); void DisableItems();

private:

void InitializeComponent();

void MenuItemHandler(Object* sender, EventArgs* e);

void ToolbarHandler(Object* sender, ToolBarButtonClickEventArgs* e);

void AboutHelp(); void HelpTopics(); void Print();

void PrintPage(Object* sender,PrintPageEventArgs* ev); void PrintPreview();

void Exit();

void CloseView(); void Tile();

void Cascade();

void ClearAll(); //Clear the contents

382 Project 2 CREATING AN APPLICATION USING MANAGED C++

void Open(); void Save(); void New();

void NewWindow();

void ThickLine(); void PenWidthsDlg();

//START PSK

void DrawLine(); void DrawEllipse(); void DrawRect(); void DrawText();

void DrawFilledEllipse(); void DrawFilledRect(); void HandleDelegate();

//END PSK

CGDIPlusDoc* CreateDocument();

private:

System::ComponentModel::Container* pComponents;

PrintDocument* pPrintDoc;

HelpProvider* pHelpProvider;

MenuItem* pFileMenuItem;

MenuItem* pEditMenuItem;

MenuItem* pViewMenuItem;

MenuItem* pPenMenuItem;

MenuItem* pHelpMenuItem;

MenuItem* pWindowMenuItem;

MenuItem* pNewMenuItem;

MenuItem* pOpenMenuItem;

MenuItem* pCloseMenuItem;

MenuItem* pSaveMenuItem;

MenuItem* pSaveAsMenuItem;

MenuItem* pPrintMenuItem;

CREATING A MANAGED EXTENSIONS APPLICATION Chapter 12 383

MenuItem* pPrintPreviewMenuItem;

MenuItem* pExitMenuItem;

MenuItem* pThickLineMenuItem;

MenuItem* pNewWindowMenuItem;

MenuItem* pCascadeMenuItem;

MenuItem* pTileMenuItem;

MenuItem* pClearAllMenuItem;

MenuItem* pToolbarMenuItem;

MenuItem* pStatusbarMenuItem;

MenuItem* pHelpAboutMenuItem;

MenuItem* pHelpTopicsMenuItem;

MenuItem* pPenWidthMenuItem;

//PSK START

MenuItem* pDrawMenuItem;

MenuItem* pDrawLine;

MenuItem* pDrawEllipse;

MenuItem* pDrawRect;

MenuItem* pDrawText;

MenuItem* pDrawFilledEllipse;

MenuItem* pDrawFilledRect;

MenuItem* pDelegateHandler;

//PSK END

MainMenu* pMainMenu;

MenuItem* pLine1MenuItem;

MenuItem* pLine2MenuItem;

MenuItem* pLine3MenuItem;

//Toolbar

ToolBar* pToolBar;

StatusBar* pStatusBar;

ImageList* pImageList;

ToolBarButton* pHelpTBButton;

ToolBarButton* pPrintTBButton;

ToolBarButton* pPreviewTBButton;

ToolBarButton* pSaveTBButton;

ToolBarButton* pOpenTBButton;

384 Project 2 CREATING AN APPLICATION USING MANAGED C++

ToolBarButton* pNewTBButton;

};

#endif __GDIPLUS_H__

//Application .cpp file

#include “GDIPlus.h” #include “GDIPlusView.h”

CMainWindow::CMainWindow()

{

pParentWindow = this;

InitializeComponent();

nDocCount=0;

CreateDocument();

}

CMainWindow::~CMainWindow()

{

//pComponents->Dispose();

}

void CMainWindow::InitializeComponent()

{

pComponents = new System::ComponentModel::Container();

pPrintDoc = new Printing::PrintDocument();

pPrintDoc->PrintPage += new PrintPageEventHandler(this, PrintPage);

AutoScaleBaseSize = System::Drawing::Size(5, 13);

Text = “GDIPlus”;

IsMdiContainer = true;

Menu = pMainMenu;

ClientSize = System::Drawing::Size(600, 400);

pHelpProvider = new System::Windows::Forms::HelpProvider(); pHelpProvider->HelpNamespace = S”Help\\GDIPlus.chm”; pHelpProvider->SetShowHelp(this, true);

CREATING A MANAGED EXTENSIONS APPLICATION Chapter 12 385

//File

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

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

386 Project 2 CREATING AN APPLICATION USING MANAGED C++

EventHandler(this, &CMainWindow::MenuItemHandler));

Windows::Forms::MenuItem *rEditItems[] = new Windows::Forms::MenuItem *[1];

rEditItems[0] = pClearAllMenuItem; //rEditItems[1] = pLine3MenuItem;

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[1] = 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 EventHandler(this, &CMainWindow::MenuItemHandler));

pStatusbarMenuItem->Checked = true; Windows::Forms::MenuItem *rviewItems[] =

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

rviewItems[0] = pToolbarMenuItem; rviewItems[1] = pStatusbarMenuItem;

pViewMenuItem = new Windows::Forms::MenuItem(S”View”, rviewItems);

CREATING A MANAGED EXTENSIONS APPLICATION Chapter 12 387

//START PSK

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); //END PSK

//Windows

pNewWindowMenuItem = new Windows::Forms::MenuItem(S”New Window”,new EventHandler(this, &CMainWindow::MenuItemHandler));

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

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

388 Project 2 CREATING AN APPLICATION USING MANAGED C++

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;

//Toolbar and Status bar

CREATING A MANAGED EXTENSIONS APPLICATION Chapter 12 389

pStatusBar = new Windows::Forms::StatusBar(); pStatusBar->TabIndex = 2;

pStatusBar->Text = “For Help, press F1”;

pHelpTBButton= new Windows::Forms::ToolBarButton(); pHelpTBButton->ImageIndex = 5; 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”));