📄 filedlg.cpp
字号:
#include "resource.h"
#include <windows.h>
#include <windowsx.h>
#include <tchar.h>
#include <commdlg.h>
#include "filedlg.h"
const static int s_nListColumnFmt[] =
{
LVCFMT_LEFT, LVCFMT_RIGHT, LVCFMT_LEFT, LVCFMT_LEFT
};
const static int s_nListColumnWidth[] =
{
145, 65, 75, 120
};
const static UINT s_nListColumnTextId[] =
{
IDS_COLUMN_NAME, IDS_COLUMN_SIZE, IDS_COLUMN_TYPE, IDS_COLUMN_DATE
};
static LPCTSTR bslash = _T("\\");
// public members
CFileDialog::CFileDialog(LPOPENFILENAME pofn)
{
InitCommonControls();
m_pofn = pofn;
m_hwndDlg = NULL;
m_hFnt = NULL;
m_nListSort = LIST_SORT_NAME;
m_fSortOrder = TRUE;
m_fShowExt = FALSE;
m_fCtrl = FALSE;
m_pszFilter = NULL;
m_pszDefExt = NULL;
m_pOrgListView = NULL;
m_hInst=pofn->hInstance;
}
CFileDialog::~CFileDialog()
{
if (m_hFnt)
DeleteObject(m_hFnt);
}
int CFileDialog::DoModal(BOOL fSave)
{
if (!m_pofn)
return IDCANCEL;
m_fSave = fSave;
int nResource = GetDlgResourceID();
return DialogBoxParam(m_hInst, MAKEINTRESOURCE(nResource),
m_pofn->hwndOwner, FileDlgProc, (LPARAM)this);
}
LRESULT CALLBACK ListViewProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
#if (_WIN32_WCE >= 300 || _WIN32_WCE_EMULATION)
WNDPROC p = (WNDPROC)GetWindowLong(hwnd, GWL_USERDATA);
#else
FARPROC p = (FARPROC)GetWindowLong(hwnd, GWL_USERDATA);
#endif
if (uMsg == WM_GETDLGCODE)
return DLGC_WANTALLKEYS;
return CallWindowProc(p, hwnd, uMsg, wParam, lParam);
}
// protected members
BOOL CFileDialog::OnInitDialog(HWND hwndDlg)
{
m_hwndDlg = hwndDlg;
m_hwndLV = GetDlgItem(m_hwndDlg, IDC_LIST_FILE);
if (m_fDlgType == DLG_TYPE_PPC3)
m_helper.SHInitDialog(m_hwndDlg);
INITCOMMONCONTROLSEX iccex;
iccex.dwSize = sizeof(INITCOMMONCONTROLSEX);
iccex.dwICC = ICC_BAR_CLASSES | ICC_COOL_CLASSES;
InitCommonControlsEx(&iccex);
m_pOrgListView = (WNDPROC)GetWindowLong(m_hwndLV, GWL_WNDPROC);
if (m_pOrgListView) {
SetWindowLong(m_hwndLV, GWL_WNDPROC, (DWORD)ListViewProc);
SetWindowLong(m_hwndLV, GWL_USERDATA, (DWORD)m_pOrgListView);
}
WCHAR caption[32];
m_helper.LoadString(IDS_CAPTION_CANCEL,caption,32);//Cancel
SetDlgItemText(m_hwndDlg, IDCANCEL, caption);
m_helper.LoadString(IDS_CAPTION_NAME,caption,32);//Name:
SetDlgItemText(m_hwndDlg,IDC_STATIC_NAME,caption);
m_helper.LoadString(IDS_CAPTION_TYPE,caption,32);//Type:
SetDlgItemText(m_hwndDlg,IDC_STATIC_TYPE,caption);
// add by Y.N
if (m_helper.IsWM5()) {
ShowWindow(GetDlgItem(m_hwndDlg, IDCANCEL), SW_HIDE);
}
GetShellSettings();
CreateToolBar();
CheckWindowSize();
ParseFilter();
CreateExtList();
SendMessage(GetDlgItem(m_hwndDlg, IDC_COMBO_FILTER), CB_SETEXTENDEDUI, 1, 0);
InitListView();
SetDlgItemText(m_hwndDlg, IDC_EDIT_NAME, m_pofn->lpstrFile);
LPTSTR psz;
TCHAR sz[MAX_LOADSTRING];
if (m_pofn->lpstrInitialDir &&
_tcslen(m_pofn->lpstrInitialDir) &&
LoadFolderItem(m_pofn->lpstrInitialDir))
goto done;
_tcscpy(sz, m_pofn->lpstrFile);
if (_tcslen(sz) && LoadFolderItem(sz))
goto done;
psz = _tcsrchr(sz, _T('\\'));
if (psz) {
SetDlgItemText(m_hwndDlg, IDC_EDIT_NAME, psz + 1);
*psz = NULL;
}
if (_tcslen(sz) && LoadFolderItem(sz))
goto done;
m_helper.LoadString(IDS_DEF_DIR, sz, MAX_LOADSTRING);
if (LoadFolderItem(sz))
goto done;
LoadFolderItem(bslash);
done:
if (m_fSave)
SetFocus(GetDlgItem(m_hwndDlg, IDC_EDIT_NAME));
else
SetFocus(GetDlgItem(m_hwndDlg, IDC_LIST_FILE));
ChangeListStyle(LVS_LIST); // add by Y.N
SetActiveWindow(NULL);
SetForegroundWindow(m_hwndDlg);
return FALSE;
}
void CFileDialog::OnOK()
{
DWORD dwAttr;
LPTSTR pszSrc = NULL, pszPath = NULL, p;
TCHAR szMsg[MAX_LOADSTRING];
TCHAR szTitle[MAX_LOADSTRING];
if (m_pofn->lpstrTitle && _tcslen(m_pofn->lpstrTitle))
{
_tcsncpy(szTitle, m_pofn->lpstrTitle, MAX_LOADSTRING);
}
else
{
m_helper.LoadString(m_fSave ? IDS_SAVE_TITLE : IDS_OPEN_TITLE, szTitle, MAX_LOADSTRING);
}
int n = GetWindowTextLength(GetDlgItem(m_hwndDlg, IDC_EDIT_NAME));
if (!n) return;
SendMessage(GetDlgItem(m_hwndDlg, IDC_EDIT_NAME), EM_SETSEL, 0, -1);
pszSrc = new TCHAR[n + 1];
GetDlgItemText(m_hwndDlg, IDC_EDIT_NAME, pszSrc, n + 1);
if (_tcschr(pszSrc, _T('/')) ||
_tcschr(pszSrc, _T(':')) ||
_tcschr(pszSrc, _T(';')) ||
_tcschr(pszSrc, _T('?')) ||
_tcschr(pszSrc, _T(':')) ||
_tcschr(pszSrc, _T('<')) ||
_tcschr(pszSrc, _T('>')) ||
_tcschr(pszSrc, _T('|')))
goto done;
if (_tcschr(pszSrc, _T('*'))) {
// search
if (m_pszFilter) {
delete [] m_pszFilter;
m_pszFilter = NULL;
}
m_pszFilter = pszSrc; pszSrc = NULL;
LoadFolderItem(m_szCurrent, FALSE);
goto done;
}
else if (_tcschr(pszSrc, _T('\"'))) {
// multi
if (m_fSave || !(m_pofn->Flags & OFN_ALLOWMULTISELECT))
goto done;
n = m_pofn->nMaxFile;
pszPath = m_pofn->lpstrFile;
p = _tcslen(m_szCurrent) ? m_szCurrent : (LPTSTR)bslash;
_tcsncpy(pszPath, p, n);
n -= _tcslen(pszPath) + 2;
pszPath += _tcslen(pszPath);
*pszPath++ = NULL;
p = pszSrc;
while (n > 0 && *p != NULL) {
LPTSTR psz;
if (*p == _T('\"'))
p++;
psz = _tcschr(p, _T('\"'));
if (m_pofn->Flags & OFN_FILEMUSTEXIST) {
int nLen = MAX_PATH;
TCHAR szPath[MAX_PATH + 1] = _T("");
_tcscpy(szPath, m_szCurrent);
_tcscat(szPath, bslash);
nLen -= _tcslen(m_szCurrent) + 1;
if (psz)
_tcsncat(szPath, p, min(psz - p, nLen));
else
_tcsncat(szPath, p, nLen);
dwAttr = GetFileAttributes(szPath);
if (dwAttr == 0xFFFFFFFF ||
(dwAttr & FILE_ATTRIBUTE_DIRECTORY)) {
m_helper.LoadString(IDS_MSG_FILE_MUST_EXIST, szMsg, MAX_LOADSTRING);
p = new TCHAR[_tcslen(szPath) + _tcslen(szMsg) + 1];
wsprintf(p, szMsg, szPath);
MessageBox(m_hwndDlg, p, szTitle, MB_ICONEXCLAMATION | MB_OK);
delete [] p;
pszPath = NULL;
goto done;
}
}
if (psz) {
_tcsncpy(pszPath, p, min(psz - p, n));
n -= psz - p;
pszPath += psz - p;
}
else {
_tcsncpy(pszPath, p, n);
n -= _tcslen(p);
pszPath += _tcslen(p);
}
if (n > 0)
*pszPath++ = NULL;
if (!psz)
break;
p = psz;
while (*p == _T(' ') || *p == _T('\"'))
p++;
}
pszPath = NULL;
n = (n > 1) ? IDOK : IDCANCEL;
EndDialog(n);
}
else {
// single
n = (*pszSrc != _T('\\')) ? _tcslen(m_szCurrent) + _tcslen(pszSrc) + 1 : _tcslen(pszSrc);
p = _tcsrchr(pszSrc, _T('.'));
if (!p || _tcschr(p, _T('\\'))) {
if (m_pszDefExt)
n += _tcslen(m_pszDefExt);
else if (m_pofn->lpstrDefExt) {
n += _tcslen(m_pofn->lpstrDefExt) + 1;
}
else {
p = _tcschr((LPTSTR)m_listExt.GetAt(0), _T('.'));
if (p && !_tcschr(p, _T('*')))
n += _tcslen(p);
}
}
else {
if (m_pofn->lpstrDefExt && m_pszDefExt)
n += max(_tcslen(m_pszDefExt), _tcslen(m_pofn->lpstrDefExt) + 1);
else if (m_pszDefExt)
n += _tcslen(m_pszDefExt);
else if (m_pofn->lpstrDefExt)
n += _tcslen(m_pofn->lpstrDefExt) + 1;
}
pszPath = new TCHAR[n + 1];
*pszPath = NULL;
if (*pszSrc != _T('\\')) {
_tcscpy(pszPath, m_szCurrent);
_tcsncat(pszPath, bslash, n);
}
_tcscat(pszPath, pszSrc);
p = _tcsrchr(pszSrc, _T('.'));
if (!p || _tcschr(p, _T('\\'))) {
if (m_pszDefExt)
_tcscat(pszPath, m_pszDefExt);
else {
dwAttr = GetFileAttributes(pszPath);
if (dwAttr != 0xFFFFFFFF &&
(dwAttr & FILE_ATTRIBUTE_DIRECTORY)) {
if (LoadFolderItem(pszPath, FALSE)) {
goto done;
}
}
else if (m_pofn->lpstrDefExt) {
_tcscat(pszPath, _T("."));
_tcscat(pszPath, m_pofn->lpstrDefExt);
}
else {
p = _tcschr((LPTSTR)m_listExt.GetAt(0), _T('.'));
if (p && !_tcschr(p, _T('*')))
_tcscat(pszPath, p);
}
}
}
else {
dwAttr = GetFileAttributes(pszPath);
if (dwAttr == 0xFFFFFFFF ||
(dwAttr & FILE_ATTRIBUTE_DIRECTORY)) {
if (m_pszDefExt)
_tcscat(pszPath, m_pszDefExt);
//else if (m_pofn->lpstrDefExt)
// _tcscat(pszPath, m_pofn->lpstrDefExt);
}
}
if (!m_fSave && (m_pofn->Flags & OFN_FILEMUSTEXIST)) {
dwAttr = GetFileAttributes(pszPath);
if (dwAttr & FILE_ATTRIBUTE_DIRECTORY) {
m_helper.LoadString(IDS_MSG_FILE_MUST_EXIST, szMsg, MAX_LOADSTRING);
p = new TCHAR[_tcslen(pszPath) + _tcslen(szMsg) + 1];
wsprintf(p, szMsg, pszPath);
MessageBox(m_hwndDlg, p, szTitle, MB_ICONEXCLAMATION | MB_OK);
delete [] p;
goto done;
}
}
if (!m_fSave && (m_pofn->Flags & OFN_CREATEPROMPT)) {
dwAttr = GetFileAttributes(pszPath);
if (dwAttr & FILE_ATTRIBUTE_DIRECTORY) {
m_helper.LoadString(IDS_MSG_CREATE_PROMPT, szMsg, MAX_LOADSTRING);
p = new TCHAR[_tcslen(pszPath) + _tcslen(szMsg) + 1];
wsprintf(p, szMsg, pszPath);
n = MessageBox(m_hwndDlg, p, szTitle, MB_ICONQUESTION | MB_YESNO);
delete [] p;
if (n == IDNO)
goto done;
}
}
if (m_fSave && (m_pofn->Flags & OFN_PATHMUSTEXIST)) {
p = _tcsrchr(pszPath, _T('\\'));
if (p) *p = NULL;
dwAttr = GetFileAttributes(pszPath);
if (p) *p = _T('\\');
if (dwAttr == 0xFFFFFFFF ||
!(dwAttr & FILE_ATTRIBUTE_DIRECTORY)) {
m_helper.LoadString(IDS_MSG_PATH_MUST_EXIST, szMsg, MAX_LOADSTRING);
p = new TCHAR[_tcslen(pszPath) + _tcslen(szMsg) + 1];
wsprintf(p, szMsg, pszPath);
MessageBox(m_hwndDlg, p, szTitle, MB_ICONEXCLAMATION | MB_OK);
delete [] p;
goto done;
}
}
if (m_fSave && (m_pofn->Flags & OFN_OVERWRITEPROMPT)) {
dwAttr = GetFileAttributes(pszPath);
if (dwAttr != 0xFFFFFFFF &&
!(dwAttr & FILE_ATTRIBUTE_DIRECTORY)) {
m_helper.LoadString(IDS_MSG_OVERWRITE_PROMPT, szMsg, MAX_LOADSTRING);
p = new TCHAR[_tcslen(pszPath) + _tcslen(szMsg) + 1];
wsprintf(p, szMsg, pszPath);
n = MessageBox(m_hwndDlg, p, szTitle, MB_ICONEXCLAMATION | MB_YESNO);
delete [] p;
if (n == IDNO)
goto done;
}
}
_tcsncpy(m_pofn->lpstrFile, pszPath, m_pofn->nMaxFile - 1);
m_pofn->lpstrFile[m_pofn->nMaxFile - 1] = NULL;
if (m_pofn->lpstrFileTitle && m_pofn->nMaxFileTitle) {
p = _tcsrchr(pszPath, _T('\\'));
if (p)
_tcsncpy(m_pofn->lpstrFileTitle, p + 1, m_pofn->nMaxFileTitle - 1);
else
_tcsncpy(m_pofn->lpstrFileTitle, pszPath, m_pofn->nMaxFileTitle - 1);
m_pofn->lpstrFileTitle[m_pofn->nMaxFileTitle - 1] = NULL;
}
n = (_tcslen(pszPath) + 1 <= m_pofn->nMaxFile) ? IDOK : IDCANCEL;
EndDialog(n);
}
done:
if (pszPath)
delete [] pszPath;
if (pszSrc)
delete [] pszSrc;
}
void CFileDialog::EndDialog(int nResult)
{
if (nResult == IDOK) {
int n = SendMessage(GetDlgItem(m_hwndDlg, IDC_COMBO_FILTER), CB_GETCURSEL, 0, 0);
if (n != CB_ERR && m_pofn->lpstrFilter)
m_pofn->nFilterIndex = n + 1;
}
if (m_fCtrl) {
keybd_event(VK_CONTROL, 0x1e, KEYEVENTF_KEYUP, 1);
}
if (m_pszFilter) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -