BuildFilePath 及打开文件对话框

也许以后就主要在这里发SOUI的介绍了。

贴一段文件相关的helper, 测试一下贴代码是不是方便。

 1 /**

 2 * Copyright (C) 2014-2050 

 3 * All rights reserved.

 4 * 

 5 * @file       FileHelper.h

 6 * @brief      

 7 * @version    v1.0      

 8 * @author     SOUI group   

 9 * @date       2014/08/03

10 * 

11 * Describe    实现两个文件相关的helper

12 */

13 

14 #pragma once

15 

16 

17 /**

18 * BuildFilePath

19 * @brief    递归创建子目录

20 * @param    LPCTSTR pszCurPath --  创建路径的起始位置

21 * @param    LPCTSTR pszNewPath --  新路径

22 * @param    BOOL bPath --  pszNewPath是一个路径标识

23 * @return   BOOL -- true创建成功

24 * Describe  pszNewPath指向一个文件名时,只创建文件路径部分

25 */    

26 inline BOOL BuildFilePath(LPCTSTR pszCurPath,LPCTSTR pszNewPath,BOOL bPath=TRUE)

27 {

28     TCHAR szCurDir[MAX_PATH+1];

29     GetCurrentDirectory(MAX_PATH,szCurDir);

30     if(!SetCurrentDirectory(pszCurPath)) return FALSE;

31     TCHAR szNewPath[MAX_PATH+1];

32     _tcscpy_s(szNewPath,_countof(szNewPath),pszNewPath);

33     if(bPath)

34     {

35         int nLen=_tcslen(szNewPath);

36         if(szNewPath[nLen-1]!=_T('\\'))

37             _tcscat(szNewPath,_T("\\"));

38     }

39     LPTSTR pszPath=_tcschr(szNewPath,_T('\\'));

40     while(pszPath)

41     {

42         *pszPath=0;

43         if(!CreateDirectory(szNewPath,NULL)) return FALSE;

44         *pszPath=_T('\\');

45         pszPath=_tcschr(pszPath+1,_T('\\'));

46     }

47     SetCurrentDirectory(szCurDir);

48     return TRUE;

49 }

50 

51 class CFileDialogEx

52 {

53 public:

54 

55     OPENFILENAME m_ofn;

56     BOOL m_bOpenFileDialog;            // TRUE for file open, FALSE for file save

57     TCHAR m_szFileTitle[_MAX_FNAME];   // contains file title after return

58     TCHAR m_szFileName[_MAX_PATH];     // contains full path name after return

59 

60     CFileDialogEx(BOOL bOpenFileDialog, // TRUE for FileOpen, FALSE for FileSaveAs

61         LPCTSTR lpszDefExt = NULL,

62         LPCTSTR lpszFileName = NULL,

63         DWORD dwFlags = OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,

64         LPCTSTR lpszFilter = NULL,

65         HWND hWndParent = NULL)

66     {

67         memset(&m_ofn, 0, sizeof(m_ofn)); // initialize structure to 0/NULL

68         m_szFileName[0] = _T('\0');

69         m_szFileTitle[0] = _T('\0');

70 

71         m_bOpenFileDialog = bOpenFileDialog;

72         m_ofn.lStructSize = sizeof(m_ofn);

73         m_ofn.lpstrFile = m_szFileName;

74         m_ofn.nMaxFile = _MAX_PATH;

75         m_ofn.lpstrDefExt = lpszDefExt;

76         m_ofn.lpstrFileTitle = (LPTSTR)m_szFileTitle;

77         m_ofn.nMaxFileTitle = _MAX_FNAME;

78         m_ofn.Flags = dwFlags | OFN_EXPLORER | OFN_ENABLEHOOK | OFN_ENABLESIZING;

79         m_ofn.lpstrFilter = lpszFilter;

80         m_ofn.hwndOwner = hWndParent;

81 

82         // setup initial file name

83         if(lpszFileName != NULL)

84             _tcscpy_s(m_szFileName, _countof(m_szFileName), lpszFileName);

85     }

86 

87     INT_PTR DoModal(HWND hWndParent = ::GetActiveWindow())

88     {

89         if(m_ofn.hwndOwner == NULL)   // set only if not specified before

90             m_ofn.hwndOwner = hWndParent;

91 

92         if(m_bOpenFileDialog)

93             return ::GetOpenFileName(&m_ofn);

94         else

95             return ::GetSaveFileName(&m_ofn);

96     }

97 };

看上去还不错。

你可能感兴趣的:(FilePath)