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

📄 findfiledlg.cpp

📁 VC++编程
💻 CPP
字号:
/*********************************************************************

   Copyright (C) 2000 Smaller Animals Software, Inc.

   This software is provided 'as-is', without any express or implied
   warranty.  In no event will the authors be held liable for any damages
   arising from the use of this software.

   Permission is granted to anyone to use this software for any purpose,
   including commercial applications, and to alter it and redistribute it
   freely, subject to the following restrictions:

   1. The origin of this software must not be misrepresented; you must not
     claim that you wrote the original software. If you use this software
     in a product, an acknowledgment in the product documentation would be
     appreciated but is not required.

   2. Altered source versions must be plainly marked as such, and must not be
     misrepresented as being the original software.

   3. This notice may not be removed or altered from any source distribution.

   http://www.smalleranimals.com
   smallest@smalleranimals.com

**********************************************************************/

// FindFileDlg.cpp : implementation file
//

#include "stdafx.h"
#include "resource.h"
#include "FindFileDlg.h"
#include "FileFinder.h"

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

/////////////////////////////////////////////////////////////////////////////
// CFindFileDlg dialog

UINT _SearchForFile(void *p);

#define ICONS 12
HICON g_FindFileIconArray[ICONS];

CFindFileDlg::CFindFileDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CFindFileDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CFindFileDlg)
	m_csCurFolder = _T("");
	m_csOperation = _T("");
	//}}AFX_DATA_INIT

	m_bInThread = false;

	m_bFindSingleFile = false;	
	m_bRecurse = true;
	m_bCancel = false;
	
	m_bSearchNetworkDrives = false;
	m_bSearchRemovableDrives = false;
	m_bSearchCDROMDrives = false;

	m_bCancel = false;

	m_iCurIcon = 0;

	m_csTitle = "Searching for the file...";

	g_FindFileIconArray[0] = AfxGetApp()->LoadIcon(IDI_ICON1);
	g_FindFileIconArray[1] = AfxGetApp()->LoadIcon(IDI_ICON2);
	g_FindFileIconArray[2] = AfxGetApp()->LoadIcon(IDI_ICON3);
	g_FindFileIconArray[3] = AfxGetApp()->LoadIcon(IDI_ICON4);
	g_FindFileIconArray[4] = AfxGetApp()->LoadIcon(IDI_ICON5);
	g_FindFileIconArray[5] = AfxGetApp()->LoadIcon(IDI_ICON6);
	g_FindFileIconArray[6] = AfxGetApp()->LoadIcon(IDI_ICON7);
	g_FindFileIconArray[7] = AfxGetApp()->LoadIcon(IDI_ICON8);
	g_FindFileIconArray[8] = AfxGetApp()->LoadIcon(IDI_ICON9);
	g_FindFileIconArray[9] = AfxGetApp()->LoadIcon(IDI_ICON10);
	g_FindFileIconArray[10] = AfxGetApp()->LoadIcon(IDI_ICON11);
	g_FindFileIconArray[11] = AfxGetApp()->LoadIcon(IDI_ICON12);
}

//////////////////////////////////////////////////////////////////////

void CFindFileDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CFindFileDlg)
	DDX_Control(pDX, IDC_ANI, m_aniIcon);
	DDX_Text(pDX, IDC_CUR_FOLDER, m_csCurFolder);
	DDX_Text(pDX, IDC_OPERATION, m_csOperation);
	//}}AFX_DATA_MAP
}

//////////////////////////////////////////////////////////////////////

BEGIN_MESSAGE_MAP(CFindFileDlg, CDialog)
	//{{AFX_MSG_MAP(CFindFileDlg)
	ON_BN_CLICKED(ID_STOP, OnStop)
	ON_WM_SHOWWINDOW()
	ON_WM_TIMER()
	//}}AFX_MSG_MAP
	ON_MESSAGE(WM_BEGIN_SEARCH, OnBegin)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CFindFileDlg message handlers

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

	m_csCurFolder = "";	
	
	UpdateData(FALSE);

	SetWindowText(m_csTitle);

	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

//////////////////////////////////////////////////////////////////////

void CFindFileDlg::OnCancel() 
{
	if (!m_bInThread)
	{
		CDialog::OnCancel();
	}
	else
	{
		m_bCancel = true;
	}
}

//////////////////////////////////////////////////////////////////////////
//
//////////////////////////////////////////////////////////////////////////

