📄 filedialogst.cpp
字号:
#include "stdafx.h"
#include "FileDialogST.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
CFileDialogST::CFileDialogST(BOOL bOpenFileDialog, LPCTSTR lpszDefExt, LPCTSTR lpszFileName, DWORD dwFlags, LPCTSTR lpszFilter, CWnd* pParentWnd)
{
::ZeroMemory(&m_ofn, sizeof(m_ofn));
::ZeroMemory(&m_szFile, sizeof(m_szFile));
::ZeroMemory(&m_szFileTitle, sizeof(m_szFileTitle));
::ZeroMemory(&m_szSelectedFolder, sizeof(m_szSelectedFolder));
// Store parameters
m_bOpenFileDialog = bOpenFileDialog;
m_ofn.lpstrDefExt = lpszDefExt;
if (lpszFileName != NULL)
{
m_ofn.lpstrFile = (LPTSTR)lpszFileName;
}
else
{
m_ofn.lpstrFile = m_szFile;
m_ofn.nMaxFile = MAX_PATH;
} // else
m_ofn.lpstrFileTitle = m_szFileTitle;
m_ofn.nMaxFileTitle = MAX_PATH;
m_ofn.Flags = dwFlags | OFN_EXPLORER;
m_ofn.lpstrFilter = lpszFilter;
if (pParentWnd != NULL)
{
m_ofn.hwndOwner = pParentWnd->m_hWnd;
}
}
CFileDialogST::CFileDialogST()
{
::ZeroMemory(&m_ofn, sizeof(m_ofn));
::ZeroMemory(&m_szFile, sizeof(m_szFile));
::ZeroMemory(&m_szFileTitle, sizeof(m_szFileTitle));
::ZeroMemory(&m_szSelectedFolder, sizeof(m_szSelectedFolder));
m_bOpenFileDialog = TRUE;
}
CFileDialogST::~CFileDialogST()
{
}
int CFileDialogST::DoModal()
{
BOOL bRetValue;
DWORD dwWinMajor;
dwWinMajor = (DWORD)(LOBYTE(LOWORD(::GetVersion())));
if (dwWinMajor >= 5)
{
m_ofn.lStructSize = sizeof(m_ofn);
}
else
{
m_ofn.lStructSize = sizeof(OPENFILENAME);
}
// Execute dialog
if (m_bOpenFileDialog)
{
bRetValue = ::GetOpenFileName(&m_ofn);
}
else
{
bRetValue = ::GetSaveFileName(&m_ofn);
}
return (bRetValue ? IDOK : IDCANCEL);
}
CString CFileDialogST::GetPathName() const
{
return m_ofn.lpstrFile;
}
CString CFileDialogST::GetFileName() const
{
return m_ofn.lpstrFileTitle;
}
CString CFileDialogST::GetFileTitle() const
{
TCHAR szTitle[MAX_PATH];
// Split path into components
_tsplitpath(m_ofn.lpstrFile, NULL, NULL, szTitle, NULL);
return szTitle;
}
CString CFileDialogST::GetFileExt() const
{
TCHAR szExt[MAX_PATH];
// Split path into components
_tsplitpath(m_ofn.lpstrFile, NULL, NULL, NULL, szExt);
return szExt;
}
CString CFileDialogST::GetFileDir() const
{
TCHAR szDrive[MAX_PATH];
TCHAR szDir[MAX_PATH];
// Split path into components
_tsplitpath(m_ofn.lpstrFile, szDrive, szDir, NULL, NULL);
::lstrcat(szDrive, szDir);
return szDrive;
}
CString CFileDialogST::GetFileDrive() const
{
TCHAR szDrive[MAX_PATH];
// Split path into components
_tsplitpath(m_ofn.lpstrFile, szDrive, NULL, NULL, NULL);
return szDrive;
}
POSITION CFileDialogST::GetStartPosition() const
{
return (POSITION)m_ofn.lpstrFile;
}
CString CFileDialogST::GetNextPathName(POSITION& pos) const
{
BOOL bExplorer = m_ofn.Flags & OFN_EXPLORER;
TCHAR chDelimiter;
if (bExplorer)
chDelimiter = '\0';
else
chDelimiter = ' ';
LPTSTR lpsz = (LPTSTR)pos;
if (lpsz == m_ofn.lpstrFile) // first time
{
if ((m_ofn.Flags & OFN_ALLOWMULTISELECT) == 0)
{
pos = NULL;
return m_ofn.lpstrFile;
}
// find char pos after first Delimiter
while(*lpsz != chDelimiter && *lpsz != '\0')
lpsz = _tcsinc(lpsz);
lpsz = _tcsinc(lpsz);
// if single selection then return only selection
if (*lpsz == 0)
{
pos = NULL;
return m_ofn.lpstrFile;
}
}
CString strPath = m_ofn.lpstrFile;
if (!bExplorer)
{
LPTSTR lpszPath = m_ofn.lpstrFile;
while(*lpszPath != chDelimiter)
lpszPath = _tcsinc(lpszPath);
strPath = strPath.Left(lpszPath - m_ofn.lpstrFile);
}
LPTSTR lpszFileName = lpsz;
CString strFileName = lpsz;
// find char pos at next Delimiter
while(*lpsz != chDelimiter && *lpsz != '\0')
lpsz = _tcsinc(lpsz);
if (!bExplorer && *lpsz == '\0')
{
pos = NULL;
}
else
{
if (!bExplorer)
{
strFileName = strFileName.Left(lpsz - lpszFileName);
}
lpsz = _tcsinc(lpsz);
if (*lpsz == '\0') // if double terminated then done
{
pos = NULL;
}
else
{
pos = (POSITION)lpsz;
}
}
// only add '\\' if it is needed
if (!strPath.IsEmpty())
{
// check for last back-slash or forward slash (handles DBCS)
LPCTSTR lpsz = _tcsrchr(strPath, '\\');
if (lpsz == NULL)
lpsz = _tcsrchr(strPath, '/');
// if it is also the last character, then we don't need an extra
if (lpsz != NULL &&
(lpsz - (LPCTSTR)strPath) == strPath.GetLength()-1)
{
ASSERT(*lpsz == '\\' || *lpsz == '/');
return strPath + strFileName;
}
}
return strPath + '\\' + strFileName;
}
int __stdcall CFileDialogST::BrowseCtrlCallback(HWND hwnd, UINT uMsg, LPARAM lParam, LPARAM lpData)
{
if (uMsg == BFFM_INITIALIZED && lpData != NULL)
{
::SendMessage(hwnd, BFFM_SETSELECTION, TRUE, lpData);
}
else // uMsg == BFFM_SELCHANGED
{
}
return 0;
}
int CFileDialogST::SelectFolder(LPCTSTR lpszTitle, LPCTSTR lpszStartPath, UINT ulFlags, CWnd* pParentWnd)
{
LPMALLOC pMalloc;
BROWSEINFO bi;
LPITEMIDLIST pidl;
int nRetValue = IDCANCEL;
::ZeroMemory(&bi, sizeof(bi));
// Gets the Shell's default allocator
if (::SHGetMalloc(&pMalloc) == NOERROR)
{
// Get help on BROWSEINFO struct - it's got all the bit settings.
if (pParentWnd != NULL)
bi.hwndOwner = pParentWnd->m_hWnd;
bi.pidlRoot = NULL;
bi.pszDisplayName = m_szSelectedFolder;
bi.lpszTitle = lpszTitle;
bi.ulFlags = ulFlags;
bi.lpfn = BrowseCtrlCallback;
bi.lParam = (LPARAM)lpszStartPath;
// This next call issues the dialog box.
if ((pidl = ::SHBrowseForFolder(&bi)) != NULL)
{
if (::SHGetPathFromIDList(pidl, m_szSelectedFolder))
{
// At this point pszBuffer contains the selected path
nRetValue = IDOK;
}
// Free the PIDL allocated by SHBrowseForFolder.
pMalloc->Free(pidl);
}
// Release the shell's allocator.
pMalloc->Release();
}
return nRetValue;
}
CString CFileDialogST::GetSelectedFolder() const
{
return m_szSelectedFolder;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -