65.9K
CodeProject 正在变化。 阅读更多。
Home

CFileDialogST v1.0

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.89/5 (17投票s)

2001年6月26日

3分钟阅读

viewsIcon

189902

downloadIcon

4891

一个使用API的CFileDialog实现。

Sample Image - CFileDialogST.jpg

摘要

CFileDialogST 是使用 SDK API 重新实现的 MFC CFileDialog 类。 第一个有价值的特性是能够显示新的 Windows 2000 打开/保存通用对话框! CFileDialogST 还包括一个可以轻松显示用于选择文件夹的通用对话框的函数。

该类支持 Unicode,并且与原始 MFC 实现完全兼容。 构造函数和函数具有相同的名称和参数列表,因此使用新的应该毫不费力。

CFileDialogST 函数

CFileDialogST(BOOL bOpenFileDialog, LPCTSTR lpszDefExt = NULL, LPCTSTR lpszFileName = NULL, DWORD dwFlags = OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, LPCTSTR lpszFilter = NULL, CWnd* pParentWnd = NULL)

构造一个 CFileDialogST 对象。 大多数常用的参数可以在参数列表中传递。

// Parameters:
//  [IN]    bOpenFileDialog
//           Set to TRUE to construct a File Open dialog box or
//           FALSE to construct a File Save As dialog box.
//  [IN]    lpszDefExt
//           The default filename extension.
//           If the user does not include an extension in the Filename edit box,
//           the extension specified by lpszDefExt is automatically appended
//           to the filename.
//           If this parameter is NULL, no file extension is appended.
//  [IN]    lpszFileName
//           The initial filename that appears in the filename edit box.
//           If NULL, no filename initially appears
//  [IN]    dwFlags
//           A combination of one or more flags that allow 
//           you to customize the dialog box.
//  [IN]    lpszFilter
//           A series of string pairs that specify filters you can apply to the file.
//           If you specify file filters, only selected files will appear in the
//           Files list box.
//  [IN]    pParentWnd
//           Pointer to the owner window for the dialog box. Can be NULL.
//
CFileDialogST(BOOL bOpenFileDialog, LPCTSTR lpszDefExt, 
  LPCTSTR lpszFileName, DWORD dwFlags, 
  LPCTSTR lpszFilter, CWnd* pParentWnd)

CFileDialogST()

构造一个 CFileDialogST 对象。 所有必需的参数都必须通过访问 m_ofnm_bOpenFileDialog 公共成员来手动初始化。

DoModal()

此函数显示文件选择对话框,并允许用户进行选择。 必须填写 m_ofn 公共结构的所有必需字段。 这可以使用类构造函数或直接访问该结构来完成。 此外,公共变量 m_bOpenFileDialog 必须设置为 TRUE 才能获得打开对话框,或设置为 FALSE 才能获得保存对话框。

// Return value:
//  IDOK
//    The user has selected a filename.
//  IDCANCEL
//    The user has closed the dialog without selecting any filename.
//
int DoModal()

CString GetPathName() const

此函数返回所选文件的完整路径。

// Return value:
//   A CString object containing the full path of the file.
//
CString GetPathName() const

CString GetFileName() const

此函数返回所选文件的文件名。

// Return value:
//   A CString object containing the name of the file.
//
CString GetFileName() const

CString GetFileTitle() const

此函数返回所选文件的标题。

// Return value:
//   A CString object containing the title of the file.
//
CString GetFileTitle() const

CString GetFileExt() const

此函数返回所选文件的扩展名。

// Return value:
//   A CString object containing the extension of the file.
//
CString GetFileExt() const

CString GetFileDir() const

此函数返回所选文件的目录(不带驱动器)。

// Return value:
//   A CString object containing the directory (without drive) of the file.
//
CString GetFileDir() const

CString GetFileDrive() const

此函数返回所选文件的驱动器。

// Return value:
//   A CString object containing the drive of the file.
//
CString GetFileDrive() const

POSITION GetStartPosition() const

此函数返回文件名列表的第一个元素的位置。

// Return value:
//   A POSITION value that can be used for iteration.
//   NULL if the list is empty.
//
POSITION GetStartPosition() const

CString GetNextPathName(POSITION& pos) const

此函数返回下一个所选文件的完整路径。

// Parameters:
//   [IN]    pos
//            A reference to a POSITION value 
//            returned by a previous GetNextPathName 
//            or GetStartPosition function call. 
//            NULL if the end of the list has been reached.
//
// Return value:
//   A CString object containing the full path of the file.
//
CString GetNextPathName(POSITION& pos) const

int SelectFolder(LPCTSTR lpszTitle = NULL, LPCTSTR lpszStartPath = NULL, UINT ulFlags = BIF_RETURNFSANCESTORS | BIF_RETURNONLYFSDIRS, CWnd* pParentWnd = NULL)

此函数允许用户选择一个文件夹。

// Parameters:
//   [IN]    lpszTitle
//             Address of a null-terminated string that is displayed above the 
//             tree view control in the dialog box. This string can be used to 
//             specify instructions to the user. Can be NULL.
//   [IN]    lpszStartPath
//             Address of a null-terminated string containing the initial folder
//             to open. Can be NULL.
//   [IN]    ulFlags
//             Flags specifying the options for the dialog box.
//   [IN]    pParentWnd
//             Pointer to the owner window for the dialog box. Can be NULL.
//
// Return value:
//   IDOK
//     The user has selected a folder and pressed OK. A call
//     to GetSelectedFolder() will return the selected folder.
//   IDCANCEL
//     The user has closed the dialog without selecting any folder.
//
int SelectFolder(LPCTSTR lpszTitle, LPCTSTR lpszStartPath, 
                               UINT ulFlags, CWnd* pParentWnd)

CString GetSelectedFolder() const

此函数返回用户通过调用 SelectFolder 选择的文件夹。

// Return value:
//    A CString object containing the selected folder.
//    Without a previous call to SelectFolder this string can be empty or
//    reflect the last selected folder.
//
CString GetSelectedFolder() const

示例

CFileDialogST 演示应用程序显示了如何打开文件(即使是多选)、如何请求文件名以保存以及如何浏览文件夹。

想在 DLL 中包含 CFileDialogST 吗?

CFileDialogST 已经准备好从 DLL 内部使用。 您需要从您的 DLL 导出 CFileDialogST。 在您的 DLL 项目中包含以下文件

  • FileDialogST.h
  • FileDialogST.cpp

将以下定义添加到您的 DLL 项目设置中

  • _CMLHTDLL_NOLIB_
  • _CMLHTDLL_BUILDDLL_

FileDialogST.h 中,注释掉以下行

#define _FILEDIALOGST_NODLL_

然后,根据您的 DLL 生成的 .lib 文件更新各种 #pragma comment(lib, "???")

备注

这种架构使得可以向该类添加其他功能。 例如,可以添加对选择计算机命令对话框的支持。 如果有人实现了新功能,我将很乐意将他的代码包含在下一个 CFileDialogST 演示应用程序中。

© . All rights reserved.