void CFindFileDlg::MsgPump(DWORD dwLen)
{
	MSG m_msgCur;                   // current message

	CWinApp	*pWinApp = AfxGetApp();                     

    DWORD dInitTime = GetTickCount();

	while (::PeekMessage(&m_msgCur, NULL, NULL, NULL, PM_NOREMOVE)  &&
			(GetTickCount() - dInitTime < dwLen) )
	{
		pWinApp->PumpMessage();
	}

}

//////////////////////////////////////////////////////////////////////

void CFindFileDlg::OnStop() 
{
	m_bCancel = true;
}

//////////////////////////////////////////////////////////////////////

void CFindFileDlg::OnShowWindow(BOOL bShow, UINT nStatus) 
{
	CDialog::OnShowWindow(bShow, nStatus);
	
	PostMessage(WM_BEGIN_SEARCH);
}

//////////////////////////////////////////////////////////////////////

long CFindFileDlg::OnBegin(UINT u, LONG l)
{
	SetTimer(1, 300, NULL);

	// this will protect our data while the two threads access it
	InitializeCriticalSection(&g_findFileCritSection);

	// our shared data space
	searchStruct	ss;
	ss.m_bFindSingleFile		= m_bFindSingleFile;
	ss.m_csFindFile				= m_csFindFile;
	ss.m_bRecurse				= m_bRecurse;
	ss.m_csRootFolder			= m_csRootFolder;
	ss.m_bSearchNetworkDrives	= m_bSearchNetworkDrives;
	ss.m_bSearchRemovableDrives = m_bSearchRemovableDrives;
	ss.m_bSearchCDROMDrives		= m_bSearchCDROMDrives;

	ss.m_csaFoundFiles.RemoveAll();

	ss.m_bDone = false;
	ss.m_bOk = true;
	ss.m_bCancel = false;
	
	// create the thread
	CWinThread * pThread = AfxBeginThread(_SearchForFile, (LPVOID)&ss, THREAD_PRIORITY_NORMAL, 0, CREATE_SUSPENDED, NULL);

	// be nice
	if (pThread==NULL)
	{
		AfxMessageBox("Thread Creation Failed", MB_ICONEXCLAMATION);
	}
	else
	{

		// here we go!
		m_bInThread = true;

		// whee!!!
		pThread->ResumeThread();

		// while the thread is still alive...
		while (!GET_SAFE(ss.m_bDone)) 
		{
			// have we been cancelled?
			if (m_bCancel)
			{
				//if (!GET_SAFE(ss.m_bDone))
				{
					SET_SAFE(ss.m_bCancel, true);
				}
			}

			// update progress
			if (!GET_SAFE(ss.m_bDone))
			{
				m_csCurFolder = GET_SAFE(ss.m_csCurFolder);	
				m_csOperation = GET_SAFE(ss.m_csCurOperation);
			}

			UpdateData(FALSE);

			// push some msgs around
			MsgPump();
		}

		// done (the thread said so)
		m_bInThread = false;
		m_bCancel = false;
	}

	// don't need this any more
	DeleteCriticalSection(&g_findFileCritSection);

	// reset the UI
	m_csCurFolder = "";
	m_csOperation = "Done";
	UpdateData(FALSE);

	// copy to output
	m_csaFoundFiles.RemoveAll();
	m_csaFoundFiles.Copy(ss.m_csaFoundFiles);

	KillTimer(1);

	if (ss.m_bOk)
	{
		CDialog::OnOK();
	}
	else
	{
		CDialog::OnCancel();
	}
	
	return 0L;
}

//////////////////////////////////////////////////////////////////////

UINT _SearchForFile(void *p)
{
	// this is serious trouble...
	if (p==NULL)
	{
		return 0L;
	}

	// let this class do all of the work for us
	CFileFinder finder((searchStruct *)p);
	
	// do it
	bool ok = finder.FindFile();

	// set the output status
	SET_SAFE(((searchStruct *)p)->m_bOk, ok);

 	SET_SAFE(((searchStruct *)p)->m_bDone, true);

	AfxEndThread(0, TRUE);

	return 0;
}

//////////////////////////////////////////////////////////////////////

void CFindFileDlg::OnTimer(UINT nIDEvent) 
{
	if (nIDEvent==1)
	{
		// change the icon
		m_aniIcon.SetIcon(g_FindFileIconArray[m_iCurIcon]);

		m_iCurIcon++;

		if (m_iCurIcon >= ICONS)
		{
			m_iCurIcon = 0;
		}
		
		return;
	}
	
	CDialog::OnTimer(nIDEvent);
}

⌨️ 快捷键说明

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