📄 fileopen.cpp
字号:
#define STRICT
#include <windows.h>
#include <windowsx.h>
#include <commctrl.h>
#include <commdlg.h>
#include <tchar.h>
#include "basewin.h"
#include "resource.h"
#include "tools.h"
#include "fileopen.h"
#define GawaroBaseKey TEXT("Software\\Gawaro")
#define MAX_RECENT 10
extern HFONT CreateListboxFont() ;
/*-------------------------------------------------------------------------*
* FileOpen 僟僀傾儘僌
*-------------------------------------------------------------------------*/
class FileOpenDialog : public Window {
BOOL m_fSave ;
OPENFILENAME *m_pOfn ;
TCHAR m_sWildCard[ MAX_PATH ] ;
TCHAR m_sFile[ MAX_PATH ] ;
TCHAR m_sDir[ MAX_PATH ] ;
HFONT m_hFont ;
LPCTSTR m_pAppName ;
BOOL m_fRecent ;
public:
FileOpenDialog( OPENFILENAME *pOfn, BOOL fSave, LPCTSTR pAppName ) ;
~FileOpenDialog() ;
LPCTSTR GetText() const { return m_sFile ; }
LPCTSTR GetDir() const { return m_sDir ; }
void WMCreate( HWND hDlg ) ;
LRESULT WMCommand( HWND hDlg, UINT msg, WPARAM wp, LPARAM lp ) ;
BOOL EnumDir( HWND hWnd, LPDWORD pCount, LPDWORD pMem ) ;
BOOL ListDir( HWND hWnd, LPCTSTR pPrevDir ) ;
void InitListDir( HWND hWnd ) ;
BOOL ChangeDir( HWND hDlg, LPBOOL pDone ) ;
BOOL EnumRecent( HWND hWnd ) ;
BOOL SelectRecent( HWND hDlg ) ;
static LPCTSTR c_sDir ;
static LPCTSTR c_sPatDir ;
static LPCTSTR c_sPatFile ;
} ;
LPCTSTR FileOpenDialog::c_sDir = TEXT(" <DIR>") ;
LPCTSTR FileOpenDialog::c_sPatDir = TEXT(" %s\t%s") ;
LPCTSTR FileOpenDialog::c_sPatFile = TEXT("%s\t%8d") ;
FileOpenDialog::FileOpenDialog( OPENFILENAME *pOfn, BOOL fSave, LPCTSTR pAppName )
{
LPCTSTR ptr ;
m_fSave = fSave ;
m_pOfn = pOfn ;
/* 僼僅儞僩傪庢摼偡傞 */
m_hFont = CreateListboxFont() ;
/* 儚僀儖僪僇乕僪傪嶌惉偡傞 */
_tcscpy( m_sWildCard, TEXT("*.*") ) ;
ptr = pOfn->lpstrFilter ;
if ( ptr && *ptr ) {
ptr += _tcslen( ptr ) + 1 ;
if ( *ptr ) {
_tcscpy( m_sWildCard, ptr ) ;
}
}
_tcscpy( m_sFile, pOfn->lpstrFile ? pOfn->lpstrFile : TEXT("") ) ;
_tcscpy( m_sDir, pOfn->lpstrInitialDir ? pOfn->lpstrInitialDir : TEXT("\\") ) ;
m_pAppName = pAppName ;
m_fRecent = pAppName ? TRUE : FALSE ;
}
FileOpenDialog::~FileOpenDialog()
{
DeleteObject( m_hFont ) ;
}
void
FileOpenDialog::WMCreate( HWND hDlg )
{
DWORD w ;
RECT rect ;
int tabs[ 1 ] ;
HWND hWnd = GetDlgItem( hDlg, IDC_LST_FILE ) ;
LPCTSTR pszTitle ;
/* 僟僀傾儘僌僞僀僩儖傪愝掕偡傞 */
if ( m_pOfn->lpstrTitle ) {
pszTitle = m_pOfn->lpstrTitle ;
} else if ( m_fSave ) {
pszTitle = TEXT("Save As") ;
} else {
pszTitle = TEXT("Open") ;
}
SetWindowText( hDlg, pszTitle ) ;
/* 僨傿儗僋僩儕儕僗僩偺僼僅儞僩傪愝掕偡傞 */
SendMessage( hWnd, WM_SETFONT, (WPARAM) m_hFont, (LPARAM) TRUE ) ;
/* TAB偺埵抲傪愝掕偡傞 */
GetClientRect( hDlg, &rect ) ;
w = rect.right - 8 * 10 ;
tabs[0] = w / ((LOWORD(GetDialogBaseUnits()) + 3) / 4) ;
SendMessage( hWnd, LB_SETTABSTOPS, 1, (LPARAM) tabs ) ;
if ( m_fRecent ) {
/* 嵟怴偺僼傽僀儖堦棗傪摼傞 */
if ( EnumRecent( hWnd ) ) {
/* 僇儗儞僩僨傿儗僋僩儕傪昞帵偡傞 */
SetDlgItemText( hDlg, IDC_CAP_DIR, TEXT("Recent Files...") ) ;
return ;
}
}
/* 僨傿儗僋僩儕撪梕傪弶婜昞帵偡傞 */
InitListDir( hWnd ) ;
/* 僇儗儞僩僨傿儗僋僩儕傪昞帵偡傞 */
SetDlgItemText( hDlg, IDC_CAP_DIR, m_sDir ) ;
/* 僨傿儗僋僩儕儕僗僩偺慖戰傪夝彍偡傞 */
SendMessage( hWnd, LB_SETCURSEL, (WPARAM) LB_ERR, 0 ) ;
}
LRESULT
FileOpenDialog::WMCommand( HWND hDlg, UINT msg, WPARAM wp, LPARAM lp )
{
LPTSTR ptr ;
BOOL fDone ;
HWND hCtrl ;
DWORD index ;
TCHAR buf[ MAX_PATH ] ;
hCtrl = GetDlgItem( hDlg, IDC_LST_FILE ) ;
switch ( GET_WM_COMMAND_ID( wp, lp ) ) {
case IDCANCEL:
EndDialog( hDlg, FALSE ) ;
return TRUE ;
case IDC_BTN_DIR:
if ( m_fRecent ) {
/* 僨傿儗僋僩儕撪梕傪弶婜昞帵偡傞 */
InitListDir( hCtrl ) ;
/* 僇儗儞僩僨傿儗僋僩儕傪昞帵偡傞 */
SetDlgItemText( hDlg, IDC_CAP_DIR, m_sDir ) ;
/* 僨傿儗僋僩儕堦棗偵僼僅乕僇僗堏摦 */
SetFocus( hCtrl ) ;
}
return TRUE ;
case IDC_LST_FILE:
switch ( GET_WM_COMMAND_CMD( wp, lp ) ) {
case LBN_SELCHANGE:
if ( m_fRecent ) {
break ;
}
index = SendDlgItemMessage( hDlg, IDC_LST_FILE, LB_GETCURSEL, 0, 0 ) ;
if ( index == LB_ERR ) {
break ;
}
SendDlgItemMessage( hDlg, IDC_LST_FILE, LB_GETTEXT, (WPARAM) index, (LPARAM) buf ) ;
ptr = _tcschr( buf, TEXT('\t') ) ;
if ( ptr ) {
if ( !_tcsicmp( ptr + 1, c_sDir ) ) {
*buf = 0 ;
} else {
*ptr = 0 ;
}
} else {
*buf = 0 ;
}
SetDlgItemText( hDlg, IDC_EDT_FILE, buf ) ;
return TRUE ;
case LBN_DBLCLK:
if ( m_fRecent ) {
if ( SelectRecent( hDlg ) ) {
EndDialog( hDlg, TRUE ) ;
}
} else {
if ( ChangeDir( hDlg, &fDone ) ) {
if ( fDone ) {
EndDialog( hDlg, TRUE ) ;
}
return TRUE ;
}
}
break ;
}
break ;
case IDOK:
if ( GetFocus() == hCtrl ) {
SendMessage( hDlg, WM_COMMAND, MAKELONG(IDC_LST_FILE, LBN_DBLCLK), 0 ) ;
return TRUE ;
} else if ( m_fRecent ) {
/* 僨傿儗僋僩儕撪梕傪弶婜昞帵偡傞 */
InitListDir( hCtrl ) ;
/* 僇儗儞僩僨傿儗僋僩儕傪昞帵偡傞 */
SetDlgItemText( hDlg, IDC_CAP_DIR, m_sDir ) ;
/* 僨傿儗僋僩儕堦棗偵僼僅乕僇僗堏摦 */
SetFocus( hCtrl ) ;
return TRUE ;
}
GetDlgItemText( hDlg, IDC_EDT_FILE, buf, MAX_PATH ) ;
if ( !*buf ) {
SetFocus( hCtrl ) ;
return TRUE ;
}
ptr = _tcsrchr( buf, TEXT('.') ) ;
if ( !ptr ) {
/* 奼挘巕偑偮偄偰偄側偄応崌 */
if ( m_pOfn->lpstrDefExt ) {
/* 僨僼僅儖僩偺奼挘巕傪偮偗傞 */
_tcscat( buf, TEXT(".") ) ;
_tcscat( buf, m_pOfn->lpstrDefExt ) ;
}
} else if ( !*(ptr + 1) ) {
/* 嵟屻偵偮偗偨僺儕僆僪偼嶍傞 */
*ptr = 0 ;
}
_tcscpy( m_sFile, m_sDir ) ;
catdir( m_sFile, buf, NULL ) ;
EndDialog( hDlg, TRUE ) ;
return TRUE ;
}
return 0 ;
}
BOOL
FileOpenDialog::EnumDir( HWND hWnd, LPDWORD pCount, LPDWORD pMem )
{
WIN32_FIND_DATA find ;
HANDLE hFind ;
BOOL fAdd = !pCount && !pMem ;
TCHAR findPath[ MAX_PATH ], *ptr ;
DWORD count = 0, num_mem = 0, attr ;
attr = GetFileAttributes( m_sDir ) ;
if ( attr == 0xFFFFFFFF || !(attr & FILE_ATTRIBUTE_DIRECTORY) ) {
return FALSE ;
}
if ( m_sDir && _tcscmp( m_sDir, TEXT("\\") ) ) {
/* 恊僨傿儗僋僩儕傊偺僄儞僩儕傪嶌惉偡傞 */
wsprintf( findPath, c_sPatDir, TEXT(".."), c_sDir ) ;
if ( fAdd ) {
SendMessage( hWnd, LB_ADDSTRING, 0, (LPARAM) findPath ) ;
} else {
count ++ ;
num_mem += _tcslen( findPath ) ;
}
}
/* 僨傿儗僋僩儕専嶕梡偺儚僀儖僪僇乕僪傪嶌惉偡傞 */
_tcscpy( findPath, m_sDir ) ;
catdir( findPath, TEXT("*.*"), NULL ) ;
/* 僨傿儗僋僩儕傪専嶕偡傞 */
hFind = FindFirstFile( findPath, &find ) ;
if ( hFind != INVALID_HANDLE_VALUE ) {
do {
if ( !_tcscmp( find.cFileName, TEXT(".") ) ) {
continue ;
} else if ( !_tcscmp( find.cFileName, TEXT("..") ) ) {
continue ;
} else if ( !(find.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ) {
continue ;
}
wsprintf( findPath, c_sPatDir, find.cFileName, c_sDir ) ;
if ( fAdd ) {
/* 儕僗僩儃僢僋僗偵崁栚傪捛壛偡傞 */
SendMessage( hWnd, LB_ADDSTRING, 0, (LPARAM) findPath ) ;
} else {
count ++ ;
num_mem += _tcslen( findPath ) + 1 ;
}
} while ( FindNextFile( hFind, &find ) ) ;
FindClose( hFind ) ;
}
/* 僼傽僀儖専嶕梡偺儚僀儖僪僇乕僪傪嶌惉偡傞 */
_tcscpy( findPath, m_sDir ) ;
catdir( findPath, m_sWildCard, NULL ) ;
/* 僼傽僀儖傪専嶕偡傞 */
hFind = FindFirstFile( findPath, &find ) ;
if ( hFind != INVALID_HANDLE_VALUE ) {
do {
if ( find.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY ) {
continue ;
#ifdef _WIN32_WCE
} else if ( find.dwFileAttributes & FILE_ATTRIBUTE_INROM ) {
continue ;
} else if ( find.dwFileAttributes & FILE_ATTRIBUTE_ROMSTATICREF ) {
continue ;
} else if ( find.dwFileAttributes & FILE_ATTRIBUTE_ROMMODULE ) {
continue ;
#endif /* _WIN32_WCE */
}
/* 僶僀僫儕僼傽僀儖偼彍奜 */
ptr = _tcsrchr( find.cFileName, TEXT('.') ) ;
if ( ptr ) {
if ( !_tcsicmp( ptr, TEXT(".BMP") ) ) {
continue ;
} else if ( !_tcsicmp( ptr, TEXT(".2BP") ) ) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -