www.gusucode.com > VC生成的临时文件清除工具源码程序 > VC生成的临时文件清除工具源码程序/code/DelTempDlg.cpp

    // DelTempDlg.cpp : implementation file
//

#include "stdafx.h"
#include "DelTemp.h"
#include "DelTempDlg.h"

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

/////////////////////////////////////////////////////////////////////////////
// CDelTempDlg dialog

CDelTempDlg::CDelTempDlg(CWnd* pParent /* = NULL*/)
	: CDialog(CDelTempDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CDelTempDlg)
	m_strPath  =  _T("");
	m_strStatus  =  _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon  =  AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CDelTempDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CDelTempDlg)
	DDX_Control(pDX, IDC_COMBO_FILE_TYPE, m_listFileType);
	DDX_Text(pDX, IDC_EDIT_PATH, m_strPath);
	DDX_Text(pDX, IDC_EDIT_STATUS, m_strStatus);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CDelTempDlg, CDialog)
	//{{AFX_MSG_MAP(CDelTempDlg)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON_ADD, OnButtonAdd)
	ON_BN_CLICKED(IDC_BUTTON_BROWSE, OnButtonBrowse)
	ON_BN_CLICKED(IDC_BUTTON_CLEAR, OnButtonClear)
	ON_BN_CLICKED(IDC_BUTTON_DEL, OnButtonDel)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDelTempDlg message handlers

BOOL CDelTempDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
	char	szBuf[MAX_PATH], szString[MAX_PATH] = "NULL", szDefault[MAX_PATH] = "NULL";
	int i = 0;
	int	nTotal = GetPrivateProfileInt("Num", "Total", -1, "DelTemp.ini");
	if (nTotal > -1)
	{
		m_listFileType.ResetContent();
		for (i=0; i<nTotal; i++)
		{
			GetPrivateProfileString("Extentions", itoa(i,szBuf,10), szDefault, szString, MAX_PATH, "DelTemp.ini");
			if (strcmp(szString, szDefault))
			{
				m_listFileType.AddString(szString);
			}
		}
	}
	m_strPath  =  "D:\\Mywork";
	m_strStatus  =  "Ready...";
	m_listFileType.SetCurSel(0);
	UpdateData(FALSE);

	return TRUE;  // return TRUE  unless you set the focus to a control
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CDelTempDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon  =  GetSystemMetrics(SM_CXICON);
		int cyIcon  =  GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x  =  (rect.Width() - cxIcon + 1) / 2;
		int y  =  (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CDelTempDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CDelTempDlg::OnOK() 
{
	// TODO: Add extra validation here
	UpdateData(TRUE);
	if (!SetCurrentDirectory(m_strPath))
	{
		m_strStatus = "Error: no such directory - " + m_strPath;
		UpdateData(FALSE);
		return;
	}

	CStringList listFileName;

	UpdateData(TRUE);
	int	nCount  =  m_listFileType.GetCount();
	CString	strFileType;
	while (nCount--)
	{
		listFileName.RemoveAll();
		m_listFileType.GetLBText(nCount, strFileType);
		m_strStatus +=  "\r\nDeleting files like " + strFileType;
		UpdateData(FALSE);
		SearchFile(strFileType, m_strPath, listFileName);
		DelFile(listFileName);
	}

	
	//CDialog::OnOK();
}

CStringList& CDelTempDlg::SearchFile(CString strFileName, CString strPath, CStringList &listFileName)
{
	char szFullPathName[MAX_PATH];
	/*static*/ WIN32_FIND_DATA	findData;

	SetCurrentDirectory(strPath);

	HANDLE	hFindHandle  =  FindFirstFile(strFileName, &findData);
	if ((hFindHandle !=  INVALID_HANDLE_VALUE))
	{
		if (!(findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
		{
			GetFullPathName(findData.cFileName, MAX_PATH, szFullPathName, NULL);
			listFileName.AddTail(szFullPathName);
		}
		while (FindNextFile(hFindHandle, &findData) !=  0)
		{
			if (!(findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
			{
				GetFullPathName(findData.cFileName, MAX_PATH, szFullPathName, NULL);
				listFileName.AddTail(szFullPathName);
			}
		}
		FindClose(hFindHandle);
	}

	hFindHandle  =  FindFirstFile("*", &findData);
	if ((hFindHandle !=  INVALID_HANDLE_VALUE))
	{
		if (findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
		{
			if (findData.cFileName[0] !=  '.')
			{
				SearchFile(strFileName, findData.cFileName, listFileName);
				SetCurrentDirectory("..");
			}
		}
		while (FindNextFile(hFindHandle, &findData) !=  0)
		{
			if (findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
			{
				if (findData.cFileName[0] !=  '.')
				{
					SearchFile(strFileName, findData.cFileName, listFileName);
					SetCurrentDirectory("..");
				}
			}
		}
		FindClose(hFindHandle);
	}

	return	listFileName;
}

bool CDelTempDlg::DelFile(const CStringList &listFileName)
{
	if (listFileName.IsEmpty())
	{
		m_strStatus += "\tNothing found!";
		UpdateData(FALSE);
		return true;
	}

	CFileStatus	rStatus;
	POSITION	rPos;
	rPos  =  listFileName.GetHeadPosition();
	CString	strFileName;
	while (rPos !=  NULL)
	{
		strFileName  =  listFileName.GetNext(rPos);
		rStatus.m_attribute  =  CFile::normal;
		
		if (!DeleteFile(strFileName))
		{
			m_strStatus +=  "\r\nError deleting file " + strFileName + "...";
		}
		else
		{
			m_strStatus +=  "\r\nFile " + strFileName + " deleted OK!";
		}
		UpdateData(FALSE);
	}
	return true;
}

void CDelTempDlg::OnButtonAdd() 
{
	// TODO: Add your control notification handler code here
	CString	strFileType;
	m_listFileType.GetWindowText(strFileType);
	m_listFileType.AddString(strFileType);
	m_listFileType.SetCurSel(0);
	RefreshIni();
}

void CDelTempDlg::OnButtonBrowse() 
{
	// TODO: Add your control notification handler code here
	BROWSEINFO	bi;
	char	szDispName[MAX_PATH+1];
	memset(&bi,0,sizeof(BROWSEINFO));
	bi.hwndOwner = m_hWnd;
	bi.pszDisplayName = szDispName;
	bi.ulFlags = BIF_BROWSEINCLUDEFILES+BIF_EDITBOX;
	bi.lpszTitle = "Please select the folder u want to clean:";
	if(ITEMIDLIST *iil = SHBrowseForFolder(&bi))
	{
		SHGetPathFromIDList(iil, szDispName);
		m_strPath  =  szDispName;
		UpdateData(FALSE);
	}
}

void CDelTempDlg::OnButtonClear() 
{
	// TODO: Add your control notification handler code here
	m_strStatus = "";
	UpdateData(FALSE);
}

void CDelTempDlg::OnButtonDel() 
{
	// TODO: Add your control notification handler code here
	m_listFileType.DeleteString(m_listFileType.GetCurSel());
	m_listFileType.SetCurSel(0);
	RefreshIni();
}

void CDelTempDlg::RefreshIni()
{
	CString	strFileType;
	char	szBuf[MAX_PATH];
	WritePrivateProfileSection("Extentions",NULL,"DelTemp.ini");
	int	nTotal = m_listFileType.GetCount();
	WritePrivateProfileString("Num", "Total", itoa(nTotal,szBuf,10), "DelTemp.ini");
	for (int i=0; i<nTotal; i++)
	{
		m_listFileType.GetLBText(i,strFileType);
		WritePrivateProfileString("Extentions", itoa(i,szBuf,10), strFileType, "DelTemp.ini");
	}
}