Environment: VC6 SP4 Win2000
This class helps you to monitor files like in the DevStudio. If the file is
modified outside, a message box
pops up and lets you choose between ignoring modifications and reloading the
data, discarding all changes.
If the file is modified a message is send to the first view of the document. The
message handler will call the reload function in the document class.
Things you have to change in your ViewClass and DocumentClass:
In your document class header file:
class CFileWatchAppDoc : public CRichEditDoc
{
....
public:
void OnFileReload();
protected:
DWORD m_hFileWatch;
};
In your document class source file:
#include "FileWatch.h"
CFileWatchAppDoc::CFileWatchAppDoc()
{
m_hFileWatch = NULL;
}
CFileWatchAppDoc::~CFileWatchAppDoc()
{
CFileWatch::RemoveHandle(m_hFileWatch);
}
BOOL CFileWatchAppDoc::OnSaveDocument(LPCTSTR lpszPathName)
{
CFileWatch::RemoveHandle(m_hFileWatch);
BOOL bSuccess = CRichEditDoc::OnSaveDocument(lpszPathName);
m_hFileWatch = CFileWatch::AddFileFolder(lpszPathName, NULL, this, 0);
return bSuccess;
}
void CFileWatchAppDoc::SetPathName(LPCTSTR lpszPathName, BOOL bAddToMRU)
{
CFileWatch::RemoveHandle(m_hFileWatch);
m_hFileWatch = CFileWatch::AddFileFolder(lpszPathName, NULL, this, 0);
CRichEditDoc::SetPathName(lpszPathName, bAddToMRU);
}
void CFileWatchAppDoc::OnFileReload()
{
SetModifiedFlag(FALSE);
if (CDocument::OnOpenDocument(GetPathName()))
UpdateAllViews(NULL);
}
In your view class header file:
class CFileWatchAppView : public CRichEditView
{
...
protected:
//{{AFX_MSG(CFileWatchAppView)
//}}AFX_MSG
afx_msg LRESULT OnFileWatchNotification(WPARAM wParam, LPARAM lParam);
DECLARE_MESSAGE_MAP()
};
In your view class source file:
BEGIN_MESSAGE_MAP(CFileWatchAppView, CRichEditView)
//{{AFX_MSG_MAP(CFileWatchAppView)
//}}AFX_MSG_MAP
ON_THREAD_MESSAGE(WM_FILEWATCH_NOTIFY, OnFileWatchNotification)
END_MESSAGE_MAP()
LRESULT CFileWatchAppView::OnFileWatchNotification(WPARAM wParam, LPARAM lParam)
{
LPCTSTR lpszPathName = (LPCTSTR)lParam;
if (AfxMessageBox(GetDocument()->GetPathName()+"\n\nThis file has been modified outside of the editor.\nDo you want to reload it and lose all the changes made?",
MB_YESNO|MB_ICONQUESTION)==IDYES)
GetDocument()->OnFileReload();
return 0;
}
Beware: The test function void CFileWatchAppDoc::OnFileModify() doesn't work always!