EQuestionAnswers.com Computer/Electronics Questions and Answers
C, C++, VC++, COM/DCOM, DLL and more
#Login #Sign up  Facebook Twitter TGoogle+
 

#Previous question #Index of VC++/WIN32/MFC Questions #Next question

OnDraw

CView class is to represent the view of the document associated with it. A document can contain graphical drawing, text, and other objects. View should draw the appropiate output to the screen. OnDraw() event which should draw the state of the document in GUI. It is called whenever a document is refreshed i.e. on new and open event. MFC framework invalidates the window after a new document is created or a new document is opened by user. Windows GDI sends a PAINT event to main frame and MFC message handler calls OnDraw() event.

OnDraw() call stack

CMySDIView::OnDraw()
CView::OnPaint()
CWnd::OnWndMsg()
CWnd::WindowProc()
AfxCallWndProc()
AfxWndProc()
AfxWndProcBase()

OnDraw() Demo

In our application we have taken string a member named m_strFile for our CDocument. We are storing the path of the file as a state and printing the same in view.

class CMySDIDoc : public CDocument
{
protected: // create from serialization only
  CMySDIDoc();
  DECLARE_DYNCREATE(CMySDIDoc)

// Attributes
public:

// Operations
public:

// Overrides
public:
  virtual BOOL OnNewDocument();
  virtual void Serialize(CArchive& ar);

public:
  virtual ~CMySDIDoc();
  CString  m_strFile;
protected:
    
// Generated message map functions
protected:

  DECLARE_MESSAGE_MAP()
};

/* Loading "Untitled" in New document */
BOOL CMySDIDoc::OnNewDocument()
{
  if (!CDocument::OnNewDocument())
    return FALSE;

  m_strFile = "Untitled";
  return TRUE;
}

/* Open or Save Event */
void CMySDIDoc::Serialize(CArchive& ar)
{
  /* Saving the path to the file */
  if (ar.IsStoring())
  {
    m_strFile = ar.GetFile()->GetFilePath();
  }
  else /* Loading the path from file */
  {
    m_strFile = ar.GetFile()->GetFilePath();
  }
}

OnDraw() override

Now MFC doc-view framework calls this OnDraw() virtual function of CView after a new document is loaded or existing document is opened. We are drawing the m_strFile as a text in the view. So new document creates an output of "Untitled" text and open document shows the path of the file.

void CMySDIView::OnDraw(CDC* pDC)
{
  RECT rect;
  CMySDIDoc* pDoc = GetDocument();
  ASSERT_VALID(pDoc);
  pDC->GetWindow()->GetClientRect(&rect);
  pDC->DrawText(pDoc->m_strFile, &rect, 0);
}

OnDraw after OnNewDocument

OnDraw after OnNewDocument

OnDraw after OnOpenDocument

OnDraw after OnOpenDocument

You have viewed 1 page out of 109. Your VC++ learning is 0.00% complete. Login to check your learning progress.

 Vote 0

Similar topics related to this section

#Programming Windows, Fifth Edition (Developer Reference)
#Programming Applications for Microsoft Windows (Microsoft Programming Series)
#Programming Windows with MFC, Second Edition
#Visual C++ Programming
#Programming Microsoft Visual C++, Fifth Edition

* #1 webmaster Tue 29 Apr/2014 08:30:18(GMT)  Like 0 Unlike 0

Dear Users,

We are pleased to inform you that a forum/blog has been incorporated with www.mybestnotes.co.in. You are welcomed to add your comments, requests, codes, solutions and feedback to it. Please login(if already a member) or signup(for free) to avail to this facility.

Regards,
Webmaster

Your message goes here:

Name:Guest
Email:anonymous@unknown.com
My Post:*
Secutiry Code: ******  *
Preview this compose before posting this in discussion forum.
 
Note:
  1. Use [Sxx] code to show smiles. Example [S02]
  2. Use [URL ], example [URL http://www.google.com/]
  3. To display code or un formatted text use [CODE] [/CODE]. Example: [CODE] printf("Hello world"); [/CODE]
 [S01]   [S02]   [S03]   [S04]   [S05]   [S06]   [S07]   [S08] 
 [S09]   [S10]   [S11]   [S12]   [S13]   [S14]   [S15]   [S16] 
 [S17]   [S18]   [S19]   [S20]   [S21]   [S22]   [S23]   [S24] 

Note: Only members are allowed, * fields are mandatory.