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

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

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

420 Project 2

CREATING AN APPLICATION USING MANAGED C++

 

 

__event MyDelegate *E;

 

 

 

 

 

void Fire_E(int n1, int n2)

 

 

{

 

 

 

 

 

 

 

 

E(n1, n2);

 

 

 

 

}

 

 

 

 

 

};

 

 

 

 

 

Y

 

 

 

 

 

 

 

 

// class to hold event handlers, the event receiver

 

public __gc struct EventReceiver

 

L

{

 

 

 

 

F

 

 

void Add(int num1, int num2)

 

 

{

 

 

M

 

}

String* pStr;

A

 

 

 

 

pStr = String::Format(“ {0} + {1} = {2}”, Convert::ToString(num1),

 

 

 

E

 

 

 

Convert::ToString(num2), Convert::ToString(num1+num2));

 

 

 

T

 

 

 

 

 

 

MessageBox::Show(pStr, “ ddition”);

void Subtract(int num1, int num2)

{

String* pStr;

pStr = String::Format(“ {0} - {1} = {2}”, Convert::ToString(num1), Convert::ToString(num2), Convert::ToString(num1-num2));

MessageBox::Show(pStr, “Subtraction”);

}

void Multiply(int num1, int num2)

{

String* pStr;

pStr = String::Format(“ {0} * {1} = {2}”, Convert::ToString(num1), Convert::ToString(num2), Convert::ToString(num1*num2));

MessageBox::Show(pStr, “Multiplication”);

}

void Divide(int num1, int num2)

{

if (num2 == 0)

{

MessageBox::Show(“Divide by Zero error”, “Division”); return;

}

Team-Fly®

CREATING A MANAGED EXTENSIONS APPLICATION Chapter 12 421

String* pStr;

pStr = String::Format(“ {0} / {1} = {2} (Rounded)”, Convert::ToString(num1), Convert::ToString(num2), Convert::ToString(num1/num2));

MessageBox::Show(pStr, “Division”);

}

};

void CMainWindow::HandleDelegate()

