Pour les besoins de l'exemple le contrôle sera dynamique et invisible, permettant ainsi un traitement sans interface utilisateur.
class CAboutDlg : public CDialog
{
public :
CAboutDlg ();
static DWORD CALLBACK RichEditStreamInCallback (DWORD dwCookie,
LPBYTE pbBuff, LONG cb, LONG FAR * pcb);
static DWORD CALLBACK RichEditStreamOutCallback (DWORD dwCookie,
LPBYTE pbBuff, LONG cb, LONG FAR * pcb);
boolm_bNewFile;
CString m_strRtfFileName;
HANDLE m_hFile;
} ;
|
DWORD CALLBACK CAboutDlg:: RichEditStreamOutCallback (
DWORD dwCookie,LPBYTE pbBuff, LONG cb, LONG FAR * pcb)
{
CAboutDlg* pDlg = reinterpret_cast < CAboutDlg* > (dwCookie);
if (pDlg- > m_bNewFile)
{
pDlg- > m_bNewFile= false ;
pDlg- > m_hFile = CreateFile (pDlg- > m_strRtfFileName,
GENERIC_WRITE,FILE_SHARE_WRITE,NULL ,CREATE_ALWAYS,0 ,NULL );
}
if (pDlg- > m_hFile = = INVALID_HANDLE_VALUE)return 1 ;
DWORD bytesWritten;
WriteFile (pDlg- > m_hFile,pbBuff,cb,& bytesWritten,NULL );
* pcb = bytesWritten;
return 0 ;
}
DWORD CALLBACK CAboutDlg:: RichEditStreamInCallback (
DWORD dwCookie, LPBYTE pbBuff, LONG cb, LONG FAR * pcb)
{
CAboutDlg* pDlg = reinterpret_cast < CAboutDlg* > (dwCookie);
if (pDlg- > m_bNewFile)
{
pDlg- > m_bNewFile= false ;
pDlg- > m_hFile = CreateFile (pDlg- > m_strRtfFileName,
GENERIC_READ,FILE_SHARE_READ,NULL ,OPEN_EXISTING,0 ,NULL );
}
if (pDlg- > m_hFile = = INVALID_HANDLE_VALUE)
return 1 ;
DWORD bytesRead;
int rt = ReadFile (pDlg- > m_hFile,pbBuff,cb,& bytesRead,NULL );
if (rt = = FALSE)
return 1 ;
* pcb = bytesRead;
if (bytesRead ! = (DWORD)cb | | bytesRead = = 0 )
{
if (pDlg- > m_hFile ! = INVALID_HANDLE_VALUE)
{
CloseHandle (pDlg- > m_hFile);
pDlg- > m_bNewFile= true ;
}
}
return 0 ;
}
BOOL CAboutDlg:: OnInitDialog ()
{
CDialog:: OnInitDialog ();
m_strRtfFileName= " c:\\initial.rtf " ;
m_bNewFile= true ;
m_hFile= INVALID_HANDLE_VALUE;
CRichEditCtrl* pRichEditCtrl = new CRichEditCtrl;
pRichEditCtrl- > Create (WS_CHILD| WS_BORDER| ES_MULTILINE,CRect (10 ,10 ,100 ,200 ),this ,1 );
EDITSTREAM es;
es.dwCookie = (DWORD_PTR )this ;
es.dwError = 0 ;
es.pfnCallback = (EDITSTREAMCALLBACK)RichEditStreamInCallback;
pRichEditCtrl- > StreamIn (SF_RTF,es);
m_strRtfFileName= " c:\\other.rtf " ;
es.dwCookie = (DWORD_PTR )this ;
es.dwError = 0 ;
es.pfnCallback = (EDITSTREAMCALLBACK)RichEditStreamOutCallback;
pRichEditCtrl- > StreamOut (SF_RTF,es);
if (m_hFile ! = INVALID_HANDLE_VALUE)
{
CloseHandle (m_hFile);
m_bNewFile= true ;
}
pRichEditCtrl- > DestroyWindow ();
delete pRichEditCtrl;
return TRUE;
}
|
|