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

About our authors: Team EQA

Further readings

Where is WinMain() function in MFC application ?

MFC hides WinMain in its framework and includes source file on WinMain(). This explains how framework calls global CWinApp::Initinstance() from entry WinMain.

What is the utility of CWinApp class?

This is constructed during global C++ objects are constructed and is already available when Windows calls the WinMain function, which is supplied by the ...

Basic steps in Win32 GUI Application with source code.

Define a custom Window class structure, Register the class name, CreateWindow, Show windows and write message get and dispatch loop statements. Define the Window CallBack procedure and write the handlers.

What is a Window CallBack procedure and what is its utility?

DispatchMessage() is a API which indirectly triggers the Window CallBack procedure. Message structure members from this function are passed to the CallBack procedure. CallBack procedure should implement event handlers depending on the need of the application.

What are LPARAM and WPARAM in window proc function?

LPARAM and WPARAM are the two parameters in Window CallBack procedure. They signifies parameters of various events. They are used in handing individual events.

What are the basic steps of a typical MFC based application?

We need to write WinMain and need to follow all these in a Win32 application. However we need not to write much if we are writing an application with MFC ...

#