Chinaunix首页 | 论坛 | 博客
  • 博客访问: 194885
  • 博文数量: 90
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 0
  • 用 户 组: 普通用户
  • 注册时间: 2017-08-23 16:48
文章分类

全部博文(90)

文章存档

2015年(1)

2011年(21)

2010年(59)

2009年(9)

我的朋友

分类: C/C++

2010-06-08 14:26:27

文档视图结构中,缺省的命令处理
在文档视图结构所构建的框架中,很多命令ID都有缺省的命令处理。很多功能都由他们完成,但这些功能在程序中不“可见”,不便于对程序的理解。
以下归纳了常见的缺省处理及其流程
在需要时候,可以重载这些函数以实现特定功能
1.ID_FILE_NEW
2.ID_FILE_OPEN
3.ID_FILE_SAVE
4.ID_FILE_SAVE_AS
5.ID_FILE_SAVE_COPY_AS
6.ID_FILE_CLOSE
7.ID_FILE_UPDATE
8.ID_FILE_PRINT_SETUP
9.ID_FILE_PRINT
10.ID_FILE_PRINT_PREVIEW
11.缺省编辑控制ID
12.ID_WINDOW_NEW
13.ID_WINDOW_ARRANGE
14.ID_WINDOW_CASCADE
15.ID_WINDOW_TILE_HORZ
16.ID_WINDOW_TILE_VERT
17.ID_WINDOW_SPLIT
18.ID_APP_ABOUT
19.ID_APP_EXIT
20.ID_HELP_INDEX
21.ID_HELP_USING
22.ID_CONTEXT_HELP
23.ID_HELP
24.ID_DEFAULT_HELP
25.ID_NEXT_PANE
26.ID_PREV_PANE
27.ID_OLE_INSERT_NEW
28.ID_OLE_EDIT_LINKS
29.ID_VIEW_TOOLBAR
30.ID_VIEW_STATUS_BAR

1.ID_FILE_NEW
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
}
2.ID_FILE_OPEN
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
}
3.ID_FILE_SAVE
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
}
4.ID_FILE_SAVE_AS
CDocument::OnFileSaveAs()
具体实现:调用NULL参数的DoSave(..)
void CDocument::OnFileSaveAs()
{
if (!DoSave(NULL))
TRACE0("Warning: File save-as failed.\n");
}
5.ID_FILE_SAVE_COPY_AS
The COleServerDoc::OnFileSaveCopyAs
6.ID_FILE_CLOSE
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;
}
7.ID_FILE_UPDATE
COleServerDoc::OnUpdateDocument
8.ID_FILE_PRINT_SETUP
CWinApp::OnFilePrintSetup
9.ID_FILE_PRINT
CView::OnFilePrint
10.ID_FILE_PRINT_PREVIEW
CView::OnFilePrintPreview
11.缺省编辑控制ID
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在对应的编辑视中有相应处理函数
12.ID_WINDOW_NEW
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);
}
13.ID_WINDOW_ARRANGE
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;
}
14.ID_WINDOW_CASCADE
参见ID_WINDOW_ARRANGE
15.ID_WINDOW_TILE_HORZ
参见ID_WINDOW_ARRANGE
16.ID_WINDOW_TILE_VERT
参见ID_WINDOW_ARRANGE
17.ID_WINDOW_SPLIT
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;
}
18.ID_APP_ABOUT
建立CWinApp::OnAppAbout();
19.ID_APP_EXIT
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);
}
20.ID_HELP_INDEX
CWinApp::OnHelpIndex
21.ID_HELP_USING
CWinApp::OnHelpUsing
22.ID_CONTEXT_HELP
CWinApp::OnContextHelp
23.ID_HELP
CWinApp::OnHelp
24.ID_DEFAULT_HELP
CWinApp::OnHelpIndex
25.ID_NEXT_PANE
CSplitterWnd::OnNextPaneCmd
26.ID_PREV_PANE
CSplitterWnd::OnNextPaneCmd
27.ID_OLE_INSERT_NEW
28.ID_OLE_EDIT_LINKS
29.ID_VIEW_TOOLBAR
切换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();
}
30.ID_VIEW_STATUS_BAR
实现方法与ID_VIEW_TOOLBAR相同
 
阅读(1608) | 评论(0) | 转发(0) |
0

上一篇:对象导言(C++编程思想)

下一篇:RichEdit

给主人留下些什么吧!~~