文档视图结构中,缺省的命令处理
文档视图结构中,缺省的命令处理
在文档视图结构所构建的框架中,很多命令ID都有缺省的命令处理。很多功能都由他们完成,但这些功能在程序中不“可见”,不便于对程序的理解。
以下归纳了常见的缺省处理及其流程
在需要时候,可以重载这些函数以实现特定功能
CWinApp::OnFileNew
调用m_pDocManager->OnFileNew()
void CDocManager::OnFileNew() { if (m_templateList.IsEmpty()) { TRACE0("Error: no document templates registered with CWinApp./n"); AfxMessageBox(AFX_IDP_FAILED_TO_CREATE_DOC); return; } CDocTemplate* pTemplate = (CDocTemplate*)m_templateList.GetHead(); //如果含有多个文档模板,显示一个新建文档类型对话框 if (m_templateList.GetCount() > 1) { // more than one document template to choose from // bring up dialog prompting user CNewTypeDlg dlg(&m_templateList); int nID = dlg.DoModal(); if (nID == IDOK) pTemplate = dlg.m_pSelectedTemplate; else return; // none - cancel operation } ASSERT(pTemplate != NULL); ASSERT_KINDOF(CDocTemplate, pTemplate); //建立空文件 pTemplate->OpenDocumentFile(NULL); // if returns NULL, the user has already been alerted }
CWinApp::OnFileOpen
调用m_pDocManager->OnFileOpen()
void CDocManager::OnFileOpen() { //出现打开文件对话框文件取得文件名 CString newName; if (!DoPromptFileName(newName, AFX_IDS_OPENFILE, OFN_HIDEREADONLY ¦ OFN_FILEMUSTEXIST, TRUE, NULL)) return; //使用OpenDocumentFile AfxGetApp()->OpenDocumentFile(newName); // if returns NULL, the user has already been alerted }
CDocument::OnFileSave()
调用DoFileSave()
void CDocument::OnFileSave()
{
DoFileSave();
}
DoFileSave()又将调用DoSave()
BOOL CDocument::DoFileSave()
{
DWORD dwAttrib = GetFileAttributes(m_strPathName);
//如果文件是只读,或已经不存在了
if (dwAttrib & FILE_ATTRIBUTE_READONLY)
{
//使用带NULL参数的DoSave
if (!DoSave(NULL))
{
TRACE0("Warning: File save with new name failed./n");
return FALSE;
}
}
else
{
//使用DoSave(....)
if (!DoSave(m_strPathName))
{
TRACE0("Warning: File save failed./n");
return FALSE;
}
}
return TRUE;
}
DoSave()的实现
BOOL CDocument::DoSave(LPCTSTR lpszPathName, BOOL bReplace) //如果文件名参数为空,则让用户取名 // note: lpszPathName can be different than 'm_strPathName' { CString newName = lpszPathName; if (newName.IsEmpty()) { CDocTemplate* pTemplate = GetDocTemplate(); ASSERT(pTemplate != NULL); newName = m_strPathName; if (bReplace && newName.IsEmpty()) { newName = m_strTitle; // check for dubious filename int iBad = newName.FindOneOf(_T(" #%;///")); if (iBad != -1) newName.ReleaseBuffer(iBad); // append the default suffix if there is one CString strExt; if (pTemplate->GetDocString(strExt, CDocTemplate::filterExt) && !strExt.IsEmpty()) { ASSERT(strExt[0] == '.'); newName += strExt; } } //"保存为"对话框 if (!AfxGetApp()->DoPromptFileName(newName, bReplace ? AFX_IDS_SAVEFILE : AFX_IDS_SAVEFILECOPY, OFN_HIDEREADONLY ¦ OFN_PATHMUSTEXIST, FALSE, pTemplate)) return FALSE; // don't even attempt to save } CWaitCursor wait; //保存,由OnSaveDocument完成 if (!OnSaveDocument(newName)) { if (lpszPathName == NULL) { // be sure to delete the file TRY { CFile::Remove(newName); } CATCH_ALL(e) { TRACE0("Warning: failed to delete file /n"); DELETE_EXCEPTION(e); } END_CATCH_ALL } return FALSE; } // reset the title and change the document name if (bReplace) SetPathName(newName); return TRUE; // success }
使用了文档类的OnSaveDocument完成保存动作
BOOL CDocument::OnSaveDocument(LPCTSTR lpszPathName)
{
CFileException fe;
CFile* pFile = NULL;
pFile = GetFile(lpszPathName, CFile::modeCreate ¦
CFile::modeReadWrite ¦ CFile::shareExclusive, &fe);
if (pFile == NULL)
{
ReportSaveLoadException(lpszPathName, &fe,
TRUE, AFX_IDP_INVALID_FILENAME);
return FALSE;
}
//建立保存用的CArchive
CArchive saveArchive(pFile, CArchive::store ¦ CArchive::bNoFlushOnDelete);
saveArchive.m_pDocument = this;
saveArchive.m_bForceFlat = FALSE;
TRY
{
CWaitCursor wait;
//使用文档类的序列化操作完成实际存档行为
Serialize(saveArchive); // save me
saveArchive.Close();
ReleaseFile(pFile, FALSE);
}
CATCH_ALL(e)
{
ReleaseFile(pFile, TRUE);
TRY
{
ReportSaveLoadException(lpszPathName, e,
TRUE, AFX_IDP_FAILED_TO_SAVE_DOC);
}
END_TRY
DELETE_EXCEPTION(e);
return FALSE;
}
END_CATCH_ALL
SetModifiedFlag(FALSE); // back to unmodified
return TRUE; // success
}
CDocument::OnFileSaveAs()
具体实现:调用NULL参数的DoSave(..)
void CDocument::OnFileSaveAs() { if (!DoSave(NULL)) TRACE0("Warning: File save-as failed./n"); }
The COleServerDoc::OnFileSaveCopyAs
CDocument::OnFileClose
void CDocument::OnFileClose() { //保存内容 if (!SaveModified()) return; //关闭文档 OnCloseDocument(); }
如果需要保存,调用DoFileSave(..)
BOOL CDocument::SaveModified()
{
//没有改动
if (!IsModified())
return TRUE;
//得到文件名
CString name;
if (m_strPathName.IsEmpty())
{
// get name based on caption
name = m_strTitle;
if (name.IsEmpty())
VERIFY(name.LoadString(AFX_IDS_UNTITLED));
}
else
{
// get name based on file title of path name
name = m_strPathName;
if (afxData.bMarked4)
{
AfxGetFileTitle(m_strPathName,
name.GetBuffer(_MAX_PATH), _MAX_PATH);
name.ReleaseBuffer();
}
}
CString prompt;
AfxFormatString1(prompt, AFX_IDP_ASK_TO_SAVE, name);
//对话框:文件已被改动,是否保存
switch (AfxMessageBox(prompt, MB_YESNOCANCEL, AFX_IDP_ASK_TO_SAVE))
{
case IDCANCEL:
return FALSE; // don't continue
case IDYES:
//保存文件
if (!DoFileSave())
return FALSE; // don't continue
break;
case IDNO:
// If not saving changes, revert the document
break;
default:
ASSERT(FALSE);
break;
}
return TRUE;
}
COleServerDoc::OnUpdateDocument
CWinApp::OnFilePrintSetup
CView::OnFilePrint
CView::OnFilePrintPreview
ID_EDIT_CLEAR
ID_EDIT_CLEAR_ALL
ID_EDIT_COPY
ID_EDIT_CUT
ID_EDIT_FIND
ID_EDIT_PASTE
ID_EDIT_PASTE_LINK
ID_EDIT_PASTE_SPECIAL
ID_EDIT_REPEAT
ID_EDIT_REPLACE
ID_EDIT_SELECT_ALL
ID_EDIT_UNDO
ID_EDIT_REDO
这些ID在对应的编辑视中有相应处理函数
CMDIFrameWnd::OnWindowNew
void CMDIFrameWnd::OnWindowNew() { //找到当前窗口,得到对应文档模板,使用文档模板的成员CreateNewFrame建立新窗口 CMDIChildWnd* pActiveChild = MDIGetActive(); CDocument* pDocument; if (pActiveChild == NULL ¦¦ (pDocument = pActiveChild->GetActiveDocument()) == NULL) { TRACE0("Warning: No active document for WindowNew command./n"); AfxMessageBox(AFX_IDP_COMMAND_FAILURE); return; // command failed } // otherwise we have a new frame ! CDocTemplate* pTemplate = pDocument->GetDocTemplate(); ASSERT_VALID(pTemplate); CFrameWnd* pFrame = pTemplate->CreateNewFrame(pDocument, pActiveChild); if (pFrame == NULL) { TRACE0("Warning: failed to create new frame./n"); return; // command failed } pTemplate->InitialUpdateFrame(pFrame, pDocument); }
ID_WINDOW_ARRANGE,以及ID_WINDOW_CASCADE、ID_WINDOW_TILE_HORZ、ID_WINDOW_TILE_VERT都由OnMDIWindowCmd()处理
OnMDIWindowCmd将相应消息发送给m_hWndMDIClient
BOOL CMDIFrameWnd::OnMDIWindowCmd(UINT nID)
{
ASSERT(m_hWndMDIClient != NULL);
UINT msg;
UINT wParam = 0;
switch (nID)
{
default:
return FALSE; // not for us
case ID_WINDOW_ARRANGE:
msg = WM_MDIICONARRANGE;
break;
case ID_WINDOW_CASCADE:
msg = WM_MDICASCADE;
break;
case ID_WINDOW_TILE_HORZ:
wParam = MDITILE_HORIZONTAL;
// fall through
case ID_WINDOW_TILE_VERT:
ASSERT(MDITILE_VERTICAL == 0);
msg = WM_MDITILE;
break;
}
::SendMessage(m_hWndMDIClient, msg, wParam, 0);
return TRUE;
}
参见ID_WINDOW_ARRANGE
参见ID_WINDOW_ARRANGE
参见ID_WINDOW_ARRANGE
CSplitterWnd::DoKeyboardSplit
用键盘控制分割窗口
BOOL CSplitterWnd::DoKeyboardSplit()
{
ASSERT_VALID(this);
int ht;
if (m_nRows > 1 && m_nCols > 1)
ht = splitterIntersection1; // split existing row+col
else if (m_nRows > 1)
ht = vSplitterBar1; // split existing row
else if (m_nCols > 1)
ht = hSplitterBar1; // split existing col
else if (m_nMaxRows > 1 && m_nMaxCols > 1)
ht = bothSplitterBox; // we can split both
else if (m_nMaxRows > 1)
ht = vSplitterBox; // we can split rows
else if (m_nMaxCols > 1)
ht = hSplitterBox; // we can split columns
else
return FALSE; // can't split
// start tracking
StartTracking(ht);
CRect rect;
rect.left = m_rectTracker.Width() / 2;
rect.top = m_rectTracker.Height() / 2;
if (m_ptTrackOffset.y != 0)
rect.top = m_rectTracker.top;
if (m_ptTrackOffset.x != 0)
rect.left = m_bTracking2 ? m_rectTracker2.left :m_rectTracker.left;
rect.OffsetRect(-m_ptTrackOffset.x, -m_ptTrackOffset.y);
ClientToScreen(&rect);
SetCursorPos(rect.left, rect.top);
return TRUE;
}
建立CWinApp::OnAppAbout();
CWinApp::OnAppExit
直接向主窗口发送WM_CLOSE消息
void CWinApp::OnAppExit() { // same as double-clicking on main window close box ASSERT(m_pMainWnd != NULL); m_pMainWnd->SendMessage(WM_CLOSE); }
CWinApp::OnHelpIndex
CWinApp::OnHelpUsing
CWinApp::OnContextHelp
CWinApp::OnHelp
CWinApp::OnHelpIndex
CSplitterWnd::OnNextPaneCmd
CSplitterWnd::OnNextPaneCmd
切换AFX_IDW_TOOLBAR工具条
消息映射
ON_UPDATE_COMMAND_UI(ID_VIEW_TOOLBAR, OnUpdateControlBarMenu)
ON_COMMAND_EX(ID_VIEW_TOOLBAR, OnBarCheck)
BOOL CFrameWnd::OnBarCheck(UINT nID)
{
ASSERT(ID_VIEW_STATUS_BAR == AFX_IDW_STATUS_BAR);
ASSERT(ID_VIEW_TOOLBAR == AFX_IDW_TOOLBAR);
ASSERT(ID_VIEW_REBAR == AFX_IDW_REBAR);
CControlBar* pBar = GetControlBar(nID);
if (pBar != NULL)
{
//设置该工具条状态
ShowControlBar(pBar, (pBar->GetStyle() & WS_VISIBLE) == 0, FALSE);
return TRUE;
}
return FALSE;
}
void CFrameWnd::OnUpdateControlBarMenu(CCmdUI* pCmdUI) { ASSERT(ID_VIEW_STATUS_BAR == AFX_IDW_STATUS_BAR); ASSERT(ID_VIEW_TOOLBAR == AFX_IDW_TOOLBAR); ASSERT(ID_VIEW_REBAR == AFX_IDW_REBAR); CControlBar* pBar = GetControlBar(pCmdUI->m_nID); if (pBar != NULL) { pCmdUI->SetCheck((pBar->GetStyle() & WS_VISIBLE) != 0); return; } pCmdUI->ContinueRouting(); }
实现方法与ID_VIEW_TOOLBAR相同
本文档介绍了MFC文档视图结构中的一些常见缺省命令处理,包括文件操作(新建、打开、保存等)、窗口管理(新建、排列、拆分)以及编辑控制和帮助功能。通过理解这些缺省处理,开发者可以更好地理解和定制MFC应用程序。
1122

被折叠的 条评论
为什么被折叠?