{

int nOp = 0; int nNum1 = 0; int nNum2 = 0; String* pOp;

//Simple calculation form

Form* f = new Form();

//Get the document of active view

CGDIPlusView* pActiveView = dynamic_cast<CGDIPlusView*> (this- >ActiveMdiChild);

CGDIPlusDoc* pActiveDoc = pActiveView->GetDocument();

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

f->Text = “Simple Calculation”;

f->ClientSize = System::Drawing::Size(200, 125);

ComboBox* cmbBox1 = new ComboBox(); cmbBox1->Location = System::Drawing::Point(70, 40); cmbBox1->Size = System::Drawing::Size(30, 20); cmbBox1->TabIndex = 2;

cmbBox1->set_DropDownStyle(ComboBoxStyle::DropDownList); //Add existing operations cmbBox1->Items->Add(Convert::ToString(“+”)); cmbBox1->Items->Add(Convert::ToString(“-”)); cmbBox1->Items->Add(Convert::ToString(“*”));

422 Project 2 CREATING AN APPLICATION USING MANAGED C++

cmbBox1->Items->Add(Convert::ToString(“/”));

Label* label1 = new Label();

label1->Location = System::Drawing::Point(15, 20); label1->Text = “Num1:”;

label1->Size = System::Drawing::Size(40, 16);

Label* label2 = new Label();

label2->Location = System::Drawing::Point(15, 48); label2->Text = “Operation:”;

label2->Size = System::Drawing::Size(56, 16);

Label* label3 = new Label();

label3->Location = System::Drawing::Point(15, 75); label3->Text = “Num2:”;

label3->Size = System::Drawing::Size(40, 16);

Button* button1 = new Button();

button1->Location = System::Drawing::Point(35, 100); button1->Size = System::Drawing::Size(60, 20); button1->TabIndex = 4;

button1->Text = “OK”;

button1->DialogResult = System::Windows::Forms::DialogResult::OK ; //Make this “OK” button

Button* button2 = new Button();

button2->Location = System::Drawing::Point(105, 100); button2->Size = System::Drawing::Size(60, 20); button2->TabIndex = 5;

button2->Text = “Cancel”;

TextBox* textBox1 = new TextBox();

textBox1->Location = System::Drawing::Point(70, 15); textBox1->TabIndex = 1;

textBox1->Size = System::Drawing::Size(64, 20);

TextBox* textBox2 = new TextBox();

textBox2->Location = System::Drawing::Point(70, 65);

CREATING A MANAGED EXTENSIONS APPLICATION Chapter 12 423

textBox2->TabIndex = 3;

textBox2->Size = System::Drawing::Size(64, 20);

f->FormBorderStyle = FormBorderStyle::FixedDialog; f->MaximizeBox = false;

f->MinimizeBox = false; f->AcceptButton = button1; f->CancelButton = button2;

f->StartPosition = FormStartPosition::CenterScreen;

f->Controls->Add(button1); f->Controls->Add(button2); f->Controls->Add(cmbBox1); f->Controls->Add(textBox1); f->Controls->Add(textBox2); f->Controls->Add(label1); f->Controls->Add(label2); f->Controls->Add(label3);

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

{

String *pStr1, *pStr2; pStr1 = textBox1->Text; pStr1 = pStr1->Trim(); pStr2 = textBox2->Text; pStr2 = pStr2->Trim(); //Check for Empty values

if (pStr1 == “” || pStr2 == “”)

{

MessageBox::Show(“Operands missing...”, “Message”); return;

}

//Convert Strings to Integers nNum1 = Convert::ToInt16(pStr1); nNum2 = Convert::ToInt16(pStr2);

424 Project 2 CREATING AN APPLICATION USING MANAGED C++

//Get the operand from the Combo box pOp = cmbBox1->Text;

//Check which operator (+,-,*,/) user has chosen if (pOp->CompareTo(“+”) == 0)

nOp=1;

else if (pOp->CompareTo(“-”) == 0) nOp=2;

else if (pOp->CompareTo(“*”) == 0) nOp=3;

else if (pOp->CompareTo(“/”) == 0) nOp=4;

f->Close();

}

EventSource* pE = new EventSource;

EventReceiver* pR = new EventReceiver;

//Add event handlers if (nOp == 1)

pE->E += new MyDelegate(pR, &EventReceiver::Add); else if (nOp == 2)

pE->E += new MyDelegate(pR, &EventReceiver::Subtract); else if (nOp == 3)

pE->E += new MyDelegate(pR, &EventReceiver::Multiply); else if (nOp == 4)

pE->E += new MyDelegate(pR, &EventReceiver::Divide);

//Raise events

pE->Fire_E(nNum1, nNum2);

}

//Entry point void main()

{

Application::Run(new CMainWindow());

}

CREATING A MANAGED EXTENSIONS APPLICATION Chapter 12 425

//Document Header File

#ifndef __GDIPLUSDOC_H__ #define __GDIPLUSDOC_H__

#using <mscorlib.dll>

#using <System.DLL>

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

#using <System.Runtime.Remoting.DLL>

using namespace System;

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

using namespace System::Drawing;

using namespace System::Drawing::Printing; using namespace System::Collections;

using namespace System::Runtime::Serialization;

using namespace System::Runtime::Serialization::Formatters::Binary ;

//Forward declaration of GDIPlusView to keep compiler happy

__gc class CGDIPlusView;

[Serializable] __gc class CStroke

{

public:

int nPenWidth;

ArrayList* pPointArray;

Rectangle rectBoundingRect;

CStroke(int nPenWidth1); ~CStroke();

Rectangle GetBoundingRectangle() {return rectBoundingRect;}

void DrawStroke(Graphics* g);

426 Project 2 CREATING AN APPLICATION USING MANAGED C++

void FinishStroke();

int Min(int x,int y) {return (x < y) ? x : y;} int Max(int x,int y) {return (x > y) ? x : y ;}

};

//Document class

__gc class CGDIPlusDoc

{

public:

CGDIPlusDoc(CMainWindow* pMainWindow); ~CGDIPlusDoc();

void SaveDocument(String* pFileName); void OpenDocument(String* pFileName);

Pen* GetCurrentPen(); Brush* GetCurBrush(); void ReplacePen();

void UpdateAllViews(CGDIPlusView* pView, CStroke* newStroke); void DeleteContents();

CStroke* NewStroke();

ArrayList* strokeList ;

ArrayList* viewList;

bool bIsDirty;

int nDocID;

protected:

UInt32 nPenWidth;

Pen* pCurrentPen;

SolidBrush* pCurrentBrush;

public:

CREATING A MANAGED EXTENSIONS APPLICATION Chapter 12 427

bool bIsThickPen; UInt32 nThinWidth; UInt32 nThickWidth;

//For drawing Ellipse & Rectangle UInt16 nShapeLeft;

UInt16 nShapeTop;

UInt16 nShapeWidth;

UInt16 nShapeHeight;

};

#endif __GDIPLUSDOC_H__

//Document .cpp file

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

//CStroke

CStroke:: CStroke(int nPenWidth1)

{

nPenWidth = nPenWidth1; pPointArray = new ArrayList();

}

CStroke::~CStroke()

{

}

void CStroke::DrawStroke(Graphics* g)

{

Pen* pPen = new Pen(Color::Black, (float)nPenWidth); for(int i=1; i < pPointArray->Count; i++)

{

__box Point* p1 = dynamic_cast<__box Point*> (pPointArray->Item[i-1]); __box Point* p2 = dynamic_cast<__box Point*> (pPointArray->Item[i]);

428 Project 2 CREATING AN APPLICATION USING MANAGED C++

g->DrawLine(pPen, *p1, *p2);

}

pPen->Dispose();

}

void CStroke::FinishStroke()

{

if(pPointArray->Count == 0)

{

rectBoundingRect.Size = Size::Empty; return;

}

__box Point* pt = dynamic_cast<__box Point*> (pPointArray->Item[0]); rectBoundingRect = Rectangle (pt->X, pt->Y, 0, 0);

for(int i=1; i < pPointArray->Count; i++)

{

pt = dynamic_cast<__box Point*> (pPointArray->Item[i]); rectBoundingRect = Rectangle::FromLTRB(Min(rectBoundingRect.Left, pt-

>X),Min(rectBoundingRect.Top, pt->Y),Max(rectBoundingRect.Right, pt->X) , Max(rectBoundingRect.Bottom, pt->Y));

}

rectBoundingRect.Inflate(Size((int)nPenWidth, (int)nPenWidth)); return;

}

//CGDIPlusDoc

CGDIPlusDoc:: CGDIPlusDoc(CMainWindow* pMainWin)

{

bIsDirty= false; bIsThickPen=false; nThinWidth=3; nThickWidth=7;

strokeList = new ArrayList() ; viewList = new ArrayList();

pCurrentBrush = new SolidBrush(Color::Blue); ReplacePen();

CREATING A MANAGED EXTENSIONS APPLICATION Chapter 12 429

//Defaults nShapeLeft = 10; nShapeTop = 10; nShapeWidth = 50; nShapeHeight = 25;

//Set the ID to be the current count + 1. This is so that the ID starts

with 1

nDocID = CMainWindow::nDocCount+1; //Create a view (Form) for this document

CGDIPlusView* pView = new CGDIPlusView(this, pMainWin); viewList->Add(pView);

pView->Show();

}

CGDIPlusDoc::~CGDIPlusDoc()

{

}

CStroke* CGDIPlusDoc:: NewStroke()

{

CStroke* s = new CStroke(nPenWidth); strokeList->Add(s);

bIsDirty = true; //Now that the doc is modified, set the dirty flag return s;

}

void CGDIPlusDoc::ReplacePen()

{

nPenWidth = bIsThickPen ? nThickWidth : nThinWidth; pCurrentPen = new Pen(Color::Black , (float)nPenWidth);

}

void CGDIPlusDoc::SaveDocument(String* fileName)

{

try

{