《深入浅出MFC》笔记(三)

本文介绍了一个Win32控制台程序示例,用于实现文件夹间文件的同步,包括新文件的检测与复制、冗余文件的删除等功能,并展示了如何在Windows应用程序中使用文本输出和调用系统记事本。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1,Win32 Console程序示例:

None.gif#include<windows.h>
None.gif#include
<stdio.h>
None.gif#include
<string.h>
None.gif#include
<conio.h>
None.gif
None.gif
constintFILEMAX=300;//allowmax.300filesineachdirectory
None.gif

None.giftypedef
struct_DESTFILE
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{//目标文件
InBlock.gif
WIN32_FIND_DATAfd;
InBlock.gifBOOLbMatch;
ExpandedBlockEnd.gif}
DESTFILE;
None.gif
None.giftypedef
struct_SRCFILE
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{//源文件
InBlock.gif
WIN32_FIND_DATAfd;
InBlock.gifBOOLbIsNew;
ExpandedBlockEnd.gif}
SRCFILE;
None.gif
None.gif
intmain(intargc,char*argv[])
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif
inti,j,iSrcFiles,iDestFiles;
InBlock.gifHANDLEhFile;
InBlock.gifWIN32_FIND_DATAfd;
InBlock.gifBOOLbRet
=TRUE;
InBlock.gif
charsrc[MAX_PATH+1],dest[MAX_PATH+1],destpath[MAX_PATH+1];
InBlock.gifSRCFILEsrcFiles[FILEMAX];
InBlock.gifDESTFILEdestFiles[FILEMAX];
InBlock.gifBOOLbFound
=FALSE;
InBlock.gifDWORDdwcNameSize
=MAX_PATH+1;
InBlock.gif
charszBuffer[MAX_PATH+1];
InBlock.gif
InBlock.gif
if(argc<2)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
return-1;
ExpandedSubBlockEnd.gif}

InBlock.gif
for(i=0;i<argc;i++)
InBlock.gifprintf(
"argv[%d]:%s/n",i,argv[i]);
InBlock.gif
InBlock.gifstrcpy(src,argv[
1]);
InBlock.gif
if(argc==2)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{//未指定目标文件夹
InBlock.gif
GetCurrentDirectory(dwcNameSize,(LPSTR)&szBuffer);
InBlock.gifstrcpy(dest,szBuffer);
InBlock.gifdest[
0]='I';//默认目标盘
InBlock.gif
strcpy(destpath,dest);//destpathshouldbesomethinglike"k:/u002/doc/".
InBlock.gif
strcat(destpath,"//");//justprepareforuselatter(whenupdatinganddeleting).
InBlock.gif
strcat(dest,"//*.*");//destshouldbesomethinglike"k:/u002/doc/*.*"
ExpandedSubBlockEnd.gif
}

InBlock.gif
else
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{//指定目标文件夹
InBlock.gif
strcpy(destpath,argv[2]);//destpathshouldbesomethinglike"k:"
InBlock.gif
//justprepareforusagelatter(whenupdatinganddeleting).
InBlock.gif
strcpy(dest,argv[2]);
InBlock.gifstrcat(dest,
"*.*");//thendestshouldbesomethinglike"k:*.*"
ExpandedSubBlockEnd.gif
}

InBlock.gifstrcat(src,
"*.*");//srcshouldbesomethinglikeg:*.*
InBlock.gif
//preparesrcFiles[]dot.gif
InBlock.gif
bRet=TRUE;
InBlock.gifiSrcFiles
=0;
InBlock.gif
//printf("Directorylistingof%s/n",src);
InBlock.gif
hFile=FindFirstFile(src,&fd);
InBlock.gif
while(hFile!=INVALID_HANDLE_VALUE&&bRet)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif
if(fd.dwFileAttributes==FILE_ATTRIBUTE_ARCHIVE)dot.gif{
InBlock.gifsrcFiles[iSrcFiles].fd
=fd;
InBlock.gifsrcFiles[iSrcFiles].bIsNew
=FALSE;
InBlock.gif
//printf("%s/n",srcFiles[iSrcFiles].fd.cFileName);
InBlock.gif
iSrcFiles++;
ExpandedSubBlockEnd.gif}

InBlock.gifbRet
=FindNextFile(hFile,&fd);
ExpandedSubBlockEnd.gif}

