DOCUMENT/VIEW ARCHITECTURE SERIALIZATION IN A SINGLE DOUCUMENT INTERFACE USING VC++

OBJECTIVE :
To implement document/view architecture and serialization in single document interface.
PROCEDURE:
1. Run MFC Appwizard to generate c:\JKL
2. Select single document interface in step1 of AppWizard.
3. In step 3 deselect the Activex controls.
4. In step 4.deselect the clocking tool bars and printing and print preview.
5. Rest all select the default settings.
6. The wizard will create classes like CSDIDoc, CSDIApp, CSDIView and CMainFrame.
7. Create a generic class mytext which is derived from the base class CObject by right clicking on the SDI classes in the class view and selecting new class.
8. In the new class mytext the constructor and destructor will be created.
9. Create the variables in mytext.h
private:
CString textfacename;
COLORREF textcolor;
CPoint textpoint;
int textsize;
public:
mytext(CPoint point,CString facename,int size,COLORREF color);
void drawtext(CDC *p);
void Serialize(CArchive(&ar));


10. In mytext.cpp type the definition of the constructor with 4 arguments as follows.
mytext::mytext(CPoint point,CString facename,int size,COLORREF color)
{
textpoint=point;
textfacename=facename;
textsize=size;
textcolor=color;
}
11. Again in mytext.cpp type the following definition for the member function drawtext as
void mytext::drawtext(CDC *p)
{
CFont myfont;
myfont.CreatePointFont(textsize,textfacename,p);
p->SelectObject(myfont);
p->SetTextColor(textcolor);
p->SetBkMode(TRANSPARENT);
p->TextOut(textpoint.x,textpoint.y,"hello world");
}

12. Using class wizard on the CSDI view class map the message WM_BUTTONDOWN to the object CSDIView.on the onLButtonDown handler add the following text inSDIView.cpp
void CJKLView::OnLButtonDown(UINT nFlags, CPoint point)
{
CClientDC dc(this);
CString facename[9]={"Arial","Impact","Verdana","Termainal","ComicSans MS"};
COLORREF color=RGB(0,0,225);
CString face=facename[rand()%5];
int size=100;
CFont myfont;
myfont.CreatePointFont(size,face,&dc);
dc.SelectObject(myfont);
dc.SetTextColor(color);
dc.SetBkMode(TRANSPARENT);
dc.TextOut(point.x,point.y,"hello world");
((CJKLDoc*)GetDocument())->addtext(point,face,size,color);
CView::OnLButtonDown(nFlags, point);
}
13. In SDIDoc.h include the following text
#include "mytext.h"
public: // Operations
CObArray textarr;
mytext* addtext(CPoint point,CString facename,int size,COLORREF color)
{
mytext *text;
text=new mytext(point,facename,size,color);
textarr,addtext;
SetModifiedFlag();
return text;
}
14. Edit the OnDraw function of SDIView.cpp as follows
void CJKLView::OnDraw(CDC* pDC)
{
CJKLDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
int count=pDoc->textarr.GetSize();
if(count)
{
for(int i=0;i<count;i++)
((mytext*)pDoc->textarr[i])->drawtext(pDC);
}
}

15. In SDIDoc.cpp type the text in the function
void CJKLDoc::Serialize(CArchive& ar)
{
textarr.Serialize(ar);
/*if (ar.IsStoring())
{
}
else
{
}*/
}
16. In mytext.CPP type the definition for the serialize function as follows
void mytext::Serialize(CArchive(&ar))
{
CObject::Serialize(ar);
if(ar.IsStoring())
ar<<textpoint<<textfacename<<textsize<<textcolor;
else
ar>>textpoint>>textfacename>>textsize>>textcolor;
}

17. To make the serialization really work DECLARE_SERIAL in the mytext.h file this
private:
DECLARE_SERIAL(mytext);
18. Declare IMPLEMENT_SERIAL in the mytext.cpp file next to #ifdef & #endif.
IMPLEMENT_SERIAL(mytext,CObject,1);
19. Run and test the application.

PROGRAM:
SOURCE FILES:

