MFC笔记--单文档程序的串行化

本文详细介绍了MFC框架中文档模板(CDocTemplate)的工作原理及其在文件打开过程中的作用,包括如何设置文档标题、文件类型等,并跟踪了从应用程序到文档类的文件打开流程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

GetDocString()

这个函数查找ID ==IDR_MAINFRAME string table当中的7个字符串。

CDocTemplate::windowTitle 主窗口标题,只出现在SDI程序。
CDocTemplate::docName 文档名称,设置成空的话====无标题
CDocTemplate::fileNewName 文件->新建 命令下的文档名称,会出现在文件对话框中
CDocTemplate::filterName 出现在文件->打开对话框 ,描述程序作者想筛选的文档类型,比如:文本文件(*.txt)
CDocTemplate::filterExt 文件对话框的扩展名,用于过滤文件,要配合 filterName使用,例子:.txt
CDocTemplate::regFileTypeId Identifier for the document type to be stored in the registration database maintained by Windows. This string is for internal use only (for example, “ExcelWorksheet”). If not specified, the document type cannot be registered with the Windows File Manager.
CDocTemplate::regFileTypeName Name of the document type to be stored in the registration database. This string may be displayed in dialog boxes of applications that access the registration database (for example, “Microsoft Excel Worksheet”).

文件->打开 的流程

1)CWinApp类首先响应

/*********
appdlg.cpp
*********/
void CWinApp::OnFileOpen()
{
    ENSURE(m_pDocManager != NULL);
    m_pDocManager->OnFileOpen();
}

2)转到CDocManger
弹出打开文件对话框并获取 filename

/**********
docmgr.cpp
***********/
void CDocManager::OnFileOpen()
{

    CString newName;
    if (!DoPromptFileName(newName, AFX_IDS_OPENFILE,
      OFN_HIDEREADONLY | OFN_FILEMUSTEXIST, TRUE, NULL))
        return;   //调用这个函数弹出打开文件对话框   用newName保存用户选择的文件名

    AfxGetApp()->OpenDocumentFile(newName);

}

3)转到CWinApp类

/****
appui.cpp
***/
CDocument* CWinApp::OpenDocumentFile(LPCTSTR lpszFileName)
{
    ENSURE_VALID(m_pDocManager);
    return m_pDocManager->OpenDocumentFile(lpszFileName);
}

4)转到CDocManager类


CDocument* CDocManager::OpenDocumentFile(LPCTSTR lpszFileName)
{
    return OpenDocumentFile(lpszFileName, TRUE);
}

CDocument* CDocManager::OpenDocumentFile(LPCTSTR lpszFileName, BOOL bAddToMRU)
{
    if (lpszFileName == NULL)
    {
        AfxThrowInvalidArgException();
    }
    /*........省略............*/


    return pBestTemplate->OpenDocumentFile(szPath, bAddToMRU, TRUE);//跳转到文档模板类
}

5)转到文档模板类

CDocument* CSingleDocTemplate::OpenDocumentFile(LPCTSTR lpszPathName, BOOL bAddToMRU, BOOL bMakeVisible)
{
/********省略**********/

pDocument->OnOpenDocument(lpszPathName)
}

6)转到文档类 pDocument->OnOpenDocument(lpszPathName)

/**** doccore.cpp  *****/
//在调用这个函数前我们有机会去自定义  文件对话框  然后传递LPCTSTR lpszPathName
BOOL CDocument::OnOpenDocument(LPCTSTR lpszPathName)
{

    CFile* pFile = GetFile(lpszPathName,
        CFile::modeRead|CFile::shareDenyWrite, pfe);

    DeleteContents();//打开新文档,释放旧文档的内存空间 

    CArchive loadArchive(pFile, CArchive::load | CArchive::bNoFlushOnDelete);
    loadArchive.m_pDocument = this;
    loadArchive.m_bForceFlat = FALSE;


    CWaitCursor wait;
    if (pFile->GetLength() != 0)
        Serialize(loadArchive);     // 调用CDocument类的Serialize()函数
    loadArchive.Close();
    ReleaseFile(pFile, FALSE);

    SetModifiedFlag(FALSE);     // start off with unmodified

    return TRUE;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值