📄 serch.cpp
字号:
// Serch.cpp : implementation file
//
#include "stdafx.h"
#include "testlistbox.h"
#include "Serch.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CSerch dialog
CSerch::CSerch(CWnd* pParent /*=NULL*/)
: CDialog(CSerch::IDD, pParent)
{
//{{AFX_DATA_INIT(CSerch)
m_sFileMask = _T("");
m_sBaseFolder = _T("");
m_myList = _T("");
m_bSearching = FALSE;
m_bSubFolders = TRUE;
m_Test = _T("");
//}}AFX_DATA_INIT
}
void CSerch::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CSerch)
DDX_Control(pDX, IDC_LIST2, m_newList);
DDX_Control(pDX, IDC_LIST1, m_list);
DDX_Text(pDX, IDC_EDITMAST, m_sFileMask);
DDX_Text(pDX, IDC_EDITROOT, m_sBaseFolder);
DDX_LBString(pDX, IDC_LIST2, m_myList);
DDX_Text(pDX, IDC_EDITTEST, m_Test);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CSerch, CDialog)
//{{AFX_MSG_MAP(CSerch)
ON_BN_CLICKED(IDC_BTBROWSE, OnBtbrowse)
ON_BN_CLICKED(IDC_Serch, OnSerch)
ON_LBN_DBLCLK(IDC_LIST2, OnDBClick)
ON_LBN_SELCHANGE(IDC_LIST2, OnSelchangeList2)
ON_BN_CLICKED(IDC_Close, OnClose)
ON_BN_CLICKED(IDC_BUTTON1, OnContendSerch)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CSerch message handlers
void CSerch::OnBtbrowse()
{
//SR 代码块执行浏览操作
// TODO: Add your control notification handler code here
CString sFolder;
LPMALLOC pMalloc;
// Gets the Shell's default allocator
if (::SHGetMalloc(&pMalloc) == NOERROR)
{
BROWSEINFO bi;
char pszBuffer[MAX_PATH];
LPITEMIDLIST pidl;
bi.hwndOwner = GetSafeHwnd();
bi.pidlRoot = NULL;
bi.pszDisplayName = pszBuffer;
bi.lpszTitle = _T("Select a directory...");
bi.ulFlags = BIF_RETURNFSANCESTORS | BIF_RETURNONLYFSDIRS;
bi.lpfn = NULL;
bi.lParam = 0;
// This next call issues the dialog box.
if ((pidl = ::SHBrowseForFolder(&bi)) != NULL)
{
if (::SHGetPathFromIDList(pidl, pszBuffer))
{
// At this point pszBuffer contains the selected path
sFolder = pszBuffer;
}
// Free the PIDL allocated by SHBrowseForFolder.
pMalloc->Free(pidl);
}
// Release the shell's allocator.
pMalloc->Release();
}
GetDlgItem(IDC_EDITROOT)->SetWindowText(sFolder);
}
BOOL CSerch::Create(LPCTSTR lpszClassName, LPCTSTR lpszWindowName, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID, CCreateContext* pContext)
{
// TODO: Add your specialized code here and/or call the base class
return CDialog::Create(IDD, pParentWnd);
}
//SR 添加一个新的dialog对话框,在windows中的虚函数中有dialog初始化的函数
BOOL CSerch::OnInitDialog()
{
CDialog::OnInitDialog();
// TODO: Add extra initialization here
//SR 列表列名初始化
m_list.InsertColumn(0,"文件名字",LVCFMT_LEFT, 150, 0);
m_list.InsertColumn(1,"所在文件夹",LVCFMT_LEFT, 200, 1);
m_list.InsertColumn(2,"大小",LVCFMT_LEFT, 60, 2);
m_list.InsertColumn(3,"修改时间",LVCFMT_LEFT, 120, 3);
// m_list.InsertColumn(0, "文件名字", LVCFMT_LEFT, 150, 0);
// m_list.InsertColumn(1, "所在文件夹", LVCFMT_LEFT, 200, 1);
// m_list.InsertColumn(2, "大小", LVCFMT_LEFT, 60, 2);
// m_list.InsertColumn(3, "修改时间", LVCFMT_LEFT, 120, 3);
CRect rect4;
m_list.GetClientRect(rect4);
m_list.SetColumnWidth(0,rect4.Width()/4);
m_list.SetColumnWidth(1,rect4.Width()/5);
m_list.SetColumnWidth(2,rect4.Width()/5);
m_list.SetColumnWidth(3,rect4.Width()*8/20);
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
void CSerch::OnSerch()
{
// TODO: Add your control notification handler code here
//SR 如果上次查询正在进行,取消上次查询
// if(m_bSearching){
// m_finder.StopSearch();
// return;
// }
// CFileFinder::CFindOpts opts;
//
// UpdateData();
// //SR 设置CFindOpts对象
// opts.sBaseFolder = m_sBaseFolder;
// opts.sFileMask.Format("*%s*",m_sFileMask);
// opts.bSubfolders =m_bSubFolders;//SR 是否查找子目录
m_newList.ResetContent();
UpdateData(TRUE);
SearchFile(m_sBaseFolder,m_sFileMask);
}
void CSerch::SearchFile(CString strDir, CString strFile)
{
CFileFind ff;
CString szDir = strDir;
BOOL bValidFile;
if(szDir.Right(1) != "\\")
szDir += "\\";
// szDir += "*.*";
szDir += "*.txt";
BOOL res = ff.FindFile(szDir,0);
CString strFileFind(L"");
while(res)
{
res = ff.FindNextFile();
strFileFind = ff.GetFileName();
// if(ff.GetFileName()==strFile)
//kai 现在是全查询,
if(strFileFind.Find(strFile)>=0||strFile==L"")
{
// if(m_Test==L""||m_Test==NULL){
if(m_Test.IsEmpty()){
//找到了,加入列表框中
m_newList.AddString(ff.GetFilePath());
}else{
bValidFile = FindTextInFile(ff.GetFilePath(),m_Test);
if(bValidFile==1){
m_newList.AddString(ff.GetFilePath());
}
}
}
if(ff.IsDirectory() && !ff.IsDots())
{
//如果是一个子目录,用递归继续往深一层找
SearchFile(ff.GetFilePath(),strFile);//加一层子目录
}
}
ff.Close();//关闭
//kai执行全查询代码
// CString strFileFind(L"");
// while(res)
// {
// res = ff.FindNextFile();
// strFileFind = ff.GetFileName();
// // if(ff.GetFileName()==strFile)
// //kai 现在是全查询,
// if(strFileFind.Find(strFile)>=0||strFile==L"")
// {
// //找到了,加入列表框中
// m_newList.AddString(ff.GetFilePath());
// }
//kai执行固定文件名查询
//if(ff.GetFileName()==strFile)
}
void CSerch::OnDBClick(UINT nFlags,CPoint point)
{
// TODO: Add your control notification handler code here
}
void CSerch::OnSelchangeList2()
{
// TODO: Add your control notification handler code here
}
void CSerch::OnClose()
{
// TODO: Add your control notification handler code here
OnOK();
}
void CSerch::OnContendSerch()
{
// TODO: Add your control notification handler code here
}
int CSerch::FindTextInFile(LPCTSTR szFile, LPCTSTR szText)
{
if((szText == NULL)||(szText =="")) return FALSE;
CFile file;
if(!file.Open(szFile,CFile::modeRead))return FALSE;
const UINT nCMinBufSize = 128;
CString sText;
CString sFindText(szText);//定义文档中的内容
UINT nFindTextLen = sFindText.GetLength();
UINT nBufSize =128;
UINT nReadSize;
UINT nCharRead;
LPSTR pTextBuf;
BOOL bTextFound(FALSE);
int nLoopCount =0;
int nflag(2);
if((2*nFindTextLen)>nCMinBufSize)
nBufSize = (2 * nFindTextLen);
//nReadSize是要读取的长度
nReadSize = nBufSize - nFindTextLen-2;
sFindText.MakeUpper();
// while((nflag<=0)&&(nCharRead == nReadSize))
do
{
pTextBuf = sText.GetBuffer(nBufSize);
if(pTextBuf[0] == 0x0)
memset(pTextBuf,' ',nFindTextLen+2);
else
memcpy(pTextBuf,pTextBuf+(nBufSize - nFindTextLen-2),nFindTextLen+2);
//kai Read:Reads data into a buffer from the file associated with the CFile object
//No.one argument is a buffer
nCharRead = file.Read(pTextBuf + nFindTextLen,nReadSize);
//判断读取的字符里有多少除汉字以外的字符
int count = InterceptString(0,CString(pTextBuf));
// if(count%2 != 0){
// // nCharRead = file.Read(pTextBuf+nFindTextLen,nReadSize-1);
// }
sText.ReleaseBuffer(nFindTextLen+nCharRead);
sText.Remove('\0');
sText.MakeUpper();
// if(sText.Find(sFindText)!=-1){
// bTextFound=1;
// }
// bTextFound = (sText.Find(sFindText)!=-1);
nflag= (sText.Find(sFindText)!=-1);
}
// while((nCharRead == nReadSize)&&!bTextFound);
while((nflag<=0)&&(nCharRead == nReadSize));
file.Close();
return (nflag);
}
int CSerch::InterceptString(int qlen, CString strSource)
{
int len,i,y,k;
CString sTemp,sreturn,ceshi;
strSource.TrimLeft();strSource.TrimRight();
len=strSource.GetLength();
y=0;
k=0;
sTemp=strSource.Right(len-qlen);
//for(i=0;i<len;i++)
for(i=0;i<len;i++)
{
if(y>=len){
break;
}
if(sTemp[y]<0 || sTemp[y]>256)
{ y=y+2;
}
else
{
y=y+1;
k++;
}
}
ceshi.Format("%d",k);
// AfxMessageBox(ceshi);
return k;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -