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

📄 fileopendlg.cpp

📁 wince下文件对话框的使用 用evc 不要用vc
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// 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


int CALLBACK SortNameAsc(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort);
int CALLBACK SortNameDes(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort);

int CALLBACK SortSizeAsc(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort);
int CALLBACK SortSizeDes(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort);

int CALLBACK SortDateAsc(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort);
int CALLBACK SortDateDes(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort);


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


CFileOpenDlg::CFileOpenDlg(CWnd* pParent /*=NULL*/)
:	CExDialog	(CFileOpenDlg::IDD, pParent),
	m_bOk		(FALSE),
	m_bTracking	(FALSE),
	m_ySplit	(152),
	m_nSort		(1)
{
	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("*");
	m_nState	= vsStandard;

	//
	// Get the locale date format
	//
	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, 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(TVN_SELCHANGED, IDC_FOLDER_TREE, OnSelchangedFolderTree)
	ON_NOTIFY(NM_CLICK, IDC_FILE_LIST, OnClickFileList)
	ON_COMMAND(ID_VIEW_STANDARD, OnViewStandard)
	ON_COMMAND(ID_VIEW_TREE, OnViewTree)
	ON_COMMAND(ID_VIEW_LIST, OnViewList)
	ON_COMMAND(ID_BAR_OK, OnBarOk)
	ON_COMMAND(ID_BAR_CANCEL, OnBarCancel)
	ON_WM_LBUTTONDOWN()
	ON_WM_LBUTTONUP()
	ON_WM_MOUSEMOVE()
	ON_NOTIFY(HDN_ITEMCLICK, 0, OnHeaderClickFileList)
	ON_NOTIFY(NM_CUSTOMDRAW, IDC_FILE_LIST,	OnCustomDrawList)
	ON_NOTIFY(TVN_ITEMEXPANDING, IDC_FOLDER_TREE, OnItemExpandingFolderTree)
	ON_NOTIFY(TVN_GETDISPINFO, IDC_FOLDER_TREE, OnGetDispInfoFolderTree)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()


//---------------------------------------------------------------------------
//
//	CFileOpenDlg virtual functions
//
//---------------------------------------------------------------------------


// CFileOpenDlg::DoPaint
//
//		Paints the splitter if it is displayed
//
void CFileOpenDlg::DoPaint(CPaintDC& dc) 
{
	if(m_nState == vsStandard)
	{
		CRect	rcClient;
		int		y;

		GetClientRect(&rcClient);

		y = m_ySplit;

		dc.MoveTo(0, y);
		dc.LineTo(rcClient.right, y);

		y += 6;		// This is the splitter height

		dc.MoveTo(0, y);
		dc.LineTo(rcClient.right, y);
	}
}


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


// CFileOpenDlg::DoSortList
//
//		Sorts the list according to the specified criterion
//
void CFileOpenDlg::DoSortList()
{
	switch(m_nSort)
	{
	case 1:
		m_listFile.SortItems(SortNameAsc, (DWORD)&m_cont);
		break;

	case -1:
		m_listFile.SortItems(SortNameDes, (DWORD)&m_cont);
		break;

	case 2:
		m_listFile.SortItems(SortSizeAsc, (DWORD)&m_cont);
		break;

	case -2:
		m_listFile.SortItems(SortSizeDes, (DWORD)&m_cont);
		break;

	case 3:
		m_listFile.SortItems(SortDateAsc, (DWORD)&m_cont);
		break;

	case -3:
		m_listFile.SortItems(SortDateDes, (DWORD)&m_cont);
		break;
	}
}


// CFileOpenDlg::SetColumnIcons
//
//		Correctly sets the columns sort icons
//
void CFileOpenDlg::SetColumnIcons()
{
	CHeaderCtrl*	pHeader = m_listFile.GetHeaderCtrl();

	if(pHeader)
	{
		HDITEM	hdi;
		int		i,
				nItems	= pHeader->GetItemCount();

		for(i = 0; i < nItems; ++i)
		{
			hdi.mask = HDI_IMAGE | HDI_FORMAT;
			pHeader->GetItem(i, &hdi);

			if(i+1 == m_nSort)
			{
				hdi.iImage = 1;
				hdi.fmt |= HDF_IMAGE | HDF_BITMAP_ON_RIGHT;
			}
			else if(i+1 == -m_nSort)
			{
				hdi.iImage = 2;
				hdi.fmt |= HDF_IMAGE | HDF_BITMAP_ON_RIGHT;
			}
			else
			{
				hdi.iImage = 0;
				hdi.fmt &= ~(HDF_IMAGE | HDF_BITMAP_ON_RIGHT);
			}
			pHeader->SetItem(i, &hdi);
		}
	}
}


// CFileOpenDlg::EnumDirs
//
//		Enumerates all subdirectories. Recurses depth-first 
//
int 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);
	int				nCount	= 0;

	strSearch += pszFilter;

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

	while(bFind)
	{
		if((fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) &&
		   !(fd.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN))
		{
			TVINSERTSTRUCT	tvi;
			CString			strSub(strBase);
			int				iIcon;

			strSub += fd.cFileName;
			iIcon = GetIconIndex(strSub);

			tvi.hParent				= hItemParent;
			tvi.hInsertAfter		= TVI_LAST;
			tvi.item.mask			= TVIF_CHILDREN | TVIF_IMAGE | TVIF_SELECTEDIMAGE |
									  TVIF_TEXT | TVIF_PARAM;
			tvi.item.pszText		= fd.cFileName;
			tvi.item.iImage			= iIcon;
			tvi.item.iSelectedImage	= iIcon;
			tvi.item.cChildren		= I_CHILDRENCALLBACK;
			tvi.item.lParam			= -1;

			hItem = m_treeFolder.InsertItem(&tvi);

			++nCount;
/*
			strSub += _T("\\");
			if(hItem)
			{
				m_treeFolder.SetItemData(hItem, 1);

				EnumDirs(strSub, pszFilter, hItem);
			}*/
		}

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


// 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),
					strFile;
	int				i = 0,
					iItem;
	COleDateTime	odt;
	CDateTimeFormat	dtf;
	CWaitCursor		wait;

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

	//
	// Fill in the list
	//
	strSearch += pszFilter;

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

	//
	// Disable painting the list
	//
	m_listFile.SetRedraw(FALSE);

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

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

				m_listFile.SetItemText(iItem, 2, dtf.GetString());
				m_listFile.SetItemData(iItem, i);

				m_cont.push_back(fd);
			}

			i++;
		}

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

	//
	// Enable painting the list
	//
	m_listFile.SetRedraw(TRUE);

}


