FileChangeDoc.cpp
上传用户:lqt88888
上传日期:2009-12-14
资源大小:905k
文件大小:5k
- // FileChangeDoc.cpp : implementation of the CFileChangeDoc class
- //
- #include "stdafx.h"
- #include "FileChange.h"
- #include "MainFrm.h"
- #include "FileChangeDoc.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- /////////////////////////////////////////////////////////////////////////////
- // CFileChangeDoc
- IMPLEMENT_DYNCREATE(CFileChangeDoc, CDocument)
- BEGIN_MESSAGE_MAP(CFileChangeDoc, CDocument)
- //{{AFX_MSG_MAP(CFileChangeDoc)
- // NOTE - the ClassWizard will add and remove mapping macros here.
- // DO NOT EDIT what you see in these blocks of generated code!
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CFileChangeDoc construction/destruction
- CFileChangeDoc::CFileChangeDoc()
- {
- // TODO: add one-time construction code here
-
- }
- CFileChangeDoc::~CFileChangeDoc()
- {
- }
- BOOL CFileChangeDoc::OnNewDocument()
- {
- if (!CDocument::OnNewDocument())
- return FALSE;
-
- // TODO: add reinitialization code here
- // (SDI documents will reuse this document)
-
- return TRUE;
- }
- /////////////////////////////////////////////////////////////////////////////
- // CFileChangeDoc serialization
- void CFileChangeDoc::Serialize(CArchive& ar)
- {
- // CEditView contains an edit control which handles all serialization
- ((CEditView*)m_viewList.GetHead())->SerializeRaw(ar);
- }
- /////////////////////////////////////////////////////////////////////////////
- // CFileChangeDoc diagnostics
- #ifdef _DEBUG
- void CFileChangeDoc::AssertValid() const
- {
- CDocument::AssertValid();
- }
- void CFileChangeDoc::Dump(CDumpContext& dc) const
- {
- CDocument::Dump(dc);
- }
- #endif //_DEBUG
- /////////////////////////////////////////////////////////////////////////////
- // CFileChangeDoc commands
- BOOL CFileChangeDoc::OnSaveDocument(LPCTSTR lpszPathName)
- {
- WatchStop();
- BOOL bRet=CDocument::OnSaveDocument(lpszPathName);
- WatchStart();
- return bRet;
- }
- void CFileChangeDoc::OnCloseDocument()
- {
- WatchStop();
- CDocument::OnCloseDocument();
- }
- BOOL CFileChangeDoc::OnOpenDocument(LPCTSTR lpszPathName)
- {
- if (!CDocument::OnOpenDocument(lpszPathName))
- return FALSE;
-
- //save the filename
- m_strPathName=lpszPathName;
- //start the watch
- WatchStart();
-
- return TRUE;
- }
- void CFileChangeDoc::OnFileAlarm(short nCause)
- {
- MessageBeep(MB_ICONEXCLAMATION);
-
- CMainFrame* pwndMain=(CMainFrame*)AfxGetApp()->GetMainWnd();
- ASSERT(pwndMain);
-
- CView* pView=(CView*)m_viewList.GetHead();
- pwndMain->MDIActivate(pView->GetParent());
-
- CString sMsg;
- switch(nCause)
- {
- case FA_WRITTEN:
- sMsg.Format("文件 %s 已被改变!",m_strPathName);
- AfxMessageBox(sMsg);
- break;
- case FA_FILELOST:
- sMsg.Format("文件 %s 已被删除或移动!",m_strPathName);
- AfxMessageBox("file deleted/moved");
- break;
- }
- }
- //Start the watching thread
- void CFileChangeDoc::WatchStart()
- {
- m_evStopWatch.ResetEvent();
- AfxBeginThread(FileChangeWatch,(LPVOID)this);
- }
- //Stop the watching thread
- void CFileChangeDoc::WatchStop()
- {
- m_evStopWatch.SetEvent();
- }
- UINT CFileChangeDoc::FileChangeWatch(LPVOID lpParam)
- {
- CFileChangeDoc* pDoc=(CFileChangeDoc*)lpParam;
- ASSERT(pDoc);
-
- CFile* pFile = pDoc->GetFile(pDoc->GetPathName(),
- CFile::modeRead|CFile::shareDenyWrite,NULL);
- ASSERT(pFile);
- if(pFile)
- {
- // 保存上次写文件的时间
- FILETIME ftLastWriteTime;
- BY_HANDLE_FILE_INFORMATION fileinfo;
- if(!GetFileInformationByHandle((HANDLE)pFile->m_hFile,&fileinfo))
- return 0;
- ftLastWriteTime=fileinfo.ftLastWriteTime;
- pDoc->ReleaseFile(pFile,FALSE);
-
- // 分离文件路径
- char path[_MAX_PATH];
- _splitpath(pDoc->GetPathName(), NULL, path, NULL, NULL);
- while(1)
- {
- // 获取文件更改通知句柄
- HANDLE hFileChanged=FindFirstChangeNotification(path,FALSE,
- FILE_NOTIFY_CHANGE_LAST_WRITE|
- FILE_NOTIFY_CHANGE_SIZE|
- FILE_NOTIFY_CHANGE_FILE_NAME);
-
- HANDLE hWaitEvents[]={pDoc->m_evStopWatch,hFileChanged};
- // 等待文件更改通知或停止监视事件
- DWORD dwResult=
- WaitForMultipleObjects(2,hWaitEvents,FALSE,INFINITE);
- FindCloseChangeNotification(hFileChanged);
-
- if(dwResult==WAIT_OBJECT_0+1)
- {
- // 获取文件的保存时间
- pFile = pDoc->GetFile(pDoc->GetPathName(),
- CFile::modeRead|CFile::shareDenyWrite,NULL);
- if(pFile)
- {
- if(GetFileInformationByHandle((HANDLE)pFile->m_hFile,
- &fileinfo))
- {
- // 判断文件是否已更改
- if((ftLastWriteTime.dwHighDateTime+
- ftLastWriteTime.dwLowDateTime)!=
- (fileinfo.ftLastWriteTime.dwHighDateTime+
- fileinfo.ftLastWriteTime.dwLowDateTime))
- pDoc->OnFileAlarm(FA_WRITTEN);
- // 保存修改时间
- ftLastWriteTime=fileinfo.ftLastWriteTime;
- }
- pDoc->ReleaseFile(pFile,FALSE);
- }
- else // 文件不存在,已被删除或移动
- {
- pDoc->OnFileAlarm(FA_FILELOST);
- }
- }
- else
- break;
- }
- }
- return 0;
- }