⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 filechangedoc.cpp

📁 visual c++ 实例编程
💻 CPP
字号:
// 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);

	//open the docfile
	CFile* pFile = pDoc->GetFile(pDoc->GetPathName(),CFile::modeRead|CFile::shareDenyWrite,NULL);	
	ASSERT(pFile);
	if(pFile)
	{
		//save the last write time
		FILETIME ftLastWriteTime;
		BY_HANDLE_FILE_INFORMATION fileinfo;
		if(!GetFileInformationByHandle((HANDLE)pFile->m_hFile,&fileinfo))
			return 0;
		ftLastWriteTime=fileinfo.ftLastWriteTime;
		pDoc->ReleaseFile(pFile,FALSE);	

		//determine the path
		char path[_MAX_PATH];
		_splitpath(pDoc->GetPathName(), NULL, path, NULL, NULL);
		while(1)
		{
			//get file notify event
			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};
	
			// wait for file changes and stop from the doc
			DWORD dwResult=WaitForMultipleObjects(2,hWaitEvents,FALSE,INFINITE);
			FindCloseChangeNotification(hFileChanged);	
			
			if(dwResult==WAIT_OBJECT_0+1)
			{
				//retrieve docfiles write time
				pFile = pDoc->GetFile(pDoc->GetPathName(),CFile::modeRead|CFile::shareDenyWrite,NULL);	
				if(pFile)
				{
					if(GetFileInformationByHandle((HANDLE)pFile->m_hFile, &fileinfo))
					{
						//detect if changes made
						if((ftLastWriteTime.dwHighDateTime+ftLastWriteTime.dwLowDateTime)!=(fileinfo.ftLastWriteTime.dwHighDateTime+fileinfo.ftLastWriteTime.dwLowDateTime))
							pDoc->OnFileAlarm(FA_WRITTEN);//do alarm

						//resave last time
						ftLastWriteTime=fileinfo.ftLastWriteTime;
					}
					pDoc->ReleaseFile(pFile,FALSE);
				}
				else
				{
					pDoc->OnFileAlarm(FA_FILELOST);//do alarm
				}
			}
			else
				break;
		}
	}
	return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -