11.04.2014 Views

Advanced MFC Programming

Advanced MFC Programming

Advanced MFC Programming

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 8. DC, Pen, Brush and Palette<br />

Storing Data<br />

When the user finishes drawing a line, the starting and ending points will be stored in the document. At<br />

this time, instead of drawing the new line directly to the window, we need to update the client window and<br />

let the function CView::OnDraw(…) be called. In this function, all the lines added previously will be drawn<br />

again, so the client will always become up-to-date.<br />

We must override function CView::OnDraw(…) to implement client window drawing for an application.<br />

This is because the window update may happen at any time. For example, when the application is restored<br />

from the icon state, the whole portion of the window will be painted again. The system will draw the nonclient<br />

area such as caption bar and borders, and it is the application’s responsibility to implement client area<br />

drawing, which should be carried out in function CView::OnDraw(…) In <strong>MFC</strong>, if the application does not<br />

implement this, the client window will be simply painted with the default color. As a programmer, it is<br />

important for us to remember that when we output something to the window, it will not be kept there<br />

forever. We must redraw the output whenever the window is being updated.<br />

This forces us to store every line that has been drawn by the user in document, and redraw all of them<br />

when necessary. This is the basic document/view structure of <strong>MFC</strong>: storing data in CDocument derived<br />

class, and representing it in function CView::OnDraw(…).<br />

In the sample, the lines are stored in an array declared in the document class CGDIDoc. Also, some new<br />

functions are declared to let the data be accessible from the view:<br />

……<br />

……<br />

……<br />

class CGDIDoc : public CDocument<br />

{<br />

public:<br />

void AddLine(CRect rect){m_paLines.Add(new CRect(rect));}<br />

CRect *GetLine(int nID){return (CRect *)m_paLines.GetAt(nID);}<br />

int GetNumOfLines(){return m_paLines.GetSize();}<br />

protected:<br />

CPtrArray m_paLines;<br />

};<br />

A CPtrArray type variable m_paLines is declared for storing lines. Class CPtrArray allows us to add<br />

and delete pointer type objects dynamically. This class encapsulates memory allocation and release, so we<br />

do not have to worry about memory management when adding new elements. Three new public member<br />

functions are also declared, among them CGDIDoc::AddLine(…) can be used to add a new line to<br />

m_paLines, CGDIDoc::GetLine(…) can be used to retrieve a specified line from m_paLines, and CGDIDoc::<br />

GetNumOfLines() can be used to retrieve the total number of lines stored in m_paLines. Here lines are<br />

stored in CRect type variables, usually this class is used to store rectangles. In the sample, the rectangle’s<br />

upper-left and bottom-right points are used to represent a line.<br />

Although class CPtrArray can manage the memory allocation and release for storing pointers, it does<br />

not release the memory for the stored objects. So in class CGDIDoc’s destructor, we need to delete all the<br />

objects stored in the array as follows:<br />

CGDIDoc::~CGDIDoc()<br />

{<br />

while(m_plLines.GetSize())<br />

{<br />

delete m_plLines.GetAt(0);<br />

m_pLines.RemoveAt(0);<br />

}<br />

}<br />

Here we just delete the first object and remove it from the array repeatedly until the size of the array<br />

becomes 0.<br />

In CView derived class, we can call function CView::GetDocument() to obtain a pointer to the<br />

document and use it to access all the public variables and functions declared there.<br />

204

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!