// CFileOpenDlg::GetIconIndex
//
//		Retrieves the file's icon index
//		Code contributed by Amit Dey.
//
int CFileOpenDlg::GetIconIndex(LPCTSTR pszFileName)
{
	SHFILEINFO	ssfi;

	SHGetFileInfo(pszFileName, 0, &ssfi, sizeof(SHFILEINFO),
		SHGFI_SYSICONINDEX | SHGFI_SMALLICON);

	return ssfi.iIcon;
}


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

			if(strFolder != strSep)
				strFullPath = strFolder + 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::GetSystemName
//
//		Returns the system name
//
CString CFileOpenDlg::GetSystemName()
{
	HKEY		hKey;
	LONG		nRval;
	DWORD		dwSize = 64,
				dwType;
	CString		strSysName;
	TCHAR		szName[65];

	//
	// Get the system name
	//
	nRval = RegOpenKeyEx(HKEY_LOCAL_MACHINE, _T("\\Ident"), 0, 0, &hKey);
	if(ERROR_SUCCESS == nRval)
	{
		nRval = RegQueryValueEx(hKey, _T("Name"), NULL, &dwType, 
			(LPBYTE) szName, &dwSize);

		if(ERROR_SUCCESS == nRval)
			strSysName = szName;

		RegCloseKey(hKey);
	}
	
	//
	// On any error, use the default name
	//
	if(ERROR_SUCCESS != nRval)
		strSysName = _T("My Device");

	return strSysName;
}


// CFileOpenDlg::UpdateControls
//
//		Updates the control states
//
void CFileOpenDlg::UpdateControls()
{
	ASSERT(m_pCmdBar);

	CToolBarCtrl&	tb = m_pCmdBar->GetToolBarCtrl();

	tb.CheckButton(ID_VIEW_STANDARD,	m_nState == vsStandard);
	tb.CheckButton(ID_VIEW_TREE,		m_nState == vsTreeOnly);
	tb.CheckButton(ID_VIEW_LIST,		m_nState == vsListOnly);

	tb.EnableButton(ID_BAR_OK, m_bOk);
}


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


// CFileOpenDlg::OnInitDialog
//
//		Initialize the dialog
//
BOOL CFileOpenDlg::OnInitDialog() 
{
	CWinApp*		pApp = AfxGetApp();
	HTREEITEM		hItemRoot;
	HIMAGELIST		hImlSys;
	SHFILEINFO		ssfi;
	CWaitCursor		wait;
	TCHAR			szRoot[]	= _T("\\");

⌨️ 快捷键说明

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