mfc屏蔽另存为对话框
2013-06-18 00:15
232 查看
来自:http://hi.baidu.com/gongziya/item/60a7f8523b0288d2d58bac1c
我写了一个推箱子的MFC单文档应用程序,在游戏中,每一个地图都对应一个文件,这样随着游戏的进行,读取文件会将保存游戏各种信息的CBOXDoc类中原有内容冲刷掉,这样,点击关闭退出游戏的时候,会弹出一个对话框,提示是否保存,很恼火,这必须要把该对话框屏蔽:
屏蔽工作是在CBOXDoc::CanCloseFrame(CFrameWnd* pFrame) 函数中实现的,切换到类向导,在CBOXDoc类右键,单击“ADD virtual Function"添加CanCloseFrame函数。编辑此函数如下:
BOOL CMyDoc::CanCloseFrame(CFrameWnd* pFrame)
{
SetModifiedFlag(FALSE);
return CDocument::CanCloseFrame(pFrame);
}
这样就不会弹出对话框了。。。
*************************************************************************************************************************************************
考虑到其他的情况,如果还是想将这个对话框屏蔽,但是又希望文档能够自动保存,上面的方法显然没有保存文档功能。
我们可以跟踪消息处理过程,当按下应用程序框架上右上角的关闭按钮(带小叉的按钮)时,将引发框架窗口的关闭消息,从而将调用框架类的CFrameWnd::OnClose()函数。
void CFrameWnd::OnClose()
{
if (m_lpfnCloseProc != NULL && !(*m_lpfnCloseProc)(this))
return; // Note: only queries the active document CDocument* pDocument = GetActiveDocument();
if (pDocument != NULL && !pDocument->CanCloseFrame(this))
{
// document can't close right now -- don't close it
return; }
// ... ...
}在得到当前的活动文档后,就调用文档类的CanCloseFrame(pFrame)函数。而这个函数继续调用基类的CDocument::CanCloseFrame(pFrame)函数。在这个函数里面最终调用CDocument:: SaveModified()函数。让我们仔细看看它的代码。
BOOL CDocument::SaveModified()
{ if (!IsModified()) //如果文档没有被修改过则不保存
return TRUE; // ok to continue
// get name/title of document CString name; if (m_strPathName.IsEmpty()) {// m_strPathName是当前文档的路径(包括文件名)。因为在打开应用程序的时候就已经打开了一个叫做machine.mi的文件,所以它不是空字串。 // 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 so, either Save or Update, as appropriate
if (!DoFileSave())
return FALSE; // don't continue
break; case IDNO:
// If not saving changes, revert the document
break; default: ASSERT(FALSE); break; } return TRUE; // keep going}从上面的代码可以看到,如果要作到关闭应用程序时自动地保存修改过的文档,则必须在进入CDocument::SaveModified()之前就消除修改标记。但是如果消除了修改标记文档又不会被存储,所以必须在消除修改标记之前保存文档。保存文档的函数将能够自动地消除修改标记。在哪里进行这个工作好呢?在文档类的CanCloseFrame()函数中。可以在这个函数中调用文档类的OnSaveDocument(lpszPathName)函数。调用这个函数需要给出文件路径和文件名称。
BOOL CMachineDoc::CanCloseFrame(CFrameWnd* pFrame)
{ if(IsModified()){ CFile outfile;
outfile.Open("machine.mi", CFile::modeCreate|CFile::modeReadWrite);
CString strPath = outfile.GetFilePath();
outfile.Close();
OnSaveDocument(strPath); // 这个函数能自动地消除文档修改标记
} return CDocument::CanCloseFrame(pFrame);}
我写了一个推箱子的MFC单文档应用程序,在游戏中,每一个地图都对应一个文件,这样随着游戏的进行,读取文件会将保存游戏各种信息的CBOXDoc类中原有内容冲刷掉,这样,点击关闭退出游戏的时候,会弹出一个对话框,提示是否保存,很恼火,这必须要把该对话框屏蔽:
屏蔽工作是在CBOXDoc::CanCloseFrame(CFrameWnd* pFrame) 函数中实现的,切换到类向导,在CBOXDoc类右键,单击“ADD virtual Function"添加CanCloseFrame函数。编辑此函数如下:
BOOL CMyDoc::CanCloseFrame(CFrameWnd* pFrame)
{
SetModifiedFlag(FALSE);
return CDocument::CanCloseFrame(pFrame);
}
这样就不会弹出对话框了。。。
*************************************************************************************************************************************************
考虑到其他的情况,如果还是想将这个对话框屏蔽,但是又希望文档能够自动保存,上面的方法显然没有保存文档功能。
我们可以跟踪消息处理过程,当按下应用程序框架上右上角的关闭按钮(带小叉的按钮)时,将引发框架窗口的关闭消息,从而将调用框架类的CFrameWnd::OnClose()函数。
void CFrameWnd::OnClose()
{
if (m_lpfnCloseProc != NULL && !(*m_lpfnCloseProc)(this))
return; // Note: only queries the active document CDocument* pDocument = GetActiveDocument();
if (pDocument != NULL && !pDocument->CanCloseFrame(this))
{
// document can't close right now -- don't close it
return; }
// ... ...
}在得到当前的活动文档后,就调用文档类的CanCloseFrame(pFrame)函数。而这个函数继续调用基类的CDocument::CanCloseFrame(pFrame)函数。在这个函数里面最终调用CDocument:: SaveModified()函数。让我们仔细看看它的代码。
BOOL CDocument::SaveModified()
{ if (!IsModified()) //如果文档没有被修改过则不保存
return TRUE; // ok to continue
// get name/title of document CString name; if (m_strPathName.IsEmpty()) {// m_strPathName是当前文档的路径(包括文件名)。因为在打开应用程序的时候就已经打开了一个叫做machine.mi的文件,所以它不是空字串。 // 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 so, either Save or Update, as appropriate
if (!DoFileSave())
return FALSE; // don't continue
break; case IDNO:
// If not saving changes, revert the document
break; default: ASSERT(FALSE); break; } return TRUE; // keep going}从上面的代码可以看到,如果要作到关闭应用程序时自动地保存修改过的文档,则必须在进入CDocument::SaveModified()之前就消除修改标记。但是如果消除了修改标记文档又不会被存储,所以必须在消除修改标记之前保存文档。保存文档的函数将能够自动地消除修改标记。在哪里进行这个工作好呢?在文档类的CanCloseFrame()函数中。可以在这个函数中调用文档类的OnSaveDocument(lpszPathName)函数。调用这个函数需要给出文件路径和文件名称。
BOOL CMachineDoc::CanCloseFrame(CFrameWnd* pFrame)
{ if(IsModified()){ CFile outfile;
outfile.Open("machine.mi", CFile::modeCreate|CFile::modeReadWrite);
CString strPath = outfile.GetFilePath();
outfile.Close();
OnSaveDocument(strPath); // 这个函数能自动地消除文档修改标记
} return CDocument::CanCloseFrame(pFrame);}
相关文章推荐
- MFC对话框屏蔽回车键
- VS2010 MFC中屏蔽ESC和ENTER键关闭对话框的方法
- MFC对话框程序 屏蔽ESC和ENTER键关闭对话框的方法
- MFC对话框程序 屏蔽ESC和ENTER键关闭对话框的方法
- MFC中关于打开和另存为对话框中过滤器的编程
- MFC对话框屏蔽Enter和ESC键
- MFC对话框程序屏蔽回车与ESC键
- MFC对话框程序屏蔽回车与ESC键
- MFC屏蔽ESC和回车键退出对话框的消息
- VS2010 MFC中屏蔽ESC和ENTER键关闭对话框的方法
- MFC对话框屏蔽Enter和ESC键
- MFC对话框程序屏蔽ESC和ENTER键退出
- MFC中屏蔽ESC和回车关闭对话框
- MFC对话框程序 屏蔽ESC和ENTER键关闭对话框的方法
- MFC中屏蔽ESC和回车关闭对话框
- MFC对话框程序屏蔽回车与ESC键
- VC MFC 屏蔽ESC和ENTER键关闭对话框
- MFC中对话框窗体屏蔽ESC键
- MFC对话框编辑框屏蔽或修改对回车的响应
- MFC对话框程序屏蔽ESC和ENTER键退出