Step Nine: Create the Class to Show the File Contents

So far, you've seen the steps you need to implement to create a file viewer that is a COM object in an INPROC server. To actually show the contents of the file, however, you need to create some windows. In the MFCVIEW sample, I created a simple class named CMyFrame, derived from CFrameWnd. This class includes the frame window and a child multiline edit window:

class CMyFrame : public CFrameWnd
{
DECLARE_DYNCREATE (CMyFrame)
public:
CMyFrame ();
virtual ~CMyFrame ();
void Create (void);
void UpdateEdit (char *);

// Attributes
public:
CEdit m_Edit;

// Operations
public:
// Overrides
// ClassWizard generated virtual function overrides.
// {{AFX_VIRTUAL (CMyFrame)
// }}AFX_VIRTUAL

// Implementation

protected:
// Generated message map functions
// {{AFX_MSG (CMyFrame)
afx_msg void OnSize (UINT nType, int cx, int cy);
afx_msg void OnFileExit ();
// }}AFX_MSG
DECLARE_MESSAGE_MAP ()
}

When the ShowInitialize member function is called, it makes a call to a function in the CMyFrame class in order to create the frame window and the multiline edit window:

void CMyFrame::Create (void)
{
// Create the frame window.
CFrameWnd::Create ("AfxFrameOrView", "Nancy's Viewer",
WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN,
CRect (CW_USEDEFAULT, CW_USEDEFAULT, 250, 250),
NULL, MAKEINTRESOURCE (IDR_VIEWERMENU), WS_EX_TOPMOST);

// Create the edit window inside.
// Get the size of the parent window.
CRect wndRect;
GetClientRect (&wndRect);

// Create a child edit control to display the text.
m_Edit.Create (ES_MULTILINE | ES_AUTOVSCROLL | WS_CHILD |
WS_VISIBLE | ES_AUTOVSCROLL, wndRect, this, ID_EDIT);
}

The multiline edit control is updated to reflect the contents of the current file through a call to a member function named UpdateEdit:

void CMyFrame::UpdateEdit (char *lpBufPtr)
{
::SendMessage (m_Edit.m_hWnd, WM_SETTEXT, 0, (LPARAM)lpBufPtr);
}

The sample file viewer I created simply displays the file contents and is resizable. In your own file viewer, you can add toolbars, status bars, font support, or anything else you like. You could use one of the new rich edit controls instead of a standard multiline edit control. The user interface is up to you.