浏览选择文件夹,读取文件夹的文件列举到Listbox,读取文本文档内容,处理完文档内容,保存到新的文本文档,文本文档格式是ANSI.
void CFurtherConvertCutCodeView::OnBnClickedButtonBrowser()
{
CString filePath;
m_ProgressFile.SetPos(0);
m_ListFiles.ResetContent();
TCHAR szPath[_MAX_PATH];
BROWSEINFO bi;
bi.hwndOwner=NULL;
bi.pidlRoot=NULL;
bi.lpszTitle=_T("文件浏览");
bi.pszDisplayName=szPath;
bi.ulFlags=BIF_RETURNONLYFSDIRS;
bi.lParam=NULL;
bi.lpfn=NULL;
LPITEMIDLIST pItemIDList=SHBrowseForFolder(&bi);
if(pItemIDList)
{
if(SHGetPathFromIDList(pItemIDList,szPath))
filePath=szPath;
CFileFind finder;
CString strFilenName;
BOOL bWorking=finder.FindFile(filePath+"\\*.din");
while(bWorking)
{
bWorking = finder.FindNextFile();
strFilenName=(LPCTSTR)finder.GetFileName(); //str为取得一个文件名
m_ListFiles.AddString(strFilenName);
}
}
else
{
filePath="";
}
GetDlgItem(IDC_STATIC_TOTAL)->SetWindowText(_T("0"));
GetDlgItem(IDC_STATIC_RUNNING)->SetWindowText(_T("0"));
GetDlgItem(IDC_STATIC_Browse)->SetWindowText(filePath);
}
void CFurtherConvertCutCodeView::OnBnClickedButtonRun()
{
// TODO: Add your control notification handler code here
CString strTotal;
CString strRunning;
int k=1;
CString strFilePath;
//添加光顺后程序保存的目录
CString strPath;
GetDlgItem(IDC_STATIC_Browse)->GetWindowText(strFilePath);
CString strFileName;
//找出勾选的项目
int iSel=m_ListFiles.GetCount();
if(iSel>0)
{
strPath=strFilePath+_T("\\_GCG261");
CreateDirectory(strPath,NULL);
}
m_ProgressFile.SetPos(0);
int j=0;
for(int i=0;i<iSel;i++)
{
if(m_ListFiles.GetCheck(i)==1)
{
j++;
}
}
strTotal.Format(_T("%d"),j);
GetDlgItem(IDC_STATIC_TOTAL)->SetWindowText(strTotal);
m_ProgressFile.SetRange(0,j);
m_ProgressFile.SetStep(1);
for(int i=0;i<iSel;i++)
{
if(m_ListFiles.GetCheck(i)==1)
{
m_ListFiles.GetText(i,strFileName);
ReadCodeFile(strFilePath+"\\"+strFileName,strPath+"\\"+strFileName);
m_ProgressFile.StepIt();
strRunning.Format(_T("%d"),k++);
GetDlgItem(IDC_STATIC_RUNNING)->SetWindowText(strRunning);
}
}
}
void CFurtherConvertCutCodeView::ReadCodeFile(CString strFileName,CString strWriteFileName)
{
CStringList listLines;
string strT;
CString strLine;
CString strPartLine;
CStdioFile writeFile;
bool isG01=false;
bool isAddG261=false;
bool isBreak=false;
int i;
CStdioFile myFile;
myFile.Open(strFileName,CFile::modeRead);
/*CFile mFile;
mFile.Open(strWriteFileName,CFile::modeCreate|CFile::modeWrite);*/
CStdioFile *rFile=new CStdioFile(strWriteFileName,CFile::modeCreate|CFile::modeWrite);
//逐行读取
while(myFile.ReadString(strLine))
{
/*CArchive ar(&mFile,CArchive::store); */
if(strLine=="M07")
{
i=0;
while(isBreak!=true)
{
myFile.ReadString(strLine);
i=i+1;
if(strLine.Find(_T("G01"))!=-1&&strLine.Find(_T("X"))!=-1&&strLine.Find(_T("Y"))!=-1)
{
isG01=true;
i=1;
}
if(isG01==true && i==2)
{
if(strLine.Find(_T("G02"))>-1||strLine.Find(_T("G03"))>-1)
{
isAddG261=true;
isBreak=true;
}
}
if(strLine.Find(_T("M08"))!=-1)
{
isBreak=true;
}
listLines.AddTail(strLine);
}//end while
isBreak=false;
if(isAddG261==true)
{
strLine="G261\nM07";
}
else
{
strLine="M07";
}
}//end if M07
if(strLine.Find(_T("M08"))!=-1&&isAddG261==true)
{
strLine="M08\nG260";
isAddG261=false;
}
if(strLine.Find(_T("#TRAFO ON"))!=-1)
{
strLine="#TRAFO ON\n#CONTOUR MODE [ DEV PATH_DEV=0.2 RELEVANT_PATH=0.1 ]";
}
rFile->WriteString(strLine.GetBuffer());
rFile->WriteString(_T("\n"));
POSITION rPos;
rPos=listLines.GetHeadPosition();
while(rPos!=NULL)
{
strPartLine=listLines.GetNext(rPos);
rFile->WriteString(strPartLine.GetBuffer());
rFile->WriteString(_T("\n"));
}
listLines.RemoveAll();
}//end while
rFile->Close();
delete rFile;
}
void CFurtherConvertCutCodeView::WritCodeFile(CString strLine)
{
///* CString
//CStdioFile myFile;
}
void CFurtherConvertCutCodeView::OnClickedCheckSelectall()
{
// TODO: Add your control notification handler code here
if(BST_CHECKED==!IsDlgButtonChecked(IDC_CHECK_SelectAll))
{
SelectCheckList(0);
}
else
{
SelectCheckList(1);
}
}
void CFurtherConvertCutCodeView::SelectCheckList(int iCheck)
{
int iSel=m_ListFiles.GetCount();
for(int i=0;i<iSel;i++)
{
m_ListFiles.SetCheck(i,iCheck);
}
}