Mytext.cpp :
// mytext.cpp: implementation of the mytext class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "JKL.h"
#include "mytext.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
IMPLEMENT_SERIAL(mytext,CObject,1);
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
mytext::mytext()
{
}
mytext::mytext(CPoint point,CString facename,int size,COLORREF color)
{
textpoint=point;
textfacename=facename;
textsize=size;
textcolor=color;
}
mytext::~mytext()
{
}
void mytext::drawtext(CDC *p)
{
CFont myfont;
myfont.CreatePointFont(textsize,textfacename,p);
p->SelectObject(myfont);
p->SetTextColor(textcolor);
p->SetBkMode(TRANSPARENT);
p->TextOut(textpoint.x,textpoint.y,"hello world");
}
void mytext::Serialize(CArchive(&ar))
{
CObject::Serialize(ar);
if(ar.IsStoring())
ar<<textpoint<<textfacename<<textsize<<textcolor;
else
ar>>textpoint>>textfacename>>textsize>>textcolor;
}

JKLview.cpp :7
// JKLView.cpp : implementation of the CJKLView class
#include "stdafx.h"
#include "JKL.h"
#include "JKLDoc.h"
#include "JKLView.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CJKLView
IMPLEMENT_DYNCREATE(CJKLView, CView)
BEGIN_MESSAGE_MAP(CJKLView, CView)
//{{AFX_MSG_MAP(CJKLView)
ON_WM_LBUTTONDOWN()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CJKLView construction/destruction
CJKLView::CJKLView()
{
}
CJKLView::~CJKLView()
{
}
BOOL CJKLView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CJKLView drawing
void CJKLView::OnDraw(CDC* pDC)
{
CJKLDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
int count=pDoc->textarr.GetSize();
if(count)
{
for(int i=0;i<count;i++)
((mytext*)pDoc->textarr[i])->drawtext(pDC);
}
}
/////////////////////////////////////////////////////////////////////////////
// CJKLView diagnostics
#ifdef _DEBUG
void CJKLView::AssertValid() const
{
CView::AssertValid();
}
void CJKLView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CJKLDoc* CJKLView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CJKLDoc)));
return (CJKLDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CJKLView message handlers
void CJKLView::OnLButtonDown(UINT nFlags, CPoint point)
{
CClientDC dc(this);
CString facename[9]={"Arial","Impact","Verdana","Termainal","ComicSans MS"};
COLORREF color=RGB(0,0,225);
CString face=facename[rand()%5];
int size=100;
CFont myfont;
myfont.CreatePointFont(size,face,&dc);
dc.SelectObject(myfont);
dc.SetTextColor(color);
dc.SetBkMode(TRANSPARENT);
dc.TextOut(point.x,point.y,"hello world");
((CJKLDoc*)GetDocument())->addtext(point,face,size,color);
CView::OnLButtonDown(nFlags, point);
}

JKLdoc.cpp :
// JKLDoc.cpp : implementation of the CJKLDoc class
#include "stdafx.h"
#include "JKL.h"
#include "JKLDoc.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CJKLDoc
IMPLEMENT_DYNCREATE(CJKLDoc, CDocument)
BEGIN_MESSAGE_MAP(CJKLDoc, CDocument)
//{{AFX_MSG_MAP(CJKLDoc)
// NOTE - the ClassWizard will add and remove mapping macros here.
// DO NOT EDIT what you see in these blocks of generated code!
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CJKLDoc construction/destruction
CJKLDoc::CJKLDoc()
{
}
CJKLDoc::~CJKLDoc()
{
}
BOOL CJKLDoc::OnNewDocument()
{
if (!CDocument::OnNewDocument())
return FALSE;
// TODO: add reinitialization code here
// (SDI documents will reuse this document)
return TRUE;
}
/////////////////////////////////////////////////////////////////////////////
// CJKLDoc serialization
void CJKLDoc::Serialize(CArchive& ar)
{
textarr.Serialize(ar);
/*if (ar.IsStoring())
{
// TODO: add storing code here
}
else
{
// TODO: add loading code here
}*/
}
/////////////////////////////////////////////////////////////////////////////
// CJKLDoc diagnostics
#ifdef _DEBUG
void CJKLDoc::AssertValid() const
{
CDocument::AssertValid();
}

void CJKLDoc::Dump(CDumpContext& dc) const
{
CDocument::Dump(dc);
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CJKLDoc commands


HEADER FILES:

Mytext.h :
// mytext.h: interface for the mytext class.
//
//////////////////////////////////////////////////////////////////////
#if !defined(AFX_MYTEXT_H__BDBE8C8D_F625_4985_9EAF_860124733BA0__INCLUDED_)
#define AFX_MYTEXT_H__BDBE8C8D_F625_4985_9EAF_860124733BA0__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
class mytext : public CObject
{
private:
DECLARE_SERIAL(mytext);
CString textfacename;
COLORREF textcolor;
CPoint textpoint;
int textsize;
public:
mytext();
mytext(CPoint point,CString facename,int size,COLORREF color);
void drawtext(CDC *p);
void Serialize(CArchive(&ar));
virtual ~mytext();

};
#endif // !defined(AFX_MYTEXT_H__BDBE8C8D_F625_4985_9EAF_860124733BA0__INCLUDED_)

JKL view.h :
// JKLView.h : interface of the CJKLView class
//
/////////////////////////////////////////////////////////////////////////////
#if !defined(AFX_JKLVIEW_H__4DD10721_ABB2_4BF3_BBB8_919056627B1C__INCLUDED_)
#define AFX_JKLVIEW_H__4DD10721_ABB2_4BF3_BBB8_919056627B1C__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
class CJKLView : public CView
{
protected: // create from serialization only
CJKLView();
DECLARE_DYNCREATE(CJKLView)

// Attributes
public:
CJKLDoc* GetDocument();

// Operations
public:
// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CJKLView)
public:
virtual void OnDraw(CDC* pDC); // overridden to draw this view
virtual BOOL PreCreateWindow(CREATESTRUCT& cs);
protected:
//}}AFX_VIRTUAL
// Implementation
public:
virtual ~CJKLView();
#ifdef _DEBUG
virtual void AssertValid() const;
virtual void Dump(CDumpContext& dc) const;
#endif
protected:
// Generated message map functions
protected:
//{{AFX_MSG(CJKLView)
afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
#ifndef _DEBUG // debug version in JKLView.cpp
inline CJKLDoc* CJKLView::GetDocument()
{ return (CJKLDoc*)m_pDocument; }
#endif
/////////////////////////////////////////////////////////////////////////////
//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.

#endif // !defined(AFX_JKLVIEW_H__4DD10721_ABB2_4BF3_BBB8_919056627B1C__INCLUDED_)

JKLdoc.h :
// JKLDoc.h : interface of the CJKLDoc class
//
/////////////////////////////////////////////////////////////////////////////
#if !defined(AFX_JKLDOC_H__999C7E76_241F_4FA3_9B31_80F7CD48AAD9__INCLUDED_)
#define AFX_JKLDOC_H__999C7E76_241F_4FA3_9B31_80F7CD48AAD9__INCLUDED_
#include "mytext.h"
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
class CJKLDoc : public CDocument
{
protected: // create from serialization only
CJKLDoc();
DECLARE_DYNCREATE(CJKLDoc)
// Attributes
public: // Operations
CObArray textarr;
mytext* addtext(CPoint point,CString facename,int size,COLORREF color)
{
mytext *text;
text=new mytext(point,facename,size,color);
textarr,addtext;
SetModifiedFlag();
return text;
} // Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CJKLDoc)
public:
virtual BOOL OnNewDocument();
virtual void Serialize(CArchive& ar);
//}}AFX_VIRTUAL
// Implementation
public:
virtual ~CJKLDoc();
#ifdef _DEBUG
virtual void AssertValid() const;
virtual void Dump(CDumpContext& dc) const;
#endif
protected:
// Generated message map functions
protected:
//{{AFX_MSG(CJKLDoc)
// NOTE - the ClassWizard will add and remove member functions here.
// DO NOT EDIT what you see in these blocks of generated code !
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
/////////////////////////////////////////////////////////////////////////////
//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.
#endif // !defined(AFX_JKLDOC_H__999C7E76_241F_4FA3_9B31_80F7CD48AAD9__INCLUDED_)




DOCUMENT/VIEW ARCHITECTURE SERIALIZATION IN A SINGLE DOUCUMENT INTERFACE USING VC++

Post a Comment

1 Comments

  1. good idea...
    please visit my blogs,
    janganbersedih.blogspot.com
    thanks a lot...

    ReplyDelete