让复选框的选择决定遍历文件的类型,最后把目录下选定类型的文件显示在列表控件中?

xixia07m 2017-07-10 11:43:18

// remarksecretDlg.h : 头文件
//

#pragma once
#include "afxcmn.h"
#include <string>


// 生成的消息映射函数
virtual BOOL OnInitDialog();
afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
afx_msg void OnPaint();
afx_msg HCURSOR OnQueryDragIcon();
DECLARE_MESSAGE_MAP()
public:
afx_msg void OnLvnItemchangedList1(NMHDR *pNMHDR, LRESULT *pResult);
CListCtrl mlist; //自注:控件变量在这里设置

//wstring suffixStr;
afx_msg void OnBnClickedButton1();
afx_msg void OnBnClickedCheck1();
afx_msg void OnBnClickedCheck2();
afx_msg void OnBnClickedCheck3();
afx_msg void OnBnClickedCheck4();
afx_msg void OnBnClickedCheck5();
};


/ remarksecretDlg.cpp : 实现文件
//


#include "stdafx.h"
#include "remarksecret.h"
#include "remarksecretDlg.h"
//#include "afxdialogex.h"
#include "Resource.h"
//#include "windows.h"
//#include <tchar.h>
#include <algorithm>
//#include <string>
#include <vector>
//#include <string>
//#include <iterator>
//#include <functional>



#ifdef _DEBUG
#define new DEBUG_NEW
#endif
using std::wstring;
wstring suffixStr;


struct FileInfo
{
std::wstring szFileName;
std::wstring szFilePath;
std::wstring szCreateTime;
};

BEGIN_MESSAGE_MAP(CremarksecretDlg, CDialogEx)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_NOTIFY(LVN_ITEMCHANGED, IDC_LIST1, &CremarksecretDlg::OnLvnItemchangedList1)
ON_BN_CLICKED(IDC_BUTTON1, &CremarksecretDlg::OnBnClickedButton1)
ON_BN_CLICKED(IDC_CHECK1, &CremarksecretDlg::OnBnClickedCheck1)
ON_BN_CLICKED(IDC_CHECK2, &CremarksecretDlg::OnBnClickedCheck2)
ON_BN_CLICKED(IDC_CHECK3, &CremarksecretDlg::OnBnClickedCheck3)
ON_BN_CLICKED(IDC_CHECK4, &CremarksecretDlg::OnBnClickedCheck4)
ON_BN_CLICKED(IDC_CHECK5, &CremarksecretDlg::OnBnClickedCheck5)
END_MESSAGE_MAP()

void CremarksecretDlg::OnBnClickedCheck1()
{
// TODO: 在此添加控件通知处理程序代码
//extern std::wstring suffixStr;
int nstatus=(((CButton*)GetDlgItem(IDC_CHECK1))->GetCheck());
if(nstatus=1)
{
suffixStr==_T("doc")||suffixStr==_T("docx");
}
else
{
suffixStr==_T(" ");
}
}


void CremarksecretDlg::OnBnClickedCheck2()
{
// TODO: 在此添加控件通知处理程序代码
//extern std::wstring suffixStr;
int nstatus=(((CButton*)GetDlgItem(IDC_CHECK2))->GetCheck());
if(nstatus=1)
{
suffixStr==_T("pdf");
}
else
{
suffixStr==_T(" ");
}
}


void CremarksecretDlg::OnBnClickedCheck3()
{
// TODO: 在此添加控件通知处理程序代码
//extern std::wstring suffixStr;
int nstatus=(((CButton*)GetDlgItem(IDC_CHECK3))->GetCheck());
if(nstatus=1)
{
suffixStr==_T("ppt")||suffixStr==_T("pptx");
}
else
{
suffixStr==_T(" ");
}
}


void CremarksecretDlg::OnBnClickedCheck4()
{
// TODO: 在此添加控件通知处理程序代码
//extern std::wstring suffixStr;
int nstatus=(((CButton*)GetDlgItem(IDC_CHECK4))->GetCheck());
if(nstatus=1)
{
suffixStr==_T("xls")||suffixStr==_T("xlsx"); ;
}
else
{
suffixStr==_T(" ");
}
}


void CremarksecretDlg::OnBnClickedCheck5()
{
// TODO: 在此添加控件通知处理程序代码
//extern std::wstring suffixStr;
int nstatus=(((CButton*)GetDlgItem(IDC_CHECK5))->GetCheck());
if(nstatus=1)
{
suffixStr==_T("txt");
}
else
{
suffixStr==_T(" ");
}

}