InBlock.gif
//preparedestFiles[]dot.gif
InBlock.gif
bRet=TRUE;
InBlock.gifiDestFiles
=0;
InBlock.gif
//printf("Directorylistingof%s/n",dest);
InBlock.gif
hFile=FindFirstFile(dest,&fd);
InBlock.gif
while(hFile!=INVALID_HANDLE_VALUE&&bRet)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif
if(fd.dwFileAttributes==FILE_ATTRIBUTE_ARCHIVE)dot.gif{
InBlock.gifdestFiles[iDestFiles].fd
=fd;
InBlock.gifdestFiles[iDestFiles].bMatch
=FALSE;
InBlock.gif
//printf("%s/n",destFiles[iDestFiles].fd.cFileName);
InBlock.gif
iDestFiles++;
ExpandedSubBlockEnd.gif}

InBlock.gifbRet
=FindNextFile(hFile,&fd);
ExpandedSubBlockEnd.gif}

InBlock.gif
//checkfornewfilesandredudantfilesdot.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
for(i=0;i<iSrcFiles;i++)dot.gif{
InBlock.gifbFound
=FALSE;
ExpandedSubBlockStart.gifContractedSubBlock.gif
for(j=0;j<iDestFiles;j++)dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif
if(!(destFiles[j].bMatch))dot.gif{
InBlock.gif
if(strcmpi(destFiles[j].fd.cFileName,srcFiles[i].fd.cFileName)==0)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{//找到匹配
InBlock.gif
//findsamefilesindestdirectory
InBlock.gif
destFiles[j].bMatch=TRUE;
InBlock.gifbFound
=TRUE;
InBlock.gif
if(CompareFileTime(&destFiles[j].fd.ftLastWriteTime,
InBlock.gif
&srcFiles[i].fd.ftLastWriteTime)<0)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{//比较文件更新时间
InBlock.gif
//srcfileisnewthandestfile
InBlock.gif
srcFiles[i].bIsNew=TRUE;
ExpandedSubBlockEnd.gif}

InBlock.gif
break;//breakjloop,becausefound!
ExpandedSubBlockEnd.gif
}

ExpandedSubBlockEnd.gif}

ExpandedSubBlockEnd.gif}

InBlock.gif
if(bFound==FALSE)//notfound,soisnew.
InBlock.gif
srcFiles[i].bIsNew=TRUE;
ExpandedSubBlockEnd.gif}

InBlock.gif
//updatingnewfilesdot.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
for(i=0,j=0;i<iSrcFiles;i++)dot.gif{//jfornewfilescounter
ExpandedSubBlockStart.gifContractedSubBlock.gif
if(srcFiles[i].bIsNew)dot.gif{
InBlock.gifprintf(
"%s/n",srcFiles[i].fd.cFileName);
InBlock.gifj
++;
ExpandedSubBlockEnd.gif}

ExpandedSubBlockEnd.gif}

InBlock.gif
if(j==0)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifprintf(
"nofilenew/n");
ExpandedSubBlockEnd.gif}

InBlock.gif
else
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifprintf(
"Thereare%dfilesneedtobeupdated/n",j);
InBlock.gifprintf(
"ifyoudonotwanttoupdatethesefiles,pressCtrl-Break/n");
InBlock.gifprintf(
"otherwiseanykeydot.gif/n");
InBlock.gifgetch();
ExpandedSubBlockEnd.gif}

InBlock.gif
for(i=0;i<iSrcFiles;i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
if(srcFiles[i].bIsNew)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifstrcpy(dest,destpath);
InBlock.gifstrcat(dest,srcFiles[i].fd.cFileName);
InBlock.gifCopyFile(srcFiles[i].fd.cFileName,dest,FALSE);
//FALSEmeansoverwrite
ExpandedSubBlockEnd.gif
}

ExpandedSubBlockEnd.gif}

InBlock.gif
//deletingredudantfilesdot.gif
InBlock.gif
for(j=0,i=0;j<iDestFiles;j++)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{//iforredudantfilescounter
InBlock.gif
if(!destFiles[j].bMatch)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifprintf(
"%s/n",destFiles[j].fd.cFileName);
InBlock.gifi
++;
ExpandedSubBlockEnd.gif}

ExpandedSubBlockEnd.gif}

