Microsoft Visual C++ .NET Professional Projects - Premier Press
.pdf120 |
Part I |
|
INTRODUCING VC++.NET |
|
|
|||
|
|
|
|
|
|
|
|
|
|
|
#include “MDIApp.h” |
|
|
|
|
|
|
|
|
#include “MainFrm.h” |
|
|
|
|
|
|
|
|
IMPLEMENT_DYNAMIC(CMainFrame, CMDIFrameWnd) |
|
|
||||
|
|
BEGIN_MESSAGE_MAP(CMainFrame, CMDIFrameWnd) |
|
|
||||
|
|
|
ON_WM_CREATE() |
|
|
|
|
|
|
|
END_MESSAGE_MAP() |
|
|
|
Y |
|
|
|
|
static UINT indicators[] = |
|
|
|
|
||
|
|
|
|
L |
|
|||
|
|
{ |
|
|
|
|
||
|
|
|
|
|
F |
|
||
|
|
|
ID_SEPARATOR, |
|
|
|
||
|
|
|
|
|
// status line indicator |
|||
|
|
|
ID_INDICATOR_CAPS, |
|
|
|
||
|
|
|
ID_INDICATOR_NU , |
M |
|
|
||
|
|
|
|
|
|
|
||
|
|
|
ID_INDICATOR SCRL, |
|
|
|
||
|
|
}; |
E |
|
|
|
||
|
|
|
|
|
|
|||
|
|
int CMainFrame::OnCreate(LPCR |
T STRUCT lpCreateStruct) |
|
||||
|
|
{ |
T |
|
|
|
|
|
|
|
if (CMDIFrameWnd::OnCreate(lpCreateStruct)A |
|
|||||
|
|
|
== -1) |
return -1;
if (!m wnd oolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP |
CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC)
||
!m_wndToolBar.LoadToolBar(IDR_MAINFRAME))
{
TRACE0(“Failed to create toolbar\n”); return -1;
}
if (!m_wndStatusBar.Create(this) || m_wndStatusBar.SetIndicators(indicators,
sizeof(indicators)/sizeof(UINT)))
{
TRACE0(“Failed to create status bar\n”); return -1; // fail to create
}
m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY); EnableDocking(CBRS_ALIGN_ANY); DockControlBar(&m_wndToolBar);
Team-Fly®
THE DOCUMENT/VIEW ARCHITECTURE |
Chapter 5 |
121 |
|
|
|
return 0;
}
BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{
if( !CMDIFrameWnd::PreCreateWindow(cs) ) return FALSE;
return TRUE;
}
//MDIAppDoc.h – Contains the declaration of the document class #Include “Draw.h”
// The header file of the user-defined class that handles the Draw menu class CMDIAppDoc : public CDocument
{
protected:
CMDIAppDoc();
DECLARE_DYNCREATE(CMDIAppDoc)
public: //overriding functions
virtual BOOL OnNewDocument();
virtual void Serialize(CArchive& ar);
public:
virtual ~CMDIAppDoc();
// User-defined attributes and functions used to handle the Draw menu option of your application
CObArray Diag_Count;
Draw* DiagAdd(CPoint begin, CPoint end, int); int GetDiagCount();
Draw* GetDiag(int);
virtual void DeleteContents();
protected:
DECLARE_MESSAGE_MAP()
};
//MDIAppDoc.cpp – Contains the definition of the document class #include “stdafx.h”
#include “MDIApp.h” #include “MDIAppDoc.h”
122 |
Part I |
INTRODUCING VC++.NET |
|
|
|
NOTE
As the comment entries specify, the code generated by the wizard has been edited to add the functionality to handle the drawing feature of the application.
IMPLEMENT_DYNCREATE(CMDIAppDoc, CDocument)
BOOL CMDIAppDoc::OnNewDocument()
{
if (!CDocument::OnNewDocument()) return FALSE;
// (SDI documents will reuse this document) return TRUE;
}
void CMDIAppDoc::Serialize(CArchive& ar)
{
Diag_Count.Serialize(ar);
}
Draw* CMDIAppDoc::DiagAdd(CPoint begin, CPoint end, int choice) // Function that populates the Diag_Count array
{
Draw* temp = new Draw(begin, end, choice);
Diag_Count.Add(temp);
return temp;
}
int CMDIAppDoc::GetDiagCount()
//Function that returns the size of the Diag_Count array
{
return Diag_Count.GetSize();
}
Draw* CMDIAppDoc::GetDiag(int index)
//Function that returns a pointer to a specific element within the Diag_Count array
{
return(Draw*)Diag_Count[index];
}
void CMDIAppDoc::DeleteContents()
{
THE DOCUMENT/VIEW ARCHITECTURE |
Chapter 5 |
123 |
|
|
|
int count=Diag_Count.GetSize(); int index;
if (count)
{
for (index=0; index<count; index++)
{
delete Diag_Count[index];
}
}
CDocument::DeleteContents();
}
//MDIAppView.h – Contains the declaration of the view class class CMDIAppView : public CView
{
protected: // create from serialization only CMDIAppView(); DECLARE_DYNCREATE(CMDIAppView)
int choice;
CPoint start, last;
public:
CMDIAppDoc* GetDocument() const;
CMDIAppDoc* pDoc;
public:
virtual void OnDraw(CDC* pDC); // overridden to draw this view virtual BOOL PreCreateWindow(CREATESTRUCT& cs);
protected:
virtual BOOL OnPreparePrinting(CPrintInfo* pInfo);
virtual void OnBeginPrinting(CDC* pDC, CPrintInfo* pInfo); virtual void OnEndPrinting(CDC* pDC, CPrintInfo* pInfo);
public:
virtual ~CMDIAppView();
protected:
DECLARE_MESSAGE_MAP()
public:
afx_msg void OnDrawLine();
afx_msg void OnLButtonDown(UINT nFlags, CPoint point); afx_msg void OnLButtonUp(UINT nFlags, CPoint point);
124 |
Part I |
INTRODUCING VC++.NET |
|
|
|
afx_msg void OnMouseMove(UINT nFlags, CPoint point); afx_msg void OnDrawRectangle();
afx_msg void OnDrawEllipse();
};
#include “stdafx.h” #include “MDIApp.h” #include “MDIAppDoc.h” #include “MDIAppView.h”
IMPLEMENT_DYNCREATE(CMDIAppView, CView)
BEGIN_MESSAGE_MAP(CMDIAppView, CView)
ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
ON_COMMAND(ID_DRAW_LINE, OnDrawLine)
ON_WM_LBUTTONDOWN()
ON_WM_LBUTTONUP()
ON_WM_MOUSEMOVE()
ON_COMMAND(ID_DRAW_RECTANGLE, OnDrawRectangle)
ON_COMMAND(ID_DRAW_ELLIPSE, OnDrawEllipse)
END_MESSAGE_MAP()
BOOL CMDIAppView::PreCreateWindow(CREATESTRUCT& cs)
{
return CView::PreCreateWindow(cs);
}
// CMDIAppView drawing
void CMDIAppView::OnDraw(CDC* pDC)
{
pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here int DiagCount=pDoc->GetDiagCount();
// Retrieving the total number of diagrams if(DiagCount)
{
THE DOCUMENT/VIEW ARCHITECTURE |
Chapter 5 |
125 |
|
|
|
int Begin; Draw *draw1;
for(Begin=0;Begin<DiagCount;Begin++)
{
draw1=pDoc->GetDiag(Begin);
//Retrieving each of the Draw objects stored
//In the Diag_Count array
draw1->Diag(pDC);
//Invoke the Diag function that displays the
//diagram in the client area
}
}
}
CMDIAppDoc* CMDIAppView::GetDocument() const // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMDIAppDoc)));
return (CMDIAppDoc*)m_pDocument;
}
void CMDIAppView::OnDrawLine()
{
choice=1;
}
void CMDIAppView::OnDrawRectangle()
{
choice=2;
}
void CMDIAppView::OnDrawEllipse()
{
choice=3;
}
void CMDIAppView::OnLButtonDown(UINT nFlags, CPoint point)
{
start=last=point;
}
void CMDIAppView::OnMouseMove(UINT nFlags, CPoint point)
126 |
Part I |
INTRODUCING VC++.NET |
|
|
|
{
if((nFlags&MK_LBUTTON)==MK_LBUTTON)
{
CClientDC *cdc;
cdc=new CClientDC(this); cdc->SetROP2(R2_NOT);
Draw *temp=new Draw(start,last,choice); temp->Diag(cdc);
Draw *temp1=new Draw(start,point,choice); temp1->Diag(cdc);
last=point;
}
}
void CMDIAppView::OnLButtonUp(UINT nFlags, CPoint point)
{
CClientDC *cdc;
cdc=new CClientDC(this);
Draw *temp=pDoc->DiagAdd(start,point,choice); temp->Diag(cdc);
choice=0;
}
//Draw.h – Contains the declarations of the user-defined class Draw class Draw : public CObject
{
DECLARE_SERIAL(Draw) //To Implement serialization In this class
public:
CPoint StartPoint, EndPoint;
void Serialize(CArchive &ar);
virtual ~Draw(); void Diag(CDC *cdc);
Draw(CPoint PointOne,CPoint PointTwo, int choice);
Draw();
int DiagType;
};
//Draw.cpp – Contains the definition of the Draw class #include “stdafx.h”
#include “MDIApp.h”
THE DOCUMENT/VIEW ARCHITECTURE |
Chapter 5 |
127 |
|
|
|
#include “Draw.h” int diagchoice;
IMPLEMENT_SERIAL(Draw,CObject,1);
Draw::Draw(CPoint PointOne, CPoint PointTwo, int choice)
{
StartPoint=PointOne;
EndPoint=PointTwo;
DiagType=choice;
}
void Draw::Diag(CDC *cdc)
{
if(DiagType==1)
{
cdc->MoveTo(StartPoint); cdc->LineTo(EndPoint);
}
else if(DiagType==2)
{
cdc->Rectangle(StartPoint.x, StartPoint.y,
EndPoint.x,EndPoint.y);
}
else if(DiagType==3)
{
cdc->Ellipse(StartPoint.x, StartPoint.y,
EndPoint.x,EndPoint.y);
}
}
void Draw::Serialize(CArchive &ar)
{
CObject::Serialize(ar);
if(ar.IsStoring())
ar<<StartPoint1<<EndPoint1<<DiagType;
else
ar>>StartPoint1>>EndPoint1>>DiagType;
}
128 |
Part I |
INTRODUCING VC++.NET |
|
|
|
As discussed before, you can use the MFC Application Wizard to create this application. To create the Draw class, choose Project, Add Class and select CObject as the base class. To add a new option to the menu, you need to switch to the Resource view and edit the menu associated with your application. A sample of the menu designed for this application is shown in Figure 5-4.
For event handlers for these menu options, right-click the option and choose Add Event Handler. The Event Handler Wizard, shown in Figure 5-5, allows you to select the type of message you want to trap, the class that will handle the event, and the name for the message handler. After specifying your choices, click Add and then Edit to add the required code.
Figure 5-6 displays the sample paint application.
Revisiting the Flow of Control in a Document/
View-Based Application
Before we conclude, let us quickly glance through how the control flows in a Doc- ument/View-based application.
FIGURE 5-4 User-defined menu option of the sample application
THE DOCUMENT/VIEW ARCHITECTURE |
Chapter 5 |
129 |
|
|
|
FIGURE 5-5 The Event Handler Wizard
FIGURE 5-6 The sample paint application