MFC Model
When we used AppWizard to create out application, it generated a lot of starter classes, files and provided access to Window resources. The MFC programming model seperates the storage of the program's data ( - the document) from the display of that document ( - the view), and from the user interaction of the document. Consider the classes created for our application:
The CAppFirstWinApp class manages the Class CDocTemplate. This coordinates one or more existing documents of a given type and manages creating the correct document, view and frame window objects for that type. It manages all framework objects. The CDocTemplate manages the CAppFirstWinDoc, the CAppFirstWinView , and the CMainFrame.
The CAppFirstWinDoc is derived from CDocument is used to store and control program's data. Typically there is a one to one correspondence between the data in the documents and the data displayed. This object manages a list of views of the data.
The CAppFirstWinView is derived from CView and is used to display the data and manage user interaction with that data. It is attached to the document but owned by the frame window.
The CMainFrame is derived from CFrameWnd provides the window/screen in which the CAppFirstWinView displays the view.
Much of the code invodes adding application specific functionalities to the CDocument and CView class
Implementing Document:
Programmers responsibility:
Derive the document class from CDocument
Add data members to the class
Implement application specific initialization and clean up
Override document Serialize function
Framework provides the following:
Document services
Calls the initialization and clean up functions at the appropriate times
Implements the overriding functions in Serialize File Open, Save etc
Implementing View:
Programmers responsibility:
Derive Class from CView
Framework provides:
View services
Our Final Program
We are going to hack the code we have developed earlier to draw some graphics to the screen. We will draw the (vertex only ) in the application window ( also called the client window). Once you have developed the code to generate the points , you should have no trouble displaying them.
We will use the AppWizard created application created last time " AppFirstWin"
We will use the Class Wizard to include the OnPaint() function in which we will place all the code.
We will place some code to dispay the vertices
A caution: What we are doing is to trivializing the use of the MFC classes to access the graphic functions. To really understand the actions a serious examination of the use of MFC classes must be done. This can be accomplished through tutorials provides as part of Visual Studio. Be prepared to spend a lot of time
The steps in our final exercise:
Open the AppFirstWin Project in VS
From the View Menu Select Class Wizard
We will select
Class name : CAppFirstWinView
Object Ids': CAppFirstWinView
Messages: WM_PAINT
Click on Add Functions
The Wizard will place the template for the OnPaint() function in the file.
Lets place the following code:
void CAppFirstWinView::OnPaint() // the OnPaint function
{
// a lot of this code is unnecessary
// this is to show you that you can use a struct here
#define MAX_ORDER 6 // constants
#define MAX_POINTS 50
// the curve is defined as a structure
// the arrays have to be dimensioned so
// that the compiler needs to know
// how much memory to allot to the program
struct bez_Curve {
int order;
double xVert[MAX_ORDER + 1];
double yVert[MAX_ORDER + 1];
double xyData [MAX_POINTS][MAX_POINTS];
};
// function prototypes
//double combination(double value1, double value2);
//double factorial (double value);
//void calcCoeff(int n,
//double A[MAX_ORDER + 1][MAX_ORDER + 1]);
// scope within main()
bez_Curve Curve1; // C++ definition of structure variable
int ivar;
double coeff[MAX_ORDER + 1][MAX_ORDER + 1];
static COLORREF Colr[2] = {RGB(255,0,0),
RGB(0,0,255)}; // color definition COLORREF used to store color
POINT Vertex1[7], Vertex2[7]; // A point
Curve1.order = 3;
CPaintDC dc(this); // device context for painting
CPen newpen; // a Pen object - used for drawing
CPen* oldpen; // pointer to a pen object
// necessary for setting properties
Curve1.xVert[0] = 0.0;
Curve1.yVert[0] = 0.3;
Curve1.xVert[1] = 0.3;
Curve1.yVert[1] = 0.5;
Curve1.xVert[2] = 0.5;
Curve1.yVert[2] = 0.8;
Curve1.xVert[3] = 1.0;
Curve1.yVert[3] = 1.0;
for (ivar = 0; ivar < Curve1.order + 1; ivar++)
{
Vertex1[ivar].x = Curve1.xVert[ivar]*200;
// defining the point - x and y values in pixels
Vertex1[ivar].y = Curve1.yVert[ivar]*200;
Vertex2[ivar].x = Curve1.xVert[ivar]*200+ 200;
Vertex2[ivar].y = Curve1.yVert[ivar]*200 + 200;
}
newpen.CreatePen(PS_SOLID, 3, Colr[0]);
// pen is created with Style, width of pixel, and color
oldpen =dc.SelectObject(&newpen);
// changes the current color to that of newpen
// and returns the Old pen information to oldpen
dc.MoveTo(Vertex1[0]); // moves the cursor to first point of Vertex1
for (ivar = 1; ivar < Curve1.order + 1; ivar++)
dc.LineTo(Vertex1[ivar]);
dc.SelectObject(oldpen);
newpen.DeleteObject();
newpen.CreatePen(PS_DASH, 1, Colr[1]);
dc.Polyline(Vertex2,4); // another way to draw a polygon
dc.TextOut(220,265,"Writing in the Client Window",28);
}
Build and Execute
That' All Folks !
Hope you enjoyed the course !!!