DOCUMENT – VIEW ARCHITECTURE
1. Run the AppWizard to generate a project called sixth.
2. Use the resource editor to edit the application’s main menu.
- click on the Resource View tab in the workspace window.
- edit the IDR_MAINFRAME menu resource to add a separator and a Clear Document item to the Edit menu.
- add a transfer menu and then define the following items:
Menu Caption Command ID
Edit Clear & Document ID_EDIT_CLEAR_ALL
Transfer &Get Data from Document\tF2 ID_TRANSFER_GETDATA
Transfer &Store Data from Document\tF3 ID_TRANSFER_STOREDATA
3. Use the resource editor to add keyboard accelerators.
Accelerator ID Key
ID_TRANSFER_GETDATA VK_F2
ID_TRANSFER_STOREDATA VK_F3
4. Use the ClassWizard to add the view class command and update command UI message handlers.
- add the following member functions to sixthView class:
Object ID Message Member Function
ID_TRANSFER_GETDATA COMMAND OnTransferGetData
ID_TRANSFER_STOREDATA COMMAND OnTransferStoreData
ID_TRANSFER_STOREDATA UPDATE_COMMAND_UI
OnUpdateTransferStoreData
5. Use ClassWizard to add the document class command and update command UI message handlers.
- add the following member functions to sixthDoc class.
ObjectID Message Member Function
ID_EDIT_CLEAR_ALL COMMAND OnEditClearDocument
ID_EDIT_CLEAR_ALL UPDATE_COMMAND_UI
OnUpdateEditClearDocument
6. Add a CString data member to the sixthDoc class.
- edit the sixthDoc.h file
public:
CString m_strText;
7. Edit the document class member functions in sixthDoc.cpp
BOOL sixthDoc :: OnNewDocument()
{
if (!CDocument :: OnNewDocument())
return FALSE;
m_strText = “Hello (from sixthDoc :: OnNewDocument)”;
return TRUE;
}
void sixthDoc :: OnEditClearDocument()
{
m_strText.Empty();
}
void sixthDoc :: OnUpdateEditClearDocument(CCmdUI* pCmdUI)
{
pCmdUI -> Enable (!m_strText.IsEmpty());
}
8. Add a CRichEditCtrl data member to the sixthView class.
- edit the sixthView.h class
public:
CRichEditCtrl m_rich;
9. Use the ClassWizard to map the WM_CREATE and WM_SIZE messages in the sixthView class.
int sixthView :: OnCreate (LPCREATESTRUCT lpCreateStruct)
{
CRect rect (0,0,0,0);
if (CView :: OnCreate (lpCreateStruct) == -1)
return -1;
m_rich.Create (ES_AUTOVSCROLL | ES_MULTILINE |
ES_WANTRETURN | WS_CHILD | WS_VISIBLE |
WS_VSCROLL ,rect,this,1);
return 0;
}
void sixthView :: OnSize (UINT nType, int cx, int cy)
{
CRect rect;
CView OnSize (nType, cx, cy);
GetClientRect (rect);
m_rich.SetWindowPos (&wndTop, 0, 0, rect.right – rect.left,
rect.bottom – rect.top, SWP_SHOWWINDOW);
}
10. Edit the menu command handler functions in sixthView.cpp
void sixthView :: OnTransferGetData()
{
sixthDoc* pDoc = GetDocument();
m_rich.SetWindowText (pDoc -> m_strText);
m_rich.SetModify (FALSE);
}
void sixthView :: OnTransferStoreData()
{
sixthDoc* pDoc = GetDocument();
m_rich.GetWindowText (pDoc -> m_strText);
m_rich.SetModify (FALSE);
}
void sixthView :: OnUpdateTransferStroeData (CCmdUI* pCmdUI)
{
pCmdUI -> Enable (m_rich.GetModify());
}
11. Build and Test the sixth application.
No comments:
Post a Comment