1,Win32 Console程序示例:
#include<windows.h>
#include<stdio.h>
#include<string.h>
#include<conio.h>

constintFILEMAX=300;//allowmax.300filesineachdirectory

typedefstruct_DESTFILE


{//目标文件
WIN32_FIND_DATAfd;
BOOLbMatch;
}DESTFILE;

typedefstruct_SRCFILE


{//源文件
WIN32_FIND_DATAfd;
BOOLbIsNew;
}SRCFILE;

intmain(intargc,char*argv[])


{
inti,j,iSrcFiles,iDestFiles;
HANDLEhFile;
WIN32_FIND_DATAfd;
BOOLbRet=TRUE;
charsrc[MAX_PATH+1],dest[MAX_PATH+1],destpath[MAX_PATH+1];
SRCFILEsrcFiles[FILEMAX];
DESTFILEdestFiles[FILEMAX];
BOOLbFound=FALSE;
DWORDdwcNameSize=MAX_PATH+1;
charszBuffer[MAX_PATH+1];

if(argc<2)


{
return-1;
}
for(i=0;i<argc;i++)
printf("argv[%d]:%s/n",i,argv[i]);

strcpy(src,argv[1]);
if(argc==2)


{//未指定目标文件夹
GetCurrentDirectory(dwcNameSize,(LPSTR)&szBuffer);
strcpy(dest,szBuffer);
dest[0]='I';//默认目标盘
strcpy(destpath,dest);//destpathshouldbesomethinglike"k:/u002/doc/".
strcat(destpath,"//");//justprepareforuselatter(whenupdatinganddeleting).
strcat(dest,"//*.*");//destshouldbesomethinglike"k:/u002/doc/*.*"
}
else


{//指定目标文件夹
strcpy(destpath,argv[2]);//destpathshouldbesomethinglike"k:"
//justprepareforusagelatter(whenupdatinganddeleting).
strcpy(dest,argv[2]);
strcat(dest,"*.*");//thendestshouldbesomethinglike"k:*.*"
}
strcat(src,"*.*");//srcshouldbesomethinglikeg:*.*
//preparesrcFiles[]
bRet=TRUE;
iSrcFiles=0;
//printf("Directorylistingof%s/n",src);
hFile=FindFirstFile(src,&fd);
while(hFile!=INVALID_HANDLE_VALUE&&bRet)


{

if(fd.dwFileAttributes==FILE_ATTRIBUTE_ARCHIVE)
{
srcFiles[iSrcFiles].fd=fd;
srcFiles[iSrcFiles].bIsNew=FALSE;
//printf("%s/n",srcFiles[iSrcFiles].fd.cFileName);
iSrcFiles++;
}
bRet=FindNextFile(hFile,&fd);
}
//preparedestFiles[]
bRet=TRUE;
iDestFiles=0;
//printf("Directorylistingof%s/n",dest);
hFile=FindFirstFile(dest,&fd);
while(hFile!=INVALID_HANDLE_VALUE&&bRet)


{

if(fd.dwFileAttributes==FILE_ATTRIBUTE_ARCHIVE)
{
destFiles[iDestFiles].fd=fd;
destFiles[iDestFiles].bMatch=FALSE;
//printf("%s/n",destFiles[iDestFiles].fd.cFileName);
iDestFiles++;
}
bRet=FindNextFile(hFile,&fd);
}
//checkfornewfilesandredudantfiles

for(i=0;i<iSrcFiles;i++)
{
bFound=FALSE;

for(j=0;j<iDestFiles;j++)
{

if(!(destFiles[j].bMatch))
{
if(strcmpi(destFiles[j].fd.cFileName,srcFiles[i].fd.cFileName)==0)


{//找到匹配
//findsamefilesindestdirectory
destFiles[j].bMatch=TRUE;
bFound=TRUE;
if(CompareFileTime(&destFiles[j].fd.ftLastWriteTime,
&srcFiles[i].fd.ftLastWriteTime)<0)


{//比较文件更新时间
//srcfileisnewthandestfile
srcFiles[i].bIsNew=TRUE;
}
break;//breakjloop,becausefound!
}
}
}
if(bFound==FALSE)//notfound,soisnew.
srcFiles[i].bIsNew=TRUE;
}
//updatingnewfiles

for(i=0,j=0;i<iSrcFiles;i++)
{//jfornewfilescounter

if(srcFiles[i].bIsNew)
{
printf("%s/n",srcFiles[i].fd.cFileName);
j++;
}
}
if(j==0)


{
printf("nofilenew/n");
}
else


{
printf("Thereare%dfilesneedtobeupdated/n",j);
printf("ifyoudonotwanttoupdatethesefiles,pressCtrl-Break/n");
printf("otherwiseanykey
/n");
getch();
}
for(i=0;i<iSrcFiles;i++)


{
if(srcFiles[i].bIsNew)


{
strcpy(dest,destpath);
strcat(dest,srcFiles[i].fd.cFileName);
CopyFile(srcFiles[i].fd.cFileName,dest,FALSE);//FALSEmeansoverwrite
}
}
//deletingredudantfiles
for(j=0,i=0;j<iDestFiles;j++)


{//iforredudantfilescounter
if(!destFiles[j].bMatch)


{
printf("%s/n",destFiles[j].fd.cFileName);
i++;
}
}
if(i==0)


{
printf("noredudantfile/n");
}
else


{
printf("Thereare%dfilesneedtobedeleted/n",i);
printf("ifyoudonotwanttodeletethosefiles,pressCtrl-Break/n");
printf("otherwiseanykey
/n");
getch();
}
for(j=0;j<iDestFiles;j++)


{
if(!destFiles[j].bMatch)


{
strcpy(dest,destpath);
strcat(dest,destFiles[j].fd.cFileName);
DeleteFile(dest);
}
}
return0;
}

_
<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />
2,String型转换为LPWSTR
在TextOut中想直接输出一个string型不行,它参数非得是一个LPWSTR,解决的办法是用MultiByteToWideChar,MultiByteToWideChar 函数可以把普通字符串转换为一个宽字符字符串(Unicode)
LONGOnPaint(HWNDhWnd,UINTnMessage,WPARAMwParam,LPARAMlParam)


{
PAINTSTRUCTps;
HDChdc;
hdc=BeginPaint(hWnd,&ps);
WCHARwszMsgInfo[256];
stringstrInfo="aa";
MultiByteToWideChar(CP_ACP,0,strInfo.c_str(),strInfo.length(),wszMsgInfo,sizeof(wszMsgInfo)/sizeof(wszMsgInfo[0]));
::TextOut(hdc,10,10,wszMsgInfo,strInfo.length());
EndPaint(hWnd,&ps);
return0;
}

3,对上一节的代码进行修改,加入一个新的功能,使得能从菜单呼出系统中的记事本
首先修改命令映射表
MSGMAP_ENTRY_commandEntries[]=


{
IDM_ABOUT,OnAbout,
IDM_EXIT,OnExit,
IDM_NOTEBOOK,OnNoteBook
};
再添加一个处理函数,在函数中创建一个记事本进程:
LONGOnNoteBook(HWNDhWnd,UINTnMessage,WPARAMwParam,LPARAMlParam)


{
PROCESS_INFORMATIONpInfo;
STARTUPINFOsi;
ZeroMemory(&si,sizeof(si));
si.cb=sizeof(si);
ZeroMemory(&pInfo,sizeof(pInfo));
BOOL 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);
}
return0;
}
4,新开一个线程获取系统时间
caseIDM_GETSYSTIME:


{
DWORDdwThrId;
hSysTime=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)ReadCurTime,NULL,0,&dwThrId);
if(hSysTime!=NULL)


{
WaitForSingleObject(hSysTime,INFINITE);
CloseHandle(hSysTime);
}
}
break;

voidReadCurTime(void)


{//当前系统时间
SYSTEMTIMEst;
GetSystemTime(&st);
charbuffer[256];
sprintf(buffer,"%d:%d:%d",st.wYear,st.wMonth,st.wDay);
}