//递归遍历所有文件
void find (LPCTSTR lpPath,std::vector<FileInfo>&FileList)
{
//std::vector<FileInfo>fileList;
std::wstring szFind(lpPath);
WIN32_FIND_DATA FindFileData;
szFind+=_T("\\*.*");
HANDLE hFind=::FindFirstFile(szFind.c_str(),&FindFileData);
if(INVALID_HANDLE_VALUE==hFind) return;
do//while(1)
{
if(FindFileData.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY)
{//dir 目录在列表中可以双击打开,不要递归!
if(FindFileData.cFileName[0]!='.')
{
std::wstring szPath(lpPath);
szPath+=_T("\\");
szPath+=FindFileData.cFileName;
TRACE1("%s\n",szPath.c_str());
find(szPath.c_str(),FileList); //自注:这里小写的fileList不行。
}
}
else
{//file
std::wstring strFileName=FindFileData.cFileName;//文件名
/*std::wstring*/suffixStr=strFileName.substr(strFileName.find_last_of('.')+1);
void OnBnClickedCheck5();
if(suffixStr==_T("txt")) //根据后缀选出自己感兴趣的文件,比如txt文件,注意大小写,如果想不区分大小写,需要自己统一转化为大写或小写然后比较
{
FILETIME ft=FindFileData.ftCreationTime; //取得创建时间
SYSTEMTIME st={0};
TCHAR buf[64]={0};
FileTimeToLocalFileTime(&(FindFileData.ftCreationTime),&ft); //转换成本地时间
FileTimeToSystemTime(&ft,&st); //在这句之前加上上一句,就不用再小时那里+8了。
swprintf_s(buf,_T("%4d年-%02d月-%02d日%02d:%02d:%02d"),st.wYear,st.wMonth,st.wDay,st.wHour/*北京时间*/,st.wMinute,st.wSecond);
std::wstring strFilePath=lpPath;
strFilePath+=_T("\\");
strFilePath+=FindFileData.cFileName; //文件全路径名
FileInfo fi; //自注:结构变量不能代入
fi.szFileName=strFileName;
fi.szFilePath=strFilePath;
fi.szCreateTime=buf;
TRACE1("%s\n",strFileName.c_str()); //自注:这句是后加上的
FileList.push_back(fi); //自注:push_back(fi)是什么意思?
//fi=FileList;
}
void OnBnClickedCheck1();
if(suffixStr==_T("doc")) //根据后缀选出自己感兴趣的文件,比如txt文件,注意大小写,如果想不区分大小写,需要自己统一转化为大写或小写然后比较
{
FILETIME ft=FindFileData.ftCreationTime; //取得创建时间
SYSTEMTIME st={0};
TCHAR buf[64]={0};
FileTimeToLocalFileTime(&(FindFileData.ftCreationTime),&ft); //转换成本地时间
FileTimeToSystemTime(&ft,&st); //在这句之前加上上一句,就不用再小时那里+8了。
swprintf_s(buf,_T("%4d年-%02d月-%02d日%02d:%02d:%02d"),st.wYear,st.wMonth,st.wDay,st.wHour,st.wMinute,st.wSecond);
std::wstring strFilePath=lpPath;
strFilePath+=_T("\\");
strFilePath+=FindFileData.cFileName; //文件全路径名
FileInfo fi;
fi.szFileName=strFileName;
fi.szFilePath=strFilePath;
fi.szCreateTime=buf;
FileList.push_back(fi);
}
else if(suffixStr==_T("docx")) //根据后缀选出自己感兴趣的文件,比如txt文件,注意大小写,如果想不区分大小写,需要自己统一转化为大写或小写然后比较
{
FILETIME ft=FindFileData.ftCreationTime; //取得创建时间
SYSTEMTIME st={0};
TCHAR buf[64]={0};
FileTimeToLocalFileTime(&(FindFileData.ftCreationTime),&ft); //转换成本地时间
FileTimeToSystemTime(&ft,&st); //在这句之前加上上一句,就不用再小时那里+8了。
swprintf_s(buf,_T("%4d年-%02d月-%02d日%02d:%02d:%02d"),st.wYear,st.wMonth,st.wDay,st.wHour,st.wMinute,st.wSecond);
std::wstring strFilePath=lpPath;
strFilePath+=FindFileData.cFileName; //文件全路径名
FileInfo fi;
fi.szFileName=strFileName;
fi.szFilePath=strFilePath;
fi.szCreateTime=buf;
FileList.push_back(fi);
}
void OnBnClickedCheck2();

}
}while(::FindNextFile(hFind,&FindFileData));
}
//FindClose(hFind);

#define BIF_NEWDIALOGSTYLE 0x0040

// CremarksecretDlg 消息处理程序


// TODO: 在此添加额外的初始化代码
mlist.SetExtendedStyle(mlist.GetExtendedStyle()|LVS_EX_GRIDLINES|LVS_EX_FULLROWSELECT);
mlist.InsertColumn(0,_T("操作"),LVCFMT_LEFT,80);//100指定每列宽度
mlist.InsertColumn(1,_T("文件名"),LVCFMT_LEFT,500);
mlist.InsertColumn(2,_T("创建时间"),LVCFMT_LEFT,120);
mlist.InsertColumn(3,_T("路径"),LVCFMT_LEFT,360);
return TRUE; // 除非将焦点设置到控件,否则返回 TRUE
}

void CremarksecretDlg::OnBnClickedButton1()
{
// TODO: 在此添加控件通知处理程序代码 自注:以下是我添加的代码
BROWSEINFO bi; //BROWSEINFO结构有很多成员参数可以自己设置
ZeroMemory(&bi,sizeof(BROWSEINFO)); //将以&bi为起始地址,大小为sizeof(BROWSEINFO)的内存区域用0填充
bi.ulFlags=BIF_BROWSEINCLUDEFILES; //设置弹出的对话框属性。此处意思为包含文件。如果不设这个值,默认的是只有文件夹。ulFlags还可以定义很多别的属性
bi.ulFlags=BIF_NEWDIALOGSTYLE; //窗口可以调整大小,有新建文件夹按钮
LPITEMIDLIST pidl = SHBrowseForFolder(&bi); //Displays a dialog box enabling the user to select a Shell folder.
TCHAR * path = new TCHAR[MAX_PATH]; //自注:点击按钮获得的地址在path变量中
if(pidl!= NULL)
{
SHGetPathFromIDList(pidl,path); //Converts an item identifier list to a file system path
//MessageBox(path);
}

//FileInfo fileList;
using std::vector;
vector<FileInfo>FileList; //自注:程序不承认FileList是个结构变量
//std::vector<FileInfo>fileList;
find(/*_T("D:\\xsmposc\\XSMPos"*/path,FileList); //取得文件信息,可自行插入列表框
//FileInfo fileList;
UINT nCount=FileList.size(); //得到控件的行数
//Filelist=fi;
int i;
CString empty;
CString name;
CString time;
CString mypath;
for(i=0;i<nCount;i++)
{
empty.Format(_T(""),i+1);
mlist.InsertItem(i,empty);
name.Format(_T("%s"),FileList[i].szFileName.c_str());
mlist.SetItemText(i,1,name); //插入列
time.Format(_T("%s"),FileList[i].szCreateTime.c_str());
mlist.SetItemText(i,2,time);
mypath.Format(_T("%s"),FileList[i].szFilePath.c_str());
mlist.SetItemText(i,3,mypath);
}

}



这个程序中有5个复选框,它们分别是doc/docx, pdf , ppt/pptx, xls/xlsx, txt,现在要按照选取的文件类型把点击文件按钮后出现的浏览文件夹对话框中选择的目录下的特定类型的
文件都遍历出来,然后显示在列表控件CListctrl中,分别显示文件名、创建时间、路径,请问要想实现控制文件类型的遍历如何改动find()函数,请高手明示,多谢。
...全文
658 64 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
64 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2017-07-24
  • 打赏
  • 举报
回复
system("dir /b /a-d c:\\*.* >d:\\allfiles.txt"); //读文件d:\\allfiles.txt的内容即C:\\下所有文件的名字 system("dir /b /a-d /s c:\\*.* >d:\\allfilesinsub.txt"); //读文件d:\\allfilesinsub.txt的内容即C:\\下所有文件的名字包含子目录 system("dir /b /ad c:\\*.* >d:\\alldirs.txt"); //读文件d:\\alldirs.txt的内容即C:\\下所有子目录的名字 请记住,能用shell命令获取文件、文件夹信息或者操作文件、文件夹最好用shell命令获取或者操作,而不要用各种API获取或者操作,因为当遇到非法文件夹名或非法文件名或非法文件长度、非法文件日期、压缩文件、链接文件、稀疏文件……等各种意料之外的情况时,API会处理的不全面或陷入死循环,而shell命令不会。 如果嫌system黑窗口一闪,将system("...")替换为WinExec("cmd /c ...",SW_HIDE);
schlafenhamster 2017-07-21
  • 打赏
  • 举报
