Functions generated using Visual C ++'s MFC appwizar are generally in the "document/View" structure.
In this program structure, the division of documents and view objects is very clear. Document objects are used to store, manage, and maintain data, while view objects are used to display and operate data.
Therefore, in document/view structures, data objects are usually defined in the Declaration of document classes, and data objects are initialized in the constructors of document classes, operations and display of data objects are completed in view objects.
To access the members of a document object in a view object, the view class provides a member function getdocument, Which is prototype:
Cdocument * getdocument () const;
The returned value is a pointer to the Document Object.
Therefore, before performing data operations on a document, you should use the View class member function getdocument to obtain the pointer of the Document Object in the view object, then, access the members of the document through this object.
For example, use the "document/View" structure to complete the "rectangle with the mouse"
1) use MFC Appwizard [EXE] to create a single-document application framework named gun3;
2) Open the FileView tab, click header files/stdadx. H, and add the include command in stdadx. h: # include <afxtempl. h>
3) define the array class Object m_rectag in the document class gun3doc. h declaration:
4) define the array size in the constructor of the document class gun3doc. cpp:
CGun3Doc::CGun3Doc(){// TODO: add one-time construction code herem_Rectag.SetSize(256,256);}
5) in the onlbuttondown () function of the View class, set a pointer to the document and obtain the members of the document through the pointer:
Void cgun3view: onlbuttondown (uint nflags, cpoint point) {// todo: add your message handler code here and/or call defaultcgun3doc * pdoc = getdocument (); // get the document pointer int r = rand () % 50 + 5; crect RET (point. x-R, point. y-R, point. X + R, point. Y + r); pdoc-> m_rectag.add (RET); // Add the element invalidaterect (Ret, false) to the array in the document; // trigger the ondraw () function cview :: onlbuttondown (nflags, point );}
6) in the ondraw () function, draw the rectangle in the array:
Void cgun3view: ondraw (CDC * PDC) {cgun3doc * pdoc = getdocument (); // obtain the document pointer assert_valid (pdoc); // todo: add draw code for native data herefor (INT I = 0; I <pdoc-> m_rectag.getsize (); I ++) PDC-> rectangle (pdoc-> m_rectag [I]);}