⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fileopendlg.cpp

📁 evc编程,使用数据库软件
💻 CPP
字号:
// FileOpenDlg.cpp : implementation file
//
#include "stdafx.h"
#include "resource.h"
#include "FileOpenDlg.h"

#include "DateTimeFormat.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif


//---------------------------------------------------------------------------
//
//	CFileOpenDlg dialog
//
//---------------------------------------------------------------------------


CFileOpenDlg::CFileOpenDlg(CWnd* pParent /*=NULL*/)
	: CExDialog(CFileOpenDlg::IDD, pParent)
{
	TCHAR	szFmt[65];

	//{{AFX_DATA_INIT(CFileOpenDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT

	SetTitle(_T("File Open"));

	m_strFilter = _T("*");

	GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SSHORTDATE, szFmt, 64);
	m_strDateFormat = szFmt;
}


void CFileOpenDlg::DoDataExchange(CDataExchange* pDX)
{
	CExDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CFileOpenDlg)
	DDX_Control(pDX, IDOK, m_btnOK);
	DDX_Control(pDX, IDC_FILE_LIST, m_listFile);
	DDX_Control(pDX, IDC_FOLDER_TREE, m_treeFolder);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CFileOpenDlg, CExDialog)
	//{{AFX_MSG_MAP(CFileOpenDlg)
	ON_NOTIFY(NM_CLICK, IDC_FOLDER_TREE, OnClickFolderTree)
	ON_NOTIFY(TVN_SELCHANGED, IDC_FOLDER_TREE, OnSelchangedFolderTree)
	ON_NOTIFY(NM_CLICK, IDC_FILE_LIST, OnClickFileList)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()


//---------------------------------------------------------------------------
//
//	CFileOpenDlg utility functions
//
//---------------------------------------------------------------------------