InBlock.gif
if(i==0)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifprintf(
"noredudantfile/n");
ExpandedSubBlockEnd.gif}

InBlock.gif
else
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifprintf(
"Thereare%dfilesneedtobedeleted/n",i);
InBlock.gifprintf(
"ifyoudonotwanttodeletethosefiles,pressCtrl-Break/n");
InBlock.gifprintf(
"otherwiseanykeydot.gif/n");
InBlock.gifgetch();
ExpandedSubBlockEnd.gif}

InBlock.gif
for(j=0;j<iDestFiles;j++)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
if(!destFiles[j].bMatch)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifstrcpy(dest,destpath);
InBlock.gifstrcat(dest,destFiles[j].fd.cFileName);
InBlock.gifDeleteFile(dest);
ExpandedSubBlockEnd.gif}

ExpandedSubBlockEnd.gif}

InBlock.gif
return0;
ExpandedBlockEnd.gif}

None.gif
None.gif_
None.gif
<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

2String型转换为LPWSTR

TextOut中想直接输出一个string型不行,它参数非得是一个LPWSTR,解决的办法是用MultiByteToWideCharMultiByteToWideChar 函数可以把普通字符串转换为一个宽字符字符串(Unicode

None.gifLONGOnPaint(HWNDhWnd,UINTnMessage,WPARAMwParam,LPARAMlParam)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gifPAINTSTRUCTps;
InBlock.gifHDChdc;
InBlock.gifhdc
=BeginPaint(hWnd,&ps);
InBlock.gifWCHARwszMsgInfo[
256];
InBlock.gif
stringstrInfo="aa";
InBlock.gifMultiByteToWideChar(CP_ACP,
0,strInfo.c_str(),strInfo.length(),wszMsgInfo,sizeof(wszMsgInfo)/sizeof(wszMsgInfo[0]));
InBlock.gif::TextOut(hdc,
10,10,wszMsgInfo,strInfo.length());
InBlock.gifEndPaint(hWnd,
&ps);
InBlock.gif
return0;
ExpandedBlockEnd.gif}

None.gif
None.gif

3,对上一节的代码进行修改,加入一个新的功能,使得能从菜单呼出系统中的记事本

首先修改命令映射表

None.gifMSGMAP_ENTRY_commandEntries[]=
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gifIDM_ABOUT,OnAbout,
InBlock.gifIDM_EXIT,OnExit,
InBlock.gifIDM_NOTEBOOK,OnNoteBook
ExpandedBlockEnd.gif}
;
None.gif

再添加一个处理函数,在函数中创建一个记事本进程:

None.gifLONGOnNoteBook(HWNDhWnd,UINTnMessage,WPARAMwParam,LPARAMlParam)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gifPROCESS_INFORMATIONpInfo;
InBlock.gifSTARTUPINFOsi;
InBlock.gifZeroMemory(
&si,sizeof(si));
InBlock.gifsi.cb
=sizeof(si);
InBlock.gifZeroMemory(
&pInfo,sizeof(pInfo));
InBlock.gifBOOL bCreated = FALSE;
bCreated = ::CreateProcess(_T("C://WINDOWS//system32//notepad.exe"),NULL,NULL,NULL,false,0,NULL,NULL,&si,&pInfo);
if(bCreated)
{//割断父子进程间的联系
CloseHandle(pInfo.hProcess);
CloseHandle(pInfo.hThread);
}
InBlock.gifreturn0;
ExpandedBlockEnd.gif}

None.gif

4,新开一个线程获取系统时间

None.gifcaseIDM_GETSYSTIME:
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gifDWORDdwThrId;
InBlock.gifhSysTime
=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)ReadCurTime,NULL,0,&dwThrId);
InBlock.gif
if(hSysTime!=NULL)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifWaitForSingleObject(hSysTime,INFINITE);
InBlock.gifCloseHandle(hSysTime);
ExpandedSubBlockEnd.gif}

ExpandedBlockEnd.gif}

None.gif
break;
None.gif
None.gif
voidReadCurTime(void)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{//当前系统时间
InBlock.gif
SYSTEMTIMEst;
InBlock.gifGetSystemTime(
&st);
InBlock.gif
charbuffer[256];
InBlock.gifsprintf(buffer,
"%d:%d:%d",st.wYear,st.wMonth,st.wDay);
ExpandedBlockEnd.gif}

None.gif

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值