回复
再给你一次代码

/////////////////////////////////////////////////////////////////////////////////////
string suffixStr;// UNICODE wstring

BOOL CShowdirDlg::GetTypes(string &str)
{
	str="";// 1002 -> 1006
	char tmp[10];
	for (UINT n=IDC_CHECK1;n<=IDC_CHECK5; n++) 
	{
		if(((CButton*)GetDlgItem(n))->GetCheck() == 1)
		{
			tmp[0]=0;
			GetDlgItem(n)->GetWindowText(tmp,9);
			str += tmp;
		}
	}
	if(str.compare("")==0) return FALSE;
	str += '.';// seperater
	return TRUE;
}
void find(LPCTSTR lpPath,vector<FileInfo>& FileList)
{
	string szFind(lpPath);
	WIN32_FIND_DATA FindFileData;
	szFind+=_T("\\*.*");
	HANDLE hFind=::FindFirstFile(szFind.c_str(),&FindFileData);
	if(INVALID_HANDLE_VALUE==hFind) return;
//
	do
	{
		if(FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
		{// dir
			if(FindFileData.cFileName[0] != '.') 
			{
				string szPath(lpPath);
				szPath+=_T("\\");
				szPath+=FindFileData.cFileName;
				//TRACE1("%s\n",szPath.c_str());
				find(szPath.c_str(),FileList);
			}
		}
		else
		{// file
			string strFileName=FindFileData.cFileName;//文件名
			//TRACE1("%s\n",strFileName.c_str());
			int at = strFileName.find_last_of('.'); 
			if(at<0) continue;// file without extention
			string ext=strFileName.substr(at);
			//TRACE1("%s\n",ext.c_str());
//
			at=0;// reset
			while(((int)at=suffixStr.find(ext,at))>=0)// find type
		//	if(stricmp(suffixStr.c_str(),_T(".txt"))==0)
			{// not suffixStr.c_str()==_T(".txt") ; never == !
				//afxDump << at << "\n";
				at+=ext.size();// move to seperetor '.'
				if(suffixStr[at]!='.') continue;// not wanted !
// 
				FILETIME ft=FindFileData.ftCreationTime;
				SYSTEMTIME st={0};
				TCHAR buf[64]={0};
				FileTimeToLocalFileTime(&(FindFileData.ftCreationTime),&ft);
				FileTimeToSystemTime(&ft,&st); 
				sprintf(buf,_T("%4d年%02d月%02d日%02d:%02d:%02d"),
					st.wYear,st.wMonth,st.wDay,st.wHour/*北京时间*/,st.wMinute,st.wSecond);
				string strFilePath=lpPath;
				FileInfo fi;
				fi.szFileName=strFileName;
				fi.szFilePath=strFilePath;
				fi.szCreateTime=buf;
				//TRACE1("%s\n",strFileName.c_str());
				FileList.push_back(fi);
			}
		}
	} while (::FindNextFile (hFind, &FindFileData));
}

#define BIF_NEWDIALOGSTYLE 0x0040
void CShowdirDlg::OnButton1() 
{
//	static LPITEMIDLIST s_pidl=0;
// new file types
	if(!GetTypes(suffixStr))
	{
		AfxMessageBox("No file extention selected!");
		return;
	}
//	afxDump << suffixStr.c_str() << "\n";
// get directory
	BROWSEINFO bi;
    ZeroMemory(&bi,sizeof(BROWSEINFO));
//	bi.ulFlags=BIF_RETURNFSANCESTORS;
//	bi.ulFlags=BIF_BROWSEINCLUDEFILES;
	bi.ulFlags |= BIF_NEWDIALOGSTYLE;//窗口可以调整大小,有新建文件夹按钮
    bi.lpszTitle="指定要处理的文件夹";
//    if(s_pidl) 	bi.pidlRoot= s_pidl;
// Displays a dialog box enabling the user to select a Shell folder.
	LPITEMIDLIST pidl = SHBrowseForFolder(&bi);
//
	TCHAR * path = new TCHAR[MAX_PATH];
    if(pidl != NULL)
    {// Converts an item identifier list to a file system path
        SHGetPathFromIDList(pidl,path); 
    }
	else
	{
		return;
	}
//	if(pidl) s_pidl=pidl;
// window ruined by SHBrowseForFolder
	m_list.DeleteAllItems();
	RedrawWindow();
// get files info
	CWaitCursor wait;
    vector<FileInfo>fileList;
    find(path,fileList);
// fill list
    UINT nCount=fileList.size();
    UINT i;
    CString empty;
    CString name;
    CString time;
    CString mypath;
	m_list.LockWindowUpdate();
	for(i=0;i<nCount;i++)
	{
		empty.Format("%d",i);
		m_list.InsertItem(i,empty);
		name.Format("%s",fileList[i].szFileName.c_str());
		m_list.SetItemText(i,1,name);
		time.Format("%s",fileList[i].szCreateTime.c_str());
		m_list.SetItemText(i,2,time);
		mypath.Format("%s",fileList[i].szFilePath.c_str());
		m_list.SetItemText(i,3,mypath);
	}
	m_list.UnlockWindowUpdate();
}
schlafenhamster 2017-07-21
  • 打赏
  • 举报
回复
1 给你说 不要 OnBnClickedCheck1() 。。。 2 多加一个 即 “doc” “docx” 是 2个按钮
xixia07m 2017-07-21
  • 打赏
  • 举报
回复
我的复选框单击响应消息函数如下:我现在想让复选框选中后,让suffixStr对应的后缀名能涵盖doc和docx,所以按如下方法写了即用"?",但是实际生成程序时证明这样写不行,请问有没有类似的可以成功实现涵盖功能的类似?这样的字符呢?多谢!

void CremarksecretDlg::OnBnClickedCheck1()
{
    // TODO: 在此添加控件通知处理程序代码
    //extern std::wstring suffixStr;
    int nstatus=(((CButton*)GetDlgItem(IDC_CHECK1))->GetCheck());
    if(nstatus=1)
    {
       suffixStr==_T("doc?")      //||suffixStr==_T("docx");   
    }
    else
    {
       suffixStr==_T(" ");
    }
}
schlafenhamster 2017-07-20
  • 打赏
  • 举报
回复
头文件

// showdirDlg.h : header file
//

#if !defined(AFX_SHOWDIRDLG_H__F25B36DD_2D3F_4037_A387_39BD82DE3D8F__INCLUDED_)
#define AFX_SHOWDIRDLG_H__F25B36DD_2D3F_4037_A387_39BD82DE3D8F__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

/////////////////////////////////////////////////////////////////////////////
// CShowdirDlg dialog

class CShowdirDlg : public CDialog
{
// Construction
public:
	CShowdirDlg(CWnd* pParent = NULL);	// standard constructor
	BOOL GetTypes(string &str);
// Dialog Data
	//{{AFX_DATA(CShowdirDlg)
	enum { IDD = IDD_SHOWDIR_DIALOG };
	CListCtrl	m_list;
	//}}AFX_DATA

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

// Implementation
protected:
	HICON m_hIcon;

	// Generated message map functions
	//{{AFX_MSG(CShowdirDlg)
	virtual BOOL OnInitDialog();
	afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
	afx_msg void OnPaint();
	afx_msg HCURSOR OnQueryDragIcon();
	afx_msg void OnButton1();
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.

#endif // !defined(AFX_SHOWDIRDLG_H__F25B36DD_2D3F_4037_A387_39BD82DE3D8F__INCLUDED_)
schlafenhamster 2017-07-20
  • 打赏
  • 举报
回复

// showdirDlg.cpp : implementation file
//

#include "stdafx.h"

#include "showdir.h"
#include "showdirDlg.h"

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

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
typedef struct FileInfo
{
    string szFileName;
    string szFilePath;
    string szCreateTime;
}FileInfo;

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()

/////////////////////////////////////////////////////////////////////////////
// CShowdirDlg dialog

CShowdirDlg::CShowdirDlg(CWnd* pParent /*=NULL*/)
: CDialog(CShowdirDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CShowdirDlg)
	// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CShowdirDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CShowdirDlg)
	DDX_Control(pDX, IDC_LIST1, m_list);
	//}}AFX_DATA_MAP
}

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

/////////////////////////////////////////////////////////////////////////////
// CShowdirDlg message handlers

BOOL CShowdirDlg::OnInitDialog()
{
    // IDM_ABOUTBOX 必须在系统命令范围内。
	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
	
	// TODO: Add extra initialization here
	char a[]="";//elloworld"; =00 CC CC CC ;
int n=sizeof("helloworld");//10+'0'
	n=sizeof("");//1
	n=sizeof("0");//2
	n=sizeof("01");//3
	n=sizeof("012");//4

	//    ShowWindow(SW_MINIMIZE);
	
    m_list.SetExtendedStyle(m_list.GetExtendedStyle()|LVS_EX_GRIDLINES|LVS_EX_FULLROWSELECT); 
    m_list.InsertColumn(0,_T(""),LVCFMT_LEFT,40);
    m_list.InsertColumn(1,_T("文件名"),LVCFMT_LEFT,150);
    m_list.InsertColumn(2,_T("创建时间"),LVCFMT_LEFT,200);
    m_list.InsertColumn(3,_T("路径"),LVCFMT_LEFT,350);
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CShowdirDlg::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 CShowdirDlg::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 CShowdirDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}
/////////////////////////////////////////////////////////////////////////////////////
string suffixStr;// UNICODE wstring

BOOL CShowdirDlg::GetTypes(string &str)
{
	str="";// 1002 -> 1006
	char tmp[10];
	for (UINT n=IDC_CHECK1;n<=IDC_CHECK5; n++) 
	{
		if(((CButton*)GetDlgItem(n))->GetCheck() == 1)
		{
			tmp[0]=0;
			GetDlgItem(n)->GetWindowText(tmp,9);
			str += tmp;
		}
	}
	if(str.c_str()=="") return FALSE;
	str += '.';// seperater
	return TRUE;
}

void find(LPCTSTR lpPath,vector<FileInfo>& FileList)
{
	string szFind(lpPath);
	WIN32_FIND_DATA FindFileData;
	szFind+=_T("\\*.*");
	HANDLE hFind=::FindFirstFile(szFind.c_str(),&FindFileData);
	if(INVALID_HANDLE_VALUE==hFind) return;
	do
	{
		if(FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
		{// dir 目录 在 列表中 可以 双击 打开 ,不要 递归 !
			if(FindFileData.cFileName[0] != '.') 
			{
				string szPath(lpPath);
				szPath+=_T("\\");
				szPath+=FindFileData.cFileName;
				//TRACE1("%s\n",szPath.c_str());
				find(szPath.c_str(),FileList);
			}
		}
		else
		{// file
			string strFileName=FindFileData.cFileName;//文件名
			//TRACE1("%s\n",strFileName.c_str());
			int at = strFileName.find_last_of('.'); 
			if(at<0) continue;// file without extention
			string ext=strFileName.substr(at);
			TRACE1("%s\n",ext.c_str());
//
			at=0;// reset
			while(((int)at=suffixStr.find(ext,at))>=0)// find type
		//	if(stricmp(suffixStr.c_str(),_T(".txt"))==0)
			{// not suffixStr.c_str()==_T(".txt") ; never == !
				//afxDump << at << "\n";
				at+=ext.size();// move to seperetor '.'
				if(suffixStr[at]!='.') continue;// not wanted !
// 
				FILETIME ft=FindFileData.ftCreationTime;   //取得创建时间
				SYSTEMTIME st={0};
				TCHAR buf[64]={0};
				FileTimeToLocalFileTime(&(FindFileData.ftCreationTime),&ft);    //转换成本地时间
				FileTimeToSystemTime(&ft,&st);     //在这句之前加上上一句,就不用再小时那里+8了。
				sprintf(buf,_T("%4d年%02d月%02d日%02d:%02d:%02d"),st.wYear,st.wMonth,st.wDay,st.wHour/*北京时间*/,st.wMinute,st.wSecond);
				string strFilePath=lpPath;
				//strFilePath+=_T("\\");
				//strFilePath+=FindFileData.cFileName;   //文件全路径名
				FileInfo fi;
				fi.szFileName=strFileName;
				fi.szFilePath=strFilePath;
				fi.szCreateTime=buf;
				//TRACE1("%s\n",strFileName.c_str());
				FileList.push_back(fi);
			}
		}
	} while (::FindNextFile (hFind, &FindFileData));
}

#define BIF_NEWDIALOGSTYLE 0x0040
void CShowdirDlg::OnButton1() 
{
	// TODO: Add your control notification handler code here
	CWaitCursor wait;
//
	BROWSEINFO bi;                       //BROWSEINFO结构有很多成员参数可以自己设置
    ZeroMemory(&bi,sizeof(BROWSEINFO));  //将以&bi为起始地址,大小为sizeof(BROWSEINFO)的内存区域用0填充
    bi.ulFlags=BIF_BROWSEINCLUDEFILES;   //设置弹出的对话框属性。此处意思为包含文件。如果不设这个值,默认的是只有文件夹。ulFlags还可以定义很多别的属性
    bi.ulFlags=BIF_NEWDIALOGSTYLE;       //窗口可以调整大小,有新建文件夹按钮
    bi.lpszTitle="指定要处理的文件夹";   //在窗口内显示提示用户的语句
    LPITEMIDLIST pidl = SHBrowseForFolder(&bi);   //Displays a dialog box enabling the user to select a Shell folder.
//
    TCHAR * path = new TCHAR[MAX_PATH];
    if(pidl != NULL)
    {
        SHGetPathFromIDList(pidl,path); //Converts an item identifier list to a file system path
    }
// window ruined by SHBrowseForFolder
	m_list.DeleteAllItems();
	RedrawWindow();
// new file types
	GetTypes(suffixStr);
//	afxDump << suffixStr.c_str() << "\n";
    vector<FileInfo>fileList;
    find(path,fileList);           //取得文件信息,可自行插入列表框
    UINT nCount=fileList.size();   //得到控件的行数
    UINT i;
    CString empty;
    CString name;
    CString time;
    CString mypath;
	m_list.LockWindowUpdate();
	for(i=0;i<nCount;i++)
	{
		empty.Format("%d",i);
		m_list.InsertItem(i,empty);
		name.Format("%s",fileList[i].szFileName.c_str());
		m_list.SetItemText(i,1,name);
		time.Format("%s",fileList[i].szCreateTime.c_str());
		m_list.SetItemText(i,2,time);
		mypath.Format("%s",fileList[i].szFilePath.c_str());
		m_list.SetItemText(i,3,mypath);
	}
	m_list.UnlockWindowUpdate();
}
稍有改动, 因为 原来的 suffixStr.find 只 部分匹配,如 选 ‘.cpp’ 那么 "*.c" 也会 被选中。
xixia07m 2017-07-20
  • 打赏
  • 举报
回复
你能把你.cpp文件的代码都贴过来我看看吗,我现在搞不清楚有些细节。
schlafenhamster 2017-07-20
  • 打赏
  • 举报
回复
BOOL CShowdirDlg::GetTypes(string &str){} // 无‘;‘ 是定义 BOOL CShowdirDlg::GetTypes(string &str); // 是声明 不是有个 全局变量 string suffixStr;// 把它作为 实参 传递
schlafenhamster 2017-07-19
  • 打赏
  • 举报
回复

#define BIF_NEWDIALOGSTYLE 0x0040
void CShowdirDlg::OnButton1() 
{
	// TODO: Add your control notification handler code here
	CWaitCursor wait;

	BROWSEINFO bi;                       //BROWSEINFO结构有很多成员参数可以自己设置
    ZeroMemory(&bi,sizeof(BROWSEINFO));  //将以&bi为起始地址,大小为sizeof(BROWSEINFO)的内存区域用0填充
    bi.ulFlags=BIF_BROWSEINCLUDEFILES;   //设置弹出的对话框属性。此处意思为包含文件。如果不设这个值,默认的是只有文件夹。ulFlags还可以定义很多别的属性
    bi.ulFlags=BIF_NEWDIALOGSTYLE;       //窗口可以调整大小,有新建文件夹按钮
    bi.lpszTitle="指定要处理的文件夹";   //在窗口内显示提示用户的语句
    LPITEMIDLIST pidl = SHBrowseForFolder(&bi);   //Displays a dialog box enabling the user to select a Shell folder.
//
    TCHAR * path = new TCHAR[MAX_PATH];
    if(pidl != NULL)
    {
        SHGetPathFromIDList(pidl,path); //Converts an item identifier list to a file system path
    }
//
	GetTypes(suffixStr);
//
    vector<FileInfo>fileList;
    find(path,fileList);           //取得文件信息,可自行插入列表框
    UINT nCount=fileList.size();   //得到控件的行数
    UINT i;
    CString empty;
    CString name;
    CString time;
    CString mypath;
	m_list.LockWindowUpdate();
	for(i=0;i<nCount;i++)
	{
		empty.Format("%d",i);
		m_list.InsertItem(i,empty);
		name.Format("%s",fileList[i].szFileName.c_str());
		m_list.SetItemText(i,1,name);
		time.Format("%s",fileList[i].szCreateTime.c_str());
		m_list.SetItemText(i,2,time);
		mypath.Format("%s",fileList[i].szFilePath.c_str());
		m_list.SetItemText(i,3,mypath);
	}
	m_list.UnlockWindowUpdate();
}
给过你了 吧。
schlafenhamster 2017-07-19
  • 打赏
  • 举报
回复
重要部分的代码 都给你了 find 直接用 GetTypes(string &str) 改个 对话框名
xixia07m 2017-07-19
  • 打赏
  • 举报
回复
请问schlafenhamster,您贴出来的那张成功程序的截图重要部分的代码能给我看看吗?
Eleven 2017-07-19
  • 打赏
  • 举报
回复
引用 43 楼 xixia07m 的回复:
现在编译通过了,但是生成的程序,功能不对,我只选了pdf类型的文件,而结果是所选目录下的pdf、doc、docx文件都出来了,这怎么办呢?是不是我的复选框单击消息响应程序有问题啊?
你枚举出所有文件以后,自己过滤一下即可~
schlafenhamster 2017-07-19
  • 打赏
  • 举报
回复
程序中 不是有 TRACE 吗 ? 打开看看 ! 另外 if((int)suffixStr.find(ext)>=0) 中 (int) 转换 很 重要 因为 .find(ext) 返回 UINT 没知道时 返回 -1(0xFFFFFFFF)
xixia07m 2017-07-19
  • 打赏
  • 举报
回复
你在这里仅仅是个声明而已啊!
xixia07m 2017-07-19
  • 打赏
  • 举报
回复
引用 54 楼 schlafenhamster 的回复:
///////////////////////////////////////////////////////////////////////////////////// string suffixStr;// UNICODE wstring // GetTypes(suffixStr);// 实参 是 suffixStr , BOOL CShowdirDlg::GetTypes(string &str)// 形参是 &str
那实参的真实值是怎么来的,怎么形成的?
xixia07m 2017-07-19
  • 打赏
  • 举报
回复
现在编译通过了,但是生成的程序,功能不对,我只选了pdf类型的文件,而结果是所选目录下的pdf、doc、docx文件都出来了,这怎么办呢?是不是我的复选框单击消息响应程序有问题啊?
schlafenhamster 2017-07-19
  • 打赏
  • 举报
回复
///////////////////////////////////////////////////////////////////////////////////// string suffixStr;// UNICODE wstring // GetTypes(suffixStr);// 实参 是 suffixStr , BOOL CShowdirDlg::GetTypes(string &str)// 形参是 &str
schlafenhamster 2017-07-19
  • 打赏
  • 举报
回复
5个复选框 在资源中给值 必须是 “.xxx” 不要按钮响应。 即 删除 OnBnClickedCheck1(),.........OnBnClickedCheck5()。 经过 GetTypes(string &str) 后 扩展名 被 连接到一起,必须是: .doc.pdf.ppt.xls.xlsx,txt,
xixia07m 2017-07-19
  • 打赏
  • 举报
回复
那你 GetTypes(suffixStr);这句的实参是从哪里得到的?最好能有代码提示。 find函数中 if((int)suffixStr.find(extention)>=0)这句的形参该怎么理解?
schlafenhamster 2017-07-19
  • 打赏
  • 举报
回复
5个复选框 的 text 不是 doc/docx,pdf,ppt/pptx,xls/xlsx,txt 应该是 .doc .docx .pdf .pptx .xls .xlsx .txt 这些text 在资源中已经 给定,不需要 点击命令,也不需要改变。 经过 GetTypes(string &str) (只需要 IDC_CHECK1 。。。) 后 为 “.doc.docx.pdf.ppt.pptx.xls.xlsx.txt " 没有 “,” 也没有 “/”.
加载更多回复(44)

16,548

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • AIGC Browser
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