自定义消息:
Code
#define UM_PROGRESS WM_USER+1
BEGIN_MESSAGE_MAP(CMainDialog, CDialog)
ON_MESSAGE(UM_PROGRESS,OnProgress)
END_MESSAGE_MAP()
//消息处理函数
LRESULT CMainDialog::OnProgress(WPARAM wParam,LPARAM lParam)
{
return 0;
}
{
dlg->SendMessage(UM_PROGRESS);
}
#define UM_PROGRESS WM_USER+1
BEGIN_MESSAGE_MAP(CMainDialog, CDialog)
ON_MESSAGE(UM_PROGRESS,OnProgress)
END_MESSAGE_MAP()
//消息处理函数
LRESULT CMainDialog::OnProgress(WPARAM wParam,LPARAM lParam)
{
return 0;
}
{
dlg->SendMessage(UM_PROGRESS);
}
程序中需要用到系统托盘的功能,在 http://www.vckbase.com/document/viewdoc/?id=677 中得到了最简单的方法~水~
几个简单函数
Code
CString CMainDialog::Int2String(int val)
{
CString tmpstr;
tmpstr.Format(L"%i",val);
return tmpstr;
}
CString CmytoolDlg::String2Lower(CString strInput)
{
int iTotalSize = strInput.GetLength();
for( int tmpindex = 0 ; tmpindex < iTotalSize ; tmpindex++ )
{
strInput.SetAt( tmpindex,tolower( strInput.GetAt(tmpindex) ) );
}
return strInput;
}
//建立多級目錄
bool CmytoolDlg::CreatedMultipleDirectory( CString direct)
{
if(direct.Right(1)!="\\")
{
direct = direct + L"\\";
}
int nLen = direct.GetLength();
for(int idx = 0; idx < nLen; idx++)
{
if(direct[idx]=='\\')
{
CString tmpstr = direct.Left(idx+1);
if(!this->FolderExist(tmpstr))
{
if( !CreateDirectory(tmpstr,NULL) )
{
return false;
}
}
}
}
return true;
}
//判斷目錄是否存在 根目錄會返回false
bool CmytoolDlg::FolderExist(CString strPath)
{
if(strPath.Right(1)=="\\")
{
strPath.Delete(strPath.GetLength()-1,1);
}
/*
if(GetFileAttributes(strPath) == FILE_ATTRIBUTE_DIRECTORY)
{
return true;
}
else
{
return false;
}
*/
WIN32_FIND_DATA wfd;
bool rValue = false;
HANDLE hFind = FindFirstFile(strPath, &wfd);
if ((hFind!=INVALID_HANDLE_VALUE) &&
(wfd.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY))
{
rValue = true;
}
FindClose(hFind);
return rValue;
}
// 查找指定目錄中子文件及子目錄信息(參數: 0:目錄 1:pdf文件數 2:其他文件數 3:子目錄數)
void CmytoolDlg::findPdfFilesNum(CString dir,int &pdfFileNum,int &otherFileNum,int &dirNum)
{
this->fileInfoList.clear();
if(dir.Right(1)!="\\")
{
dir = dir + L"\\";
}
CFileFind tempFind;
pdfFileNum = 0;
otherFileNum = 0;
dirNum = 0;
BOOL IsFinded = tempFind.FindFile(dir + L"*.*");
while(IsFinded)
{
IsFinded = tempFind.FindNextFileW();
if(!tempFind.IsDots())
{
if( !tempFind.IsDirectory() )//只查文件不遞歸了
{
CString tmpfilename = tempFind.GetFileName();//tempFind.GetCreationTime(
if( this->String2Lower( tmpfilename.Right(3) ) == "pdf" )
{
CTime tmpFileCreateTime;
tempFind.GetCreationTime(tmpFileCreateTime);
CMyFileInfo tmpFileInfo;
tmpFileInfo.fileName = tmpfilename;
tmpFileInfo.fileCreateTime = tmpFileCreateTime;
//默認按字母順序排序
bool hasadd = false;
int itmptotal = this->fileInfoList.size();
CString tmplowerval = this->String2Lower(tmpfilename);
for( std::vector<CMyFileInfo>::size_type i = 0 ; i < this->fileInfoList.size() ; i++ )
{
if( this->String2Lower( this->fileInfoList[i].fileName ).Compare(tmplowerval) > 0 )
{
this->fileInfoList.insert( this->fileInfoList.begin() + i , tmpFileInfo );
hasadd = true;
break;
}
}
if(!hasadd)
{
this->fileInfoList.push_back(tmpFileInfo);
}
pdfFileNum++;
}
else
{
otherFileNum++;
}
}
else
{
dirNum++;
}
}
}
tempFind.Close();
}
CString CMainDialog::Int2String(int val)
{
CString tmpstr;
tmpstr.Format(L"%i",val);
return tmpstr;
}
CString CmytoolDlg::String2Lower(CString strInput)
{
int iTotalSize = strInput.GetLength();
for( int tmpindex = 0 ; tmpindex < iTotalSize ; tmpindex++ )
{
strInput.SetAt( tmpindex,tolower( strInput.GetAt(tmpindex) ) );
}
return strInput;
}
//建立多級目錄
bool CmytoolDlg::CreatedMultipleDirectory( CString direct)
{
if(direct.Right(1)!="\\")
{
direct = direct + L"\\";
}
int nLen = direct.GetLength();
for(int idx = 0; idx < nLen; idx++)
{
if(direct[idx]=='\\')
{
CString tmpstr = direct.Left(idx+1);
if(!this->FolderExist(tmpstr))
{
if( !CreateDirectory(tmpstr,NULL) )
{
return false;
}
}
}
}
return true;
}
//判斷目錄是否存在 根目錄會返回false
bool CmytoolDlg::FolderExist(CString strPath)
{
if(strPath.Right(1)=="\\")
{
strPath.Delete(strPath.GetLength()-1,1);
}
/*
if(GetFileAttributes(strPath) == FILE_ATTRIBUTE_DIRECTORY)
{
return true;
}
else
{
return false;
}
*/
WIN32_FIND_DATA wfd;
bool rValue = false;
HANDLE hFind = FindFirstFile(strPath, &wfd);
if ((hFind!=INVALID_HANDLE_VALUE) &&
(wfd.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY))
{
rValue = true;
}
FindClose(hFind);
return rValue;
}
// 查找指定目錄中子文件及子目錄信息(參數: 0:目錄 1:pdf文件數 2:其他文件數 3:子目錄數)
void CmytoolDlg::findPdfFilesNum(CString dir,int &pdfFileNum,int &otherFileNum,int &dirNum)
{
this->fileInfoList.clear();
if(dir.Right(1)!="\\")
{
dir = dir + L"\\";
}
CFileFind tempFind;
pdfFileNum = 0;
otherFileNum = 0;
dirNum = 0;
BOOL IsFinded = tempFind.FindFile(dir + L"*.*");
while(IsFinded)
{
IsFinded = tempFind.FindNextFileW();
if(!tempFind.IsDots())
{
if( !tempFind.IsDirectory() )//只查文件不遞歸了
{
CString tmpfilename = tempFind.GetFileName();//tempFind.GetCreationTime(
if( this->String2Lower( tmpfilename.Right(3) ) == "pdf" )
{
CTime tmpFileCreateTime;
tempFind.GetCreationTime(tmpFileCreateTime);
CMyFileInfo tmpFileInfo;
tmpFileInfo.fileName = tmpfilename;
tmpFileInfo.fileCreateTime = tmpFileCreateTime;
//默認按字母順序排序
bool hasadd = false;
int itmptotal = this->fileInfoList.size();
CString tmplowerval = this->String2Lower(tmpfilename);
for( std::vector<CMyFileInfo>::size_type i = 0 ; i < this->fileInfoList.size() ; i++ )
{
if( this->String2Lower( this->fileInfoList[i].fileName ).Compare(tmplowerval) > 0 )
{
this->fileInfoList.insert( this->fileInfoList.begin() + i , tmpFileInfo );
hasadd = true;
break;
}
}
if(!hasadd)
{
this->fileInfoList.push_back(tmpFileInfo);
}
pdfFileNum++;
}
else
{
otherFileNum++;
}
}
else
{
dirNum++;
}
}
}
tempFind.Close();
}
线程相关的东东:
private:
HANDLE copyFileThread ;
public:
static DWORD WINAPI ThreadCopyFileProc (LPVOID lpParameter);
Code
{
this->copyFileThread=CreateThread(NULL,0,ThreadCopyFileProc,this,0,NULL);
CloseHandle(this->copyFileThread);
}
DWORD WINAPI CMainDialog::ThreadCopyFileProc(LPVOID lpParameter)
{
CMainDialog *dlg = (CMainDialog*)lpParameter;
dlg->SendMessage(UM_PROGRESS); //消息通知主线程,更新进度条
}
{
this->copyFileThread=CreateThread(NULL,0,ThreadCopyFileProc,this,0,NULL);
CloseHandle(this->copyFileThread);
}
DWORD WINAPI CMainDialog::ThreadCopyFileProc(LPVOID lpParameter)
{
CMainDialog *dlg = (CMainDialog*)lpParameter;
dlg->SendMessage(UM_PROGRESS); //消息通知主线程,更新进度条
}
读写2进制文件
Code
#include "afx.h"
CStdioFile file;
file.Open(logfilepath,CFile::modeCreate|CFile::modeWrite|CFile::typeBinary);
for()
{
//写入文件
CString strLogVal;
strLogVal.Format
file.Seek(0,CFile::end);
file.WriteString(strLogVal);
}
file.Close();
CString strText = L"";
CString szLine = L"";
CStdioFile file1;
file1.Open(this->logfiledir,CFile::modeRead|CFile::typeBinary);
//逐行读取字符串
while( file1.ReadString( szLine ) )
{
strText += szLine+'\n';
}
//关闭文件
file1.Close();
#include "afx.h"
CStdioFile file;
file.Open(logfilepath,CFile::modeCreate|CFile::modeWrite|CFile::typeBinary);
for()
{
//写入文件
CString strLogVal;
strLogVal.Format
file.Seek(0,CFile::end);
file.WriteString(strLogVal);
}
file.Close();
CString strText = L"";
CString szLine = L"";
CStdioFile file1;
file1.Open(this->logfiledir,CFile::modeRead|CFile::typeBinary);
//逐行读取字符串
while( file1.ReadString( szLine ) )
{
strText += szLine+'\n';
}
//关闭文件
file1.Close();
用CSpreadSheetEx读excel貌似dao方式
Code
CSpreadSheetEx ss(strExcelFilename);
ss.OpenSheet(strExcelSheetName);
int iTotalRowNum = ss.GetRowCounts();
CStringArray rowTmpContent;
ss.GetRow(rowTmpContent,0);//頭列
if( this->checkExcelFileFormat(rowTmpContent) )
{
GetDlgItem(IDC_EDIT_BEGINPOS)->SetWindowTextW( L"" );
GetDlgItem(IDC_EDIT_ROWNUM)->SetWindowTextW( L"" );
//设置执行范围
CSetRangeDialog setRangeDia;
//setRangeDia.initAllDiaInfo();
if(setRangeDia.setExcelInfo(strExcelFilename,strExcelSheetName))
{
if( IDOK==setRangeDia.DoModal() )
{
int tmpBegIndex = setRangeDia.getBeginIndex();
int tmpRowSpan = setRangeDia.getRowSpan();
bool dataFull = true;
if(tmpBegIndex==-1)
{
GetDlgItem(IDC_EDIT_BEGINPOS)->SetWindowTextW( L"" );
dataFull = false;
}
else
{
CString tmpstr ;
tmpstr.Format(L"%i",tmpBegIndex);
GetDlgItem(IDC_EDIT_BEGINPOS)->SetWindowTextW( tmpstr );
}
if(tmpRowSpan==-1)
{
GetDlgItem(IDC_EDIT_ROWNUM)->SetWindowTextW( L"" );
dataFull = false;
}
else
{
CString tmpstr ;
tmpstr.Format(L"%i",tmpRowSpan);
GetDlgItem(IDC_EDIT_ROWNUM)->SetWindowTextW( tmpstr );
}
if(dataFull)
{
this->dataFormExcel.RemoveAll();//清除舊數據
setRangeDia.getDataForDealwith(this->dataFormExcel);
}
}
}
else
{
MessageBoxW(L"加載excel文件失敗");
}
}
else
{
this->excelFormatError();
}
CSpreadSheetEx ss(strExcelFilename);
ss.OpenSheet(strExcelSheetName);
int iTotalRowNum = ss.GetRowCounts();
CStringArray rowTmpContent;
ss.GetRow(rowTmpContent,0);//頭列
if( this->checkExcelFileFormat(rowTmpContent) )
{
GetDlgItem(IDC_EDIT_BEGINPOS)->SetWindowTextW( L"" );
GetDlgItem(IDC_EDIT_ROWNUM)->SetWindowTextW( L"" );
//设置执行范围
CSetRangeDialog setRangeDia;
//setRangeDia.initAllDiaInfo();
if(setRangeDia.setExcelInfo(strExcelFilename,strExcelSheetName))
{
if( IDOK==setRangeDia.DoModal() )
{
int tmpBegIndex = setRangeDia.getBeginIndex();
int tmpRowSpan = setRangeDia.getRowSpan();
bool dataFull = true;
if(tmpBegIndex==-1)
{
GetDlgItem(IDC_EDIT_BEGINPOS)->SetWindowTextW( L"" );
dataFull = false;
}
else
{
CString tmpstr ;
tmpstr.Format(L"%i",tmpBegIndex);
GetDlgItem(IDC_EDIT_BEGINPOS)->SetWindowTextW( tmpstr );
}
if(tmpRowSpan==-1)
{
GetDlgItem(IDC_EDIT_ROWNUM)->SetWindowTextW( L"" );
dataFull = false;
}
else
{
CString tmpstr ;
tmpstr.Format(L"%i",tmpRowSpan);
GetDlgItem(IDC_EDIT_ROWNUM)->SetWindowTextW( tmpstr );
}
if(dataFull)
{
this->dataFormExcel.RemoveAll();//清除舊數據
setRangeDia.getDataForDealwith(this->dataFormExcel);
}
}
}
else
{
MessageBoxW(L"加載excel文件失敗");
}
}
else
{
this->excelFormatError();
}
读word文件,用auto的方式
Code
CApplication app;
CDocuments docs;
CDocument0 doc;
CnlineShapes inlineShapes;
CSelection cselection;
CnlineShape inlineShape;
if( !app.CreateDispatch(L"Word.Application") )
{
dlg->inaction = false;
::OleUninitialize();/***********/
dlg->m_errMsg = L"創建WORD服務失敗";
dlg->SendMessage(UM_PROC_ERR);
return 0;
}
docs.AttachDispatch(app.get_Documents());
app.put_Visible(FALSE);
IDispatch *dispatch = docs.Open(
&_variant_t(dlg->m_wordFile),/*word file*/
&vtMissing,&_variant_t(true),/*read only &_variant_t(true) */
&vtMissing,&_variant_t(dlg->m_wordPassw),/*word password*/
&vtMissing,&vtMissing,&vtMissing,&vtMissing,&vtMissing,&vtMissing,
&vtMissing,&vtMissing,&vtMissing,&vtMissing,&vtMissing);
if(dispatch)
{
doc.AttachDispatch(dispatch);
}
else
{
app.Quit(&vtMissing,&vtMissing,&vtMissing);
docs.ReleaseDispatch();
app.ReleaseDispatch();
dlg->inaction = false;
::OleUninitialize();/***********/
dlg->m_errMsg = L"打開文件出錯,請查看文件是否存在";
dlg->SendMessage(UM_PROC_ERR);
return 0;
}
dispatch = doc.get_InlineShapes();
int picnum = 0;
dlg->m_succNum = 0;
if(dispatch)
{
inlineShapes.AttachDispatch(dispatch);
long _total = inlineShapes.get_Count();
for (int i = 1; i <= _total; i++)
{
if(!dlg->inaction)
{
break;
}
inlineShape.AttachDispatch( inlineShapes.Item(i) );
long inlineshapeType = inlineShape.get_Type();
if(inlineshapeType==3)
{
inlineShape.Select();
cselection.AttachDispatch( app.get_Selection() );
cselection.CopyAsPicture();
if( IsClipboardFormatAvailable(CF_BITMAP) )
{
if( dlg->OpenClipboard() )
{
HBITMAP hBitmap = (HBITMAP)GetClipboardData(CF_BITMAP);
if( picnum >= dlg->m_excelData.GetSize() )
{
CloseClipboard();
break;
}
CString tmpval = dlg->m_excelData.GetAt(picnum)+L"1";
CString dir = dlg->m_tmpdir + tmpval + ".jpg";
CString dir2 = dlg->m_pdfOutDir + tmpval + ".pdf";
if(dlg->SaveBmp(hBitmap,dir))
{
dlg->jpeg2pdf(dir,dir2);
dlg->m_succNum++;
dlg->SendMessage(UM_PROC_SHOWSTATE);
}
CloseClipboard();
try
{
DeleteFile(dir);
}
catch(){}
}
}
picnum++;
}
}
}
app.Quit(&vtMissing,&vtMissing,&vtMissing);
doc.ReleaseDispatch();
docs.ReleaseDispatch();
app.ReleaseDispatch();
CApplication app;
CDocuments docs;
CDocument0 doc;
CnlineShapes inlineShapes;
CSelection cselection;
CnlineShape inlineShape;
if( !app.CreateDispatch(L"Word.Application") )
{
dlg->inaction = false;
::OleUninitialize();/***********/
dlg->m_errMsg = L"創建WORD服務失敗";
dlg->SendMessage(UM_PROC_ERR);
return 0;
}
docs.AttachDispatch(app.get_Documents());
app.put_Visible(FALSE);
IDispatch *dispatch = docs.Open(
&_variant_t(dlg->m_wordFile),/*word file*/
&vtMissing,&_variant_t(true),/*read only &_variant_t(true) */
&vtMissing,&_variant_t(dlg->m_wordPassw),/*word password*/
&vtMissing,&vtMissing,&vtMissing,&vtMissing,&vtMissing,&vtMissing,
&vtMissing,&vtMissing,&vtMissing,&vtMissing,&vtMissing);
if(dispatch)
{
doc.AttachDispatch(dispatch);
}
else
{
app.Quit(&vtMissing,&vtMissing,&vtMissing);
docs.ReleaseDispatch();
app.ReleaseDispatch();
dlg->inaction = false;
::OleUninitialize();/***********/
dlg->m_errMsg = L"打開文件出錯,請查看文件是否存在";
dlg->SendMessage(UM_PROC_ERR);
return 0;
}
dispatch = doc.get_InlineShapes();
int picnum = 0;
dlg->m_succNum = 0;
if(dispatch)
{
inlineShapes.AttachDispatch(dispatch);
long _total = inlineShapes.get_Count();
for (int i = 1; i <= _total; i++)
{
if(!dlg->inaction)
{
break;
}
inlineShape.AttachDispatch( inlineShapes.Item(i) );
long inlineshapeType = inlineShape.get_Type();
if(inlineshapeType==3)
{
inlineShape.Select();
cselection.AttachDispatch( app.get_Selection() );
cselection.CopyAsPicture();
if( IsClipboardFormatAvailable(CF_BITMAP) )
{
if( dlg->OpenClipboard() )
{
HBITMAP hBitmap = (HBITMAP)GetClipboardData(CF_BITMAP);
if( picnum >= dlg->m_excelData.GetSize() )
{
CloseClipboard();
break;
}
CString tmpval = dlg->m_excelData.GetAt(picnum)+L"1";
CString dir = dlg->m_tmpdir + tmpval + ".jpg";
CString dir2 = dlg->m_pdfOutDir + tmpval + ".pdf";
if(dlg->SaveBmp(hBitmap,dir))
{
dlg->jpeg2pdf(dir,dir2);
dlg->m_succNum++;
dlg->SendMessage(UM_PROC_SHOWSTATE);
}
CloseClipboard();
try
{
DeleteFile(dir);
}
catch(){}
}
}
picnum++;
}
}
}
app.Quit(&vtMissing,&vtMissing,&vtMissing);
doc.ReleaseDispatch();
docs.ReleaseDispatch();
app.ReleaseDispatch();
一个数字字符串加分号的函数:
CString strTest = L"321123456789";
CString newStr = L"";
int iLength = strTest.GetLength();
int curIndex = 0;
for(int i=iLength-1;i>=0;i--,curIndex++)
{
if( curIndex%3 == 0 && curIndex!=0 )
{
newStr = L"," + newStr;
}
newStr = strTest.GetAt(i) + newStr;
}
CString newStr = L"";
int iLength = strTest.GetLength();
int curIndex = 0;
for(int i=iLength-1;i>=0;i--,curIndex++)
{
if( curIndex%3 == 0 && curIndex!=0 )
{
newStr = L"," + newStr;
}
newStr = strTest.GetAt(i) + newStr;
}
AfxMessageBox(newStr);