BuildFilePath 及打开文件对话框

时间:2024-11-10 15:37:20

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

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

 /**
* Copyright (C) 2014-2050
* All rights reserved.
*
* @file FileHelper.h
* @brief
* @version v1.0
* @author SOUI group
* @date 2014/08/03
*
* Describe 实现两个文件相关的helper
*/ #pragma once /**
* BuildFilePath
* @brief 递归创建子目录
* @param LPCTSTR pszCurPath -- 创建路径的起始位置
* @param LPCTSTR pszNewPath -- 新路径
* @param BOOL bPath -- pszNewPath是一个路径标识
* @return BOOL -- true创建成功
* Describe pszNewPath指向一个文件名时,只创建文件路径部分
*/
inline BOOL BuildFilePath(LPCTSTR pszCurPath,LPCTSTR pszNewPath,BOOL bPath=TRUE)
{
TCHAR szCurDir[MAX_PATH+];
GetCurrentDirectory(MAX_PATH,szCurDir);
if(!SetCurrentDirectory(pszCurPath)) return FALSE;
TCHAR szNewPath[MAX_PATH+];
_tcscpy_s(szNewPath,_countof(szNewPath),pszNewPath);
if(bPath)
{
int nLen=_tcslen(szNewPath);
if(szNewPath[nLen-]!=_T('\\'))
_tcscat(szNewPath,_T("\\"));
}
LPTSTR pszPath=_tcschr(szNewPath,_T('\\'));
while(pszPath)
{
*pszPath=;
if(!CreateDirectory(szNewPath,NULL)) return FALSE;
*pszPath=_T('\\');
pszPath=_tcschr(pszPath+,_T('\\'));
}
SetCurrentDirectory(szCurDir);
return TRUE;
} class CFileDialogEx
{
public: OPENFILENAME m_ofn;
BOOL m_bOpenFileDialog; // TRUE for file open, FALSE for file save
TCHAR m_szFileTitle[_MAX_FNAME]; // contains file title after return
TCHAR m_szFileName[_MAX_PATH]; // contains full path name after return CFileDialogEx(BOOL bOpenFileDialog, // TRUE for FileOpen, FALSE for FileSaveAs
LPCTSTR lpszDefExt = NULL,
LPCTSTR lpszFileName = NULL,
DWORD dwFlags = OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
LPCTSTR lpszFilter = NULL,
HWND hWndParent = NULL)
{
memset(&m_ofn, , sizeof(m_ofn)); // initialize structure to 0/NULL
m_szFileName[] = _T('\0');
m_szFileTitle[] = _T('\0'); m_bOpenFileDialog = bOpenFileDialog;
m_ofn.lStructSize = sizeof(m_ofn);
m_ofn.lpstrFile = m_szFileName;
m_ofn.nMaxFile = _MAX_PATH;
m_ofn.lpstrDefExt = lpszDefExt;
m_ofn.lpstrFileTitle = (LPTSTR)m_szFileTitle;
m_ofn.nMaxFileTitle = _MAX_FNAME;
m_ofn.Flags = dwFlags | OFN_EXPLORER | OFN_ENABLEHOOK | OFN_ENABLESIZING;
m_ofn.lpstrFilter = lpszFilter;
m_ofn.hwndOwner = hWndParent; // setup initial file name
if(lpszFileName != NULL)
_tcscpy_s(m_szFileName, _countof(m_szFileName), lpszFileName);
} INT_PTR DoModal(HWND hWndParent = ::GetActiveWindow())
{
if(m_ofn.hwndOwner == NULL) // set only if not specified before
m_ofn.hwndOwner = hWndParent; if(m_bOpenFileDialog)
return ::GetOpenFileName(&m_ofn);
else
return ::GetSaveFileName(&m_ofn);
}
};

看上去还不错。

相关文章