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

📄 文件查找dlg.cpp

📁 一个简单的文件查找程序
💻 CPP
字号:
// 文件查找Dlg.cpp : implementation file
//

#include "stdafx.h"
#include "文件查找.h"
#include "文件查找Dlg.h"
//#include <macwin32.h>
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
bool flag=false;
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
/*typedef struct _FILE_INFO {
  TCHAR szFileTitle[128]; //文件的标题名
  DWORD dwFileAttributes; //文件的属性
  FILETIME ftCreationTime; //文件的创建时间
  FILETIME ftLastAccessTime; //文件的最后访问时间
  FILETIME ftLastWriteTime; //文件的最后修改时间
  DWORD nFileSizeHigh; //文件大小的高位双字
  DWORD nFileSizeLow; //文件大小的低位双字
  DWORD dwReserved0; //保留,为0 
  DWORD dwReserved1; //保留,为0
} FILE_INFO, * PFILE_INFO;
*/

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMyDlg dialog

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

void CMyDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CMyDlg)
	DDX_Text(pDX, IDC_EDIT1, m_infordisplay);
	DDX_Text(pDX, IDC_EDIT2, m_filename);
	DDX_Text(pDX, IDC_EDIT3, m_filepath);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CMyDlg, CDialog)
	//{{AFX_MSG_MAP(CMyDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
	ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMyDlg message handlers

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

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// 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
 	flag=false;
	// TODO: Add extra initialization here
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CMyDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// 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 CMyDlg::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();
	}
}

  HCURSOR CMyDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CMyDlg::OnButton2() 
{ m_infordisplay="";
  UpdateData(TRUE);
if(!m_filename.GetLength())
{	AfxMessageBox("请输入要查找的文件");
	return ;
} 
  WIN32_FIND_DATA fd;
  CString listfile;HANDLE hd;
  DWORD  length1;   
  searchfile(m_filepath,m_filename);	UpdateData(TRUE); 
  hd=::FindFirstFile((LPCTSTR)m_filepath,&fd);//开始查找
     if(hd==INVALID_HANDLE_VALUE) 
	 {MessageBox("文件未找到 "); return; }
//else//
//{
 
 //UpdateData(TRUE);
 //hd=::FindFirstFile((LPCTSTR)m_filepath,&fd);}//开始查找
//if(hd==INVALID_HANDLE_VALUE) 
//{return;} 
length1=GetFileSize(hd,NULL);
CString length2;
length2.Format("%d",length1);
m_infordisplay=m_infordisplay+ "文件名:"+fd.cFileName+"\r\n"; 
m_infordisplay=m_infordisplay+"文件类型:";
DWORD leixing1=GetFileType(hd);
switch(leixing1)
{case FILE_TYPE_UNKNOWN:
  m_infordisplay=m_infordisplay+"文件类型未知"+"\r\n";break;
case FILE_TYPE_DISK:
	 m_infordisplay=m_infordisplay+"属于磁盘文件"+"\r\n";break;
case FILE_TYPE_CHAR:
     m_infordisplay=m_infordisplay+"文件是一个控制台或打印机"+"\r\n";break;
case FILE_TYPE_PIPE:
     m_infordisplay=m_infordisplay+"文件是个管道"+"\r\n";break;
}
CFileStatus status; 
CFile::GetStatus( (LPCTSTR)m_filepath, status );
CString  num;
num.Format("%d",status.m_size );

m_infordisplay=m_infordisplay+"文件大小:"+num +"字节\r\n";
CString time1= status.m_ctime.Format("%Y年%m月%d日 %H:%M:%S");
m_infordisplay=m_infordisplay+"创建时间:"+time1 +"\r\n"; 
time1= status.m_atime.Format("%Y年%m月%d日 %H:%M:%S");
m_infordisplay=m_infordisplay+"上次修改时间:"+time1 +"\r\n"; 
DWORD  reValue=GetFileAttributes(m_filepath);
	if(reValue==FILE_ATTRIBUTE_ARCHIVE)
	m_infordisplay=m_infordisplay+"文件属性:只读" +"\r\n"; 	
	if(reValue==FILE_ATTRIBUTE_HIDDEN)
	m_infordisplay=m_infordisplay+"文件属性:隐藏" +"\r\n"; 
	if(reValue==FILE_ATTRIBUTE_READONLY)
	m_infordisplay=m_infordisplay+"文件属性:存档" +"\r\n"; 
 	UpdateData(FALSE);
FindClose(hd);//关闭查找
}

void CMyDlg::OnButton1() //浏览
{	flag=true;
	// TODO: Add your control notification handler code here
	CFileDialog filedialog(TRUE,NULL,"",OFN_HIDEREADONLY,"All Files(*.*)|*.*||",NULL);
    filedialog.DoModal();
	cstrFilePath = filedialog.GetPathName();
    m_filepath=cstrFilePath;
	m_filename=cstrFilePath;
	UpdateData(FALSE);

	 //MessageBox(cstrFilePath);


}

void CMyDlg::searchfile(CString strDir, CString strFile)
{CFileFind ff; 
	CString szDir=strDir;
	if(szDir.Right(1)!="\\")
		szDir+="\\";
	szDir+="*.*";
	BOOL res=ff.FindFile(szDir);
	while(res)
	{	res=ff.FindNextFile();
		if(ff.GetFileName()==strFile)
		{	m_filepath= ff.GetFilePath() ;
			UpdateData(FALSE);
		}
		if(ff.IsDirectory()&&!ff.IsDots())
		{	searchfile(ff.GetFilePath(),strFile);
			UpdateData(FALSE);
		}
	}
}

⌨️ 快捷键说明

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