// CFileOpenDlg::EnumDirs
//
//		Enumerates all subdirectories. Recurses depth-first 
//
void CFileOpenDlg::EnumDirs(LPCTSTR pszPath, LPCTSTR pszFilter, HTREEITEM hItemParent)
{
	WIN32_FIND_DATA	fd;
	HANDLE			hFind;
	BOOL			bFind;
	HTREEITEM		hItem = TVI_LAST;
	CString			strSearch(pszPath),
					strBase(pszPath);

	strSearch += pszFilter;

	hFind = FindFirstFile(strSearch, &fd);
	bFind = (hFind != INVALID_HANDLE_VALUE);

	while(bFind)
	{
		if(fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
		{
			CString	strSub(strBase);

			hItem = m_treeFolder.InsertItem(fd.cFileName, 2, 3, hItemParent, hItem);

			if(hItem)
			{
				m_treeFolder.SetItemData(hItem, 1);

				strSub += fd.cFileName;
				strSub += _T("\\");

				EnumDirs(strSub, pszFilter, hItem);
			}
		}

		bFind = FindNextFile(hFind, &fd);
	}
}


// CFileOpenDlg::EnumFiles
//
//		Enumerates all files in a folder
//
void CFileOpenDlg::EnumFiles(LPCTSTR pszPath, LPCTSTR pszFilter)
{
	WIN32_FIND_DATA	fd;
	HANDLE			hFind;
	BOOL			bFind;
	CString			strSearch(pszPath);
	int				i = 0,
					iItem;
	COleDateTime	odt;
	CDateTimeFormat	dtf;

	//
	// Flush the list
	//
	m_listFile.DeleteAllItems();

	strSearch += pszFilter;

	hFind = FindFirstFile(strSearch, &fd);
	bFind = (hFind != INVALID_HANDLE_VALUE);

	while(bFind)
	{
		if(!(fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
		{
			iItem = m_listFile.InsertItem(i, fd.cFileName);
			if(iItem != -1)
			{
				//
				// Format size according to locale
				//
				m_listFile.SetItemText(iItem, 1, FormatSize(fd.nFileSizeLow));

				//
				// Format date according to locale
				//
				odt = fd.ftLastAccessTime;
				dtf.SetDateTime(odt);
				dtf.SetFormat(m_strDateFormat);

				m_listFile.SetItemText(iItem, 2, dtf.GetString());
			}

			i++;
		}

		bFind = FindNextFile(hFind, &fd);
	}

	//
	// Disable the OK button
	//
	m_btnOK.EnableWindow(FALSE);
}


// CFileOpenDlg::GetFullPath
//
//		Returns the full path of a node
//
CString CFileOpenDlg::GetFullPath(HTREEITEM hItem)
{
	CString		strFullPath,
				strSep(_T("\\"));
	
	if(hItem)
	{
		while(hItem && m_treeFolder.GetItemData(hItem) == 1)
		{
			strFullPath = m_treeFolder.GetItemText(hItem) + strSep + strFullPath;

			hItem = m_treeFolder.GetParentItem(hItem);
		}
		strFullPath = strSep + strFullPath;
	}

	return strFullPath;
}


// CFileOpenDlg::FormatSize
//
//		Returns a string with the appropriate formatting
//
CString CFileOpenDlg::FormatSize(DWORD dwSize)
{
	CString			strFmt,
					strLet;

	if(dwSize > (1024 * 1024))
	{
		dwSize /= 1024 * 1024;
		strLet += _T("M");
	}
	else if(dwSize > 1024)
	{
		dwSize /= 1024;
		strLet += _T("K");
	}
	strFmt.Format(_T("%d"), (int)dwSize);
	strFmt += strLet;

	return strFmt;
}


//---------------------------------------------------------------------------
//
//	CFileOpenDlg message handlers
//
//---------------------------------------------------------------------------


// CFileOpenDlg::OnInitDialog
//
//		Initialize the dialog
//
BOOL CFileOpenDlg::OnInitDialog() 
{
	CWinApp*	pApp = AfxGetApp();
	HTREEITEM	hItemRoot;
	CWaitCursor	wait;

	CExDialog::OnInitDialog();

	//
	// Hide the OK button
	//
	ModifyStyle(0, WS_NONAVDONEBUTTON, SWP_NOSIZE); 
	SHDoneButton(m_hWnd, SHDB_HIDE);
	
	//
	// Initialize the image lists
	//
	if(m_imlTree.Create(16, 16, ILC_COLOR | ILC_MASK, 16, 16))
	{
		m_imlTree.Add(pApp->LoadIcon(IDI_POCKET_16));
		m_imlTree.Add(pApp->LoadIcon(IDI_OPENPOCKET_16));

		m_imlTree.Add(pApp->LoadIcon(IDI_CLOSEFOLDER_16));
		m_imlTree.Add(pApp->LoadIcon(IDI_OPENFOLDER_16));
	}

	//
	// Initialize the tree
	//
	if(m_imlTree.GetSafeHandle())
		m_treeFolder.SetImageList(&m_imlTree, TVSIL_NORMAL);
	hItemRoot = m_treeFolder.InsertItem(_T("My Device"), 0, 1);
	if(hItemRoot)
	{
		m_treeFolder.SetItemData(hItemRoot, 0);

		EnumDirs(_T("\\"), _T("*"), hItemRoot);
		m_treeFolder.Expand(hItemRoot, TVE_EXPAND);
	}

	//
	// Initialize the list
	//
	m_listFile.InsertColumn(0, _T("Name"), LVCFMT_LEFT,  80, -1);
	m_listFile.InsertColumn(1, _T("Size"), LVCFMT_RIGHT, 60,  1);
	m_listFile.InsertColumn(2, _T("Date"), LVCFMT_LEFT,  60,  2);
	EnumFiles(_T("\\"), m_strFilter);
	
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}


void CFileOpenDlg::OnOK() 
{
	// TODO: Add extra validation here
	
	CDialog::OnOK();
}


void CFileOpenDlg::OnCancel() 
{
	// TODO: Add extra cleanup here
	
	CDialog::OnCancel();
}


// CFileOpenDlg::OnClickFolderTree
//
//		The user clicked an item
//
void CFileOpenDlg::OnClickFolderTree(NMHDR* pNMHDR, LRESULT* pResult) 
{
	*pResult = 0;
}


// CFileOpenDlg::OnSelchangedFolderTree
//
//		The tree has changed its selection
//
void CFileOpenDlg::OnSelchangedFolderTree(NMHDR* pNMHDR, LRESULT* pResult) 
{
	NM_TREEVIEW*	pNMTreeView = (NM_TREEVIEW*)pNMHDR;
	HTREEITEM		hItem;
	
	hItem = pNMTreeView->itemNew.hItem;
	if(hItem)
	{
		m_strPath = GetFullPath(hItem);

		EnumFiles(m_strPath, m_strFilter);
	}

	*pResult = 0;
}


// CFileOpenDlg::OnClickFileList
//
//		The user clicked the file list
//
void CFileOpenDlg::OnClickFileList(NMHDR* pNMHDR, LRESULT* pResult) 
{
	int iItem;

	iItem = m_listFile.GetNextItem(-1, LVNI_SELECTED);
	if(iItem != -1)
	{
		m_strFile = m_listFile.GetItemText(iItem, 0);
	}

	//
	// Enable or disable the OK button
	//
	m_btnOK.EnableWindow(iItem != -1);
	
	*pResult = 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -