您现在的位置是: 软件 > 开发者网络 > 程序方舟 > 开发专栏 > Visual C++开发 > 正文
·速成电脑精英(包分配)白领高薪一族从这里开始



-Java套接字编程(下)
-MediaStudio Pro 6.5教程
-三款卸载软件最新试用
-基于Visual C++的Winsock API研究

MFC文件浏览程序中改变文件读写对话框样式
2002-03-28· ·贾旭滨(译) ··

1 2  下一页

  在我告诉大家怎么样修改MFC浏览程序中文件读写对话框的缺省设置之前呢,我们应该先知道MFC是怎么样显示文件打开和文件保存对话框的。如果你选择了文件菜单中的打开,那么就会有一个消息发送给CWinApp::OnFileOpen,通过一个成员变量m_pDocManager(一个指向CDocManager 对象的指针)来调用CDocManager::OnFileOpen,那么函数就会调用CDocManager的虚函数DoPromptFileName,成功后再调用CWinApp::OpenDocumentFile函数,这个文件对话框就是在DoPromptFileName的虚函数中显示的。当我们打开的是保存对话框时,文件保存(或另存为)命令消息就会发给CDocument::OnFileSave (or CDocument::OnFileSaveAs),在这2种情况下,CDocument::DoSave函数都会被调用。最后,如果文件名是空的,那么CDocument::DoSave就会调用CWinApp::DoPromptFileName,使得成员变量m_pDocManager有效,并且调用CDocManager::DoPromptFileName来显示保存对话框。

  那么现在大家都明白了,CDocManager::DoPromptFileName函数(注意它是一个虚函数)就是负责显示标准的文件打开和保存对话框的(可是有一个BOOL变量来决定显示哪一个对话框的)。

  现在看起来,这好象对改变默认的对话框设置没什么用。你可能也没有考虑过在类CDocManager中的DoPromptFileName函数,或者不知道怎么改,来使用你自己的CDocManager类。那么你不用急,下面的代码就是告诉你怎么自定义CDocManager。

// CDocManager class declaration
//

class CDocManagerEx : public CDocManager
{
  DECLARE_DYNAMIC(CDocManagerEx)

// Construction
public:
  CDocManagerEx();

// Attributes
public:

// Operations
public:

// Overrides
  // helper for standard commdlg dialogs
  virtual BOOL DoPromptFileName(CString& fileName, UINT nIDSTitle,
      DWORD lFlags, BOOL bOpenFileDialog, CDocTemplate* pTemplate);

// Implementation
public:
  virtual ~CDocManagerEx();
};


// DocManager.cpp : implementation file
//

#include "stdafx.h"
#include "PreviewFileDlg.h"
#include "DocManager.h" // the header with the class declaration

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

static void AppendFilterSuffix(CString& filter, OPENFILENAME& ofn,
  CDocTemplate* pTemplate, CString* pstrDefaultExt)
{
  ASSERT_VALID(pTemplate);
  ASSERT_KINDOF(CDocTemplate, pTemplate);

  CString strFilterExt, strFilterName;
  if (pTemplate->GetDocString(strFilterExt, CDocTemplate::filterExt) &&
  !strFilterExt.IsEmpty() &&
  pTemplate->GetDocString(strFilterName, CDocTemplate::filterName) &&
  !strFilterName.IsEmpty())
  {
    // a file based document template - add to filter list
#ifndef _MAC
    ASSERT(strFilterExt[0] == '.');
#endif
    if (pstrDefaultExt != NULL)
    {
      // set the default extension
#ifndef _MAC
      *pstrDefaultExt = ((LPCTSTR)strFilterExt) + 1; // skip the '.'
#else
      *pstrDefaultExt = strFilterExt;
#endif
      ofn.lpstrDefExt = (LPTSTR)(LPCTSTR)(*pstrDefaultExt);
      ofn.nFilterIndex = ofn.nMaxCustFilter + 1; // 1 based number
    }

    // add to filter
    filter += strFilterName;
    ASSERT(!filter.IsEmpty()); // must have a file type name
    filter += (TCHAR)'\0'; // next string please
#ifndef _MAC
    filter += (TCHAR)'*';
#endif
    filter += strFilterExt;
    filter += (TCHAR)'\0'; // next string please
    ofn.nMaxCustFilter++;
  }
}

/////////////////////////////////////////////////////////////////////////////
// CDocManagerEx

IMPLEMENT_DYNAMIC(CDocManagerEx, CDocManager)

CDocManagerEx::CDocManagerEx()
{
}

CDocManagerEx::~CDocManagerEx()
{
}

BOOL CDocManagerEx::DoPromptFileName(CString& fileName, UINT nIDSTitle, DWORD lFlags, BOOL bOpenFileDialog, CDocTemplate* pTemplate)
{
  CPreviewFileDlg dlgFile(bOpenFileDialog); // this is the only modified line!

  CString title;
  VERIFY(title.LoadString(nIDSTitle));

  dlgFile.m_ofn.Flags |= lFlags;

  CString strFilter;
  CString strDefault;
  if (pTemplate != NULL)
  {
    ASSERT_VALID(pTemplate);
    AppendFilterSuffix(strFilter, dlgFile.m_ofn, pTemplate, &strDefault);
  }
  else
  {
    // do for all doc template
    POSITION pos = m_templateList.GetHeadPosition();
    BOOL bFirst = TRUE;
    while (pos != NULL)
    {
      CDocTemplate* pTemplate = (CDocTemplate*)m_templateList.GetNext(pos);
      AppendFilterSuffix(strFilter, dlgFile.m_ofn, pTemplate,
        bFirst ? &strDefault : NULL);
      bFirst = FALSE;
    }
  }

  // append the "*.*" all files filter
  CString allFilter;
  VERIFY(allFilter.LoadString(AFX_IDS_ALLFILTER));
  strFilter += allFilter;
  strFilter += (TCHAR)'\0'; // next string please
#ifndef _MAC
  strFilter += _T("*.*");
#else
  strFilter += _T("****");
#endif
  strFilter += (TCHAR)'\0'; // last string
  dlgFile.m_ofn.nMaxCustFilter++;

  dlgFile.m_ofn.lpstrFilter = strFilter;
#ifndef _MAC
  dlgFile.m_ofn.lpstrTitle = title;
#else
  dlgFile.m_ofn.lpstrPrompt = title;
#endif
  dlgFile.m_ofn.lpstrFile = fileName.GetBuffer(_MAX_PATH);

  BOOL bResult = dlgFile.DoModal() == IDOK ? TRUE : FALSE;
  fileName.ReleaseBuffer();
  return bResult;
}

  以上代码是从MFC原代码中完整的拷贝过来的,当中只有一行需要改:对话框的声明(当然,由于这是CFileDialog的子类,你可以有更多的修改权),而且对于标准对话框来说,还应该有预览功能。AppendFilterSuffix函数是从DoPromptFileName中被调用的,原代码可以从你的工程中获得。如果你想显示打开和保存对话框,你可以使用bOpenFileDialog参数,TRUE表示对话框是打开对话框,反之,亦然。

1 2  下一页

■ 相关内容
 COM中的可连接对象与连接点机制及其MFC程序实现
 在VC++6.0中用MFC进行COM编程
 MFC中自由使用自定义消息
 为MFC和ATL控件创建签署的CAB文件
 MFC编程特点
 应用MFC开发高级应用程序
 MFC中所提供的各种视类介绍
 MFC中常用类,宏,函数介绍
 利用MFC进行开发的通用方法介绍
 利用Visual C++/MFC开发Windows程序
 Visual C++/MFC入门教程
 MAP原理及其在MFC中的实现
 理解Visual C++.NET定制MFC应用程序
 MFC中多线程的应用
 MFC简单打印方法
感谢 访问天极网,如果您觉得该文章涉及版权问题,请看这里!