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

本文提供了一个Win32控制台程序示例,演示了如何通过比较两个目录中的文件来同步文件,并展示了字符串类型转换及调用外部程序的方法。

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
const int FILEMAX  = 300;  // allow max. 300 files in each directory
None.gif

None.giftypedef 
struct _DESTFILE
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{//目标文件
InBlock.gif
    WIN32_FIND_DATA fd;
InBlock.gif    BOOL bMatch;
ExpandedBlockEnd.gif}
 DESTFILE;
None.gif
None.giftypedef 
struct _SRCFILE
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{//源文件
InBlock.gif
    WIN32_FIND_DATA fd;
InBlock.gif    BOOL bIsNew;
ExpandedBlockEnd.gif}
 SRCFILE;
None.gif
None.gif
int main(int argc, char *argv[])
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif    
int i, j, iSrcFiles, iDestFiles;
InBlock.gif    HANDLE hFile;
InBlock.gif    WIN32_FIND_DATA fd;
InBlock.gif    BOOL bRet 
= TRUE;
InBlock.gif    
char src[MAX_PATH+1], dest[MAX_PATH+1], destpath[MAX_PATH+1];
InBlock.gif    SRCFILE  srcFiles[FILEMAX];
InBlock.gif    DESTFILE destFiles[FILEMAX];
InBlock.gif    BOOL bFound 
= FALSE;
InBlock.gif    DWORD dwcNameSize 
= MAX_PATH+1;
InBlock.gif    
char szBuffer[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.gif         printf(
"argv[%d]: %s \n", i, argv[i]);
InBlock.gif
InBlock.gif    strcpy(src, argv[
1]);
InBlock.gif    
if (argc == 2
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{//未指定目标文件夹
InBlock.gif
        GetCurrentDirectory(dwcNameSize,(LPSTR) &szBuffer);
InBlock.gif        strcpy(dest, szBuffer);
InBlock.gif        dest[
0= 'I';//默认目标盘
InBlock.gif
        strcpy(destpath, dest); // destpath should be something like "k:\u002\doc\".
InBlock.gif
        strcat(destpath, "\\"); // just prepare for use latter (when updating and deleting).
InBlock.gif
        strcat(dest, "\\*.*");  // dest should be something like "k:\u002\doc\*.*"
ExpandedSubBlockEnd.gif
    }

InBlock.gif    
else 
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{//指定目标文件夹
InBlock.gif
        strcpy(destpath, argv[2]); // destpath should be something like "k:"
InBlock.gif        
// just prepare for usage latter (when updating and deleting).
InBlock.gif
        strcpy(dest, argv[2]);
InBlock.gif        strcat(dest, 
"*.*");       // then dest should be something like "k:*.*"
ExpandedSubBlockEnd.gif
    }

InBlock.gif    strcat(src, 
"*.*");         // src should be something like g:*.*
InBlock.gif    
// prepare srcFiles[]dot.gif
InBlock.gif
    bRet = TRUE;
InBlock.gif    iSrcFiles 
= 0;
InBlock.gif    
// printf("Directory listing of %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.gif            srcFiles[iSrcFiles].fd 
= fd;
InBlock.gif            srcFiles[iSrcFiles].bIsNew 
= FALSE;
InBlock.gif            
// printf("%s\n", srcFiles[iSrcFiles].fd.cFileName);
InBlock.gif
            iSrcFiles++;
ExpandedSubBlockEnd.gif        }

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

InBlock.gif    
// prepare destFiles[]dot.gif
InBlock.gif
    bRet = TRUE;
InBlock.gif    iDestFiles 
= 0;
InBlock.gif    
// printf("Directory listing of %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.gif            destFiles[iDestFiles].fd 
= fd;
InBlock.gif            destFiles[iDestFiles].bMatch 
= FALSE;
InBlock.gif            
// printf("%s\n", destFiles[iDestFiles].fd.cFileName);
InBlock.gif
            iDestFiles++;
ExpandedSubBlockEnd.gif        }

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

InBlock.gif    
// check for new files and redudant filesdot.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
    for (i=0; i<iSrcFiles; i++dot.gif{
InBlock.gif        bFound 
= 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                    
// find same files in dest directory
InBlock.gif
                    destFiles[j].bMatch = TRUE;
InBlock.gif                    bFound 
= TRUE;
InBlock.gif                    
if (CompareFileTime(&destFiles[j].fd.ftLastWriteTime,
InBlock.gif                        
&srcFiles[i].fd.ftLastWriteTime) < 0
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{//比较文件更新时间
InBlock.gif                            
// src file is new than dest file
InBlock.gif
                            srcFiles[i].bIsNew = TRUE;
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    
break;  // break j loop, because found!
ExpandedSubBlockEnd.gif
                }

ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif        
if (bFound == FALSE) // not found, so is new.
InBlock.gif
            srcFiles[i].bIsNew = TRUE;
ExpandedSubBlockEnd.gif    }

InBlock.gif    
// updating new files dot.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
    for (i=0, j=0; i<iSrcFiles; i++dot.gif{  // j for new files counter
ExpandedSubBlockStart.gifContractedSubBlock.gif
        if (srcFiles[i].bIsNew) dot.gif{
InBlock.gif            printf(
"%s\n", srcFiles[i].fd.cFileName);
InBlock.gif            j
++;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif    
if (j==0
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        printf(
"no file new \n");
ExpandedSubBlockEnd.gif    }

InBlock.gif    
else 
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        printf(
"There are %d files need to be updated \n", j);
InBlock.gif        printf(
"if you do not want to update these files, press Ctrl-Break \n");
InBlock.gif        printf(
"otherwise anykeydot.gif\n");
InBlock.gif        getch();
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.gif            strcpy(dest, destpath);
InBlock.gif            strcat(dest, srcFiles[i].fd.cFileName);
InBlock.gif            CopyFile(srcFiles[i].fd.cFileName, dest, FALSE); 
// FALSE means overwrite
ExpandedSubBlockEnd.gif
        }

ExpandedSubBlockEnd.gif    }

InBlock.gif    
// deleting redudant filesdot.gif
InBlock.gif
    for (j=0, i=0; j<iDestFiles; j++
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{  // i for redudant files counter
InBlock.gif
        if (!destFiles[j].bMatch) 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            printf(
"%s\n", destFiles[j].fd.cFileName);
InBlock.gif            i
++;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif    
if (i==0
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        printf(
"no redudant file \n");
ExpandedSubBlockEnd.gif    }

InBlock.gif    
else
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        printf(
"There are %d files need to be deleted \n", i);
InBlock.gif        printf(
"if you do not want to delete those files, press Ctrl-Break \n");
InBlock.gif        printf(
"otherwise anykeydot.gif\n");
InBlock.gif        getch();
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.gif            strcpy(dest, destpath);
InBlock.gif            strcat(dest, destFiles[j].fd.cFileName);
InBlock.gif            DeleteFile(dest);
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif    
return 0;
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.gifLONG OnPaint(HWND hWnd,UINT nMessage,WPARAM wParam,LPARAM lParam)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif    PAINTSTRUCT ps;
InBlock.gif    HDC hdc;
InBlock.gif    hdc 
= BeginPaint(hWnd, &ps);
InBlock.gif    WCHAR wszMsgInfo[
256];          
InBlock.gif    
string strInfo = "aa";
InBlock.gif    MultiByteToWideChar(CP_ACP,
0,strInfo.c_str(),strInfo.length(),wszMsgInfo,sizeof(wszMsgInfo)/sizeof(wszMsgInfo[0]));
InBlock.gif    ::TextOut(hdc,
10,10,wszMsgInfo,strInfo.length());
InBlock.gif    EndPaint(hWnd, 
&ps);
InBlock.gif    
return 0;
ExpandedBlockEnd.gif}

None.gif
None.gif

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

首先修改命令映射表

None.gifMSGMAP_ENTRY _commandEntries[] = 
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif    IDM_ABOUT,OnAbout,
InBlock.gif    IDM_EXIT,OnExit,
InBlock.gif    IDM_NOTEBOOK,OnNoteBook
ExpandedBlockEnd.gif}
;
None.gif

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

None.gifLONG OnNoteBook(HWND hWnd,UINT nMessage,WPARAM wParam,LPARAM lParam)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif    PROCESS_INFORMATION pInfo;
InBlock.gif    STARTUPINFO si;
InBlock.gif    ZeroMemory( 
&si, sizeof(si) );
InBlock.gif    si.cb 
= sizeof(si);
InBlock.gif    ZeroMemory( 
&pInfo, sizeof(pInfo) );
InBlock.gif   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);
 }
InBlock.gif    return 0;
ExpandedBlockEnd.gif}

None.gif

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

None.gifcase IDM_GETSYSTIME:
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif    DWORD dwThrId;
InBlock.gif    hSysTime 
= CreateThread(NULL,0, (LPTHREAD_START_ROUTINE)ReadCurTime,NULL,0,&dwThrId);
InBlock.gif    
if(hSysTime!=NULL)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        WaitForSingleObject(hSysTime,INFINITE);
InBlock.gif        CloseHandle(hSysTime);
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif
break;
None.gif
None.gif
void ReadCurTime(void)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{//当前系统时间
InBlock.gif
    SYSTEMTIME st;
InBlock.gif    GetSystemTime(
&st);
InBlock.gif    
char buffer[256];
InBlock.gif    sprintf(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、付费专栏及课程。

余额充值