430 Project 2 CREATING AN APPLICATION USING MANAGED C++
Stream* s = File::Open(fileName, FileMode::Create); BinaryFormatter* b = new BinaryFormatter(); b->Serialize(s, strokeList);
s->Close();
//Now that the doc is saved, its no more dirty bIsDirty = false;
|
} |
|
|
|
|
Y |
|
|
|
|
|
|
|
catch(Exception* ex) |
|
L |
|
{ |
|
|
F |
|
|
MessageBox::Show(ex->ToString()); |
|
|
} |
|
|
M |
|
|
|
|
|
|
|
} |
|
|
E |
|
|
|
|
|
|
|
void CGDIPlusDoc::OpenDocument(String* fileName) |
{ |
|
T |
|
|
|
|
|
A |
|
|
|
try |
|
|
|
|
{ |
|
|
|
|
|
|
|
Stream* s = |
File::Open(fileName, FileMode::Open); |
|
|
BinaryFormatter* b = new BinaryFormatter(); |
|
|
strokeList = dynamic_cast<ArrayList*>(b->Deserialize(s)); |
|
|
s->Close(); |
|
|
|
|
}
catch(Exception* ex)
{
MessageBox::Show(ex->ToString());
}
}
Pen* CGDIPlusDoc::GetCurrentPen()
{
return pCurrentPen;
}
Brush* CGDIPlusDoc::GetCurBrush()
{
return pCurrentBrush;
}
CREATING A MANAGED EXTENSIONS APPLICATION Chapter 12 431
//Updates all the views of the document with the new data
void CGDIPlusDoc::UpdateAllViews(CGDIPlusView* sender, CStroke* newStroke)
{
CGDIPlusView* pView;
for(int i=0; i < viewList->Count ; i++)
{
pView = dynamic_cast<CGDIPlusView*> (viewList->Item[i]);
if(pView->Equals(sender)) continue;
//If this is for a new stroke if(newStroke)
{
pView->Invalidate(newStroke->GetBoundingRectangle());
}
else
{
//must be ClearAll, hence invalidate the entire region pView->Invalidate();
}
pView->Update();
}
}
// Deletes the contents of the document void CGDIPlusDoc::DeleteContents()
{
strokeList->Clear(); UpdateAllViews(0, 0);
}
//View Header File
#ifndef __GDIPLUSVIEW_H__ #define __GDIPLUSVIEW_H__
432 Project 2 CREATING AN APPLICATION USING MANAGED C++
#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;
using namespace System::Drawing;
using namespace System::Drawing::Printing; using namespace System::Collections;
#include “GDIPlusDoc.h”
__gc class CGDIPlusView : public Form
{
///<summary>
///Required designer variable
///</summary>
private:
System::ComponentModel::Container* pComponents;
CGDIPlusDoc* pDoc;
CMainWindow* pMainWin;
public:
Point ptPrevPoint;
CStroke* pCurrentStroke;
CGDIPlusView(CGDIPlusDoc* pDoc1, CMainWindow* pParent); ~CGDIPlusView();
void InitializeComponent();
void MouseDownHandler(Object* sender,MouseEventArgs* e); void MouseMoveHandler(Object* sender,MouseEventArgs* e); void MouseUpHandler(Object* sender,MouseEventArgs* e);
CREATING A MANAGED EXTENSIONS APPLICATION Chapter 12 433
void PaintHandler(Object* sender, PaintEventArgs* e); void ClosingHandler(Object* sender, CancelEventArgs* e); void ClosedHandler(Object* sender, EventArgs* e);
CGDIPlusDoc* GetDocument();
public:
void HandleLineDraw(); void HandleEllipseDraw(); void HandleRectDraw();
void HandleTextDraw(String* pStr, Drawing::Font* pFont, Color colorText); void HandleFilledEllipseDraw();
void HandleFilledRectDraw();
};
#endif __GDIPLUSVIEW_H__
//View .cpp file
#include “GDIPlus.h” #include “GDIPlusView.h” #include “GDIPlusDoc.h”
CGDIPlusView:: CGDIPlusView(CGDIPlusDoc* pDoc1, CMainWindow* pParent)
{
InitializeComponent(); pDoc = pDoc1;
MdiParent = pParent; //PSK pMainWin = pParent;
this->Text = String::Concat(S”GDIPlusDoc”, pDoc->nDocID.ToString(), S”:”, pDoc->viewList->Count.ToString());
}
CGDIPlusView::~CGDIPlusView()
{
}
void CGDIPlusView::InitializeComponent()
{
434 Project 2 CREATING AN APPLICATION USING MANAGED C++
pComponents = new System::ComponentModel::Container();
AutoScaleBaseSize = System::Drawing::Size(5, 13);
Text = “GDIPlusDoc”;
BackColor = Color::White;
MouseDown += new MouseEventHandler(this, MouseDownHandler);
MouseMove += new MouseEventHandler(this, MouseMoveHandler);
MouseUp += new MouseEventHandler(this,MouseUpHandler);
Paint += new PaintEventHandler(this, PaintHandler);
Closing += new CancelEventHandler(this, ClosingHandler);
Closed += new EventHandler(this,ClosedHandler);
}
void CGDIPlusView::MouseDownHandler(Object* sender,MouseEventArgs* e)
{
if(!this->Capture) return;
try
{
Point* p = __nogc new Point(e->X, e->Y);
//Need to box the Point because its a ValueType __box Point* pt = __box(*p);
pCurrentStroke = pDoc->NewStroke(); pCurrentStroke->pPointArray->Add(pt);// Add first point to the new
stroke
ptPrevPoint = *p;
this->Capture = true; // Capture the mouse until button up
}
catch(Exception* ex) { MessageBox::Show(ex->ToString());
}
}
void CGDIPlusView::MouseMoveHandler(Object* sender,MouseEventArgs* e)
{
if(!this->Capture)
CREATING A MANAGED EXTENSIONS APPLICATION Chapter 12 435
return;
try
{
Point* p= __nogc new Point(e->X, e->Y);
//Need to box the Point because its a ValueType __box Point* pt = __box(*p); pCurrentStroke->pPointArray->Add(pt);
Graphics* g = CreateGraphics(); g->DrawLine(pDoc->GetCurrentPen(), ptPrevPoint, *p); ptPrevPoint = *p;
}
catch (Exception* ex) { MessageBox::Show(ex->ToString());
}
}
void CGDIPlusView::MouseUpHandler(Object* sender,MouseEventArgs* e)
{
if(!pCurrentStroke)
return;
try
{
Point* p= __nogc new Point(e->X, e->Y); __box Point* pt = __box(*p); pCurrentStroke->pPointArray->Add(pt); Graphics* g = CreateGraphics();
g->DrawLine(pDoc->GetCurrentPen(), ptPrevPoint, *p);
ptPrevPoint=*p;
//Tell the stroke item that we’re done adding points to it.
//This is so it can finish computing its bounding rectangle. pCurrentStroke->FinishStroke();
//Now that a stoke is added, inform all the views of the document about
this
pDoc->UpdateAllViews(this, pCurrentStroke);
Capture = false;
436 Project 2 CREATING AN APPLICATION USING MANAGED C++
}
catch (Exception* ex) {
MessageBox::Show(ex->ToString());
}
}
void CGDIPlusView::PaintHandler(Object* sender, PaintEventArgs* e)
{
Rectangle rectClip = e->ClipRectangle ; rectClip.Inflate(1, 1);
Rectangle rectStroke;
for(int i=0; i < pDoc->strokeList->Count; i++)
{
CStroke* st = dynamic_cast<CStroke*>(pDoc->strokeList->Item[i]); rectStroke = st->GetBoundingRectangle();
rectStroke.Inflate (1,1); if(!rectStroke.IntersectsWith(rectClip))
continue; st->DrawStroke(e->Graphics) ;
}
}
void CGDIPlusView::ClosingHandler(Object* sender, CancelEventArgs* e)
{
if( pDoc->bIsDirty && (pDoc->viewList->Count == 1) )
{
int nSave = MessageBox::Show(“Do you want to Save changes ?”, “GDIPlus”, MessageBoxButtons::YesNoCancel);
if(nSave == DialogResult::Yes)
{
SaveFileDialog* pSaveDlg = new SaveFileDialog(); pSaveDlg->Filter = “GDIPlus Files (*.scb)|*.scb|All Files
(*.*)|*.*”;
pSaveDlg->DefaultExt = “.scb”;
int nRes = pSaveDlg->ShowDialog(); if(nRes == DialogResult::OK)
CREATING A MANAGED EXTENSIONS APPLICATION Chapter 12 437
{
pDoc->SaveDocument(pSaveDlg->FileName); pDoc->viewList->Remove(this);
}
else if(nRes == DialogResult::Cancel) e->Cancel = true;
}
else if(nSave == DialogResult::Cancel)
{
e->Cancel = true; //If user selected ‘Cancel’, don’t close
the form
}
else if(nSave == DialogResult::No) pDoc->viewList->Remove(this);
}
else
{
pDoc->viewList->Remove(this);
}
}
void CGDIPlusView::ClosedHandler(Object* sender, EventArgs* e)
{
//If there are no child views, then disable menu and toolbar items if(pMainWin->MdiChildren->Length == 0 )
pMainWin->DisableItems();
}
CGDIPlusDoc* CGDIPlusView::GetDocument()
{
return pDoc;
}
void CGDIPlusView::HandleLineDraw()
{
try
{
438 Project 2 CREATING AN APPLICATION USING MANAGED C++
Graphics* g = CreateGraphics();
g->DrawLine(pDoc->GetCurrentPen(), pDoc->nShapeLeft, pDoc->nShapeTop, pDoc->nShapeWidth, pDoc->nShapeHeight); //Here width and height is a point
}
catch (Exception* ex) { MessageBox::Show(ex->ToString());
}
}
void CGDIPlusView::HandleEllipseDraw()
{
try
{
Graphics* g = CreateGraphics();
g->DrawEllipse(pDoc->GetCurrentPen(), pDoc->nShapeLeft, pDoc->nShapeTop, pDoc->nShapeWidth, pDoc->nShapeHeight);
}
catch (Exception* ex) { MessageBox::Show(ex->ToString());
}
}
void CGDIPlusView::HandleRectDraw()
{
try
{
Graphics* g = CreateGraphics(); g->DrawRectangle(pDoc->GetCurrentPen(), pDoc->nShapeLeft, pDoc-
>nShapeTop, pDoc->nShapeWidth, pDoc->nShapeHeight);
}
catch (Exception* ex) { MessageBox::Show(ex->ToString());
}
}
void CGDIPlusView::HandleFilledEllipseDraw()
{
try
CREATING A MANAGED EXTENSIONS APPLICATION Chapter 12 439
{
Graphics* g = CreateGraphics();
g->FillEllipse(pDoc->GetCurBrush(), pDoc->nShapeLeft, pDoc->nShapeTop, pDoc->nShapeWidth, pDoc->nShapeHeight);
}
catch (Exception* ex) { MessageBox::Show(ex->ToString());
}
}
void CGDIPlusView::HandleFilledRectDraw()
{
try
{
Graphics* g = CreateGraphics();
g->FillRectangle(pDoc->GetCurBrush(), pDoc->nShapeLeft, pDoc->nShapeTop, pDoc->nShapeWidth, pDoc->nShapeHeight);
}
catch (Exception* ex) { MessageBox::Show(ex->ToString());
}
}
void CGDIPlusView::HandleTextDraw(String* pStr, Drawing::Font* pFont, Color colorText)
{
try
{
Point* p = __nogc new Point(20, 20); __box Point* pt = __box(*p);
Graphics* g = CreateGraphics();
g->DrawString(pStr, pFont, GetDocument()->GetCurBrush(), *pt);
}
catch (Exception* ex) { MessageBox::Show(ex->ToString());
}
}