📄 richeditctrlgs.cpp
字号:
pf.wAlignment = PFA_LEFT;
SetParaFormat(pf); // Set the paragraph.
}
void CRichEditCtrlGS::SetCenter(void)
{ PARAFORMAT pf;
pf.cbSize = sizeof(PARAFORMAT);
pf.dwMask = PFM_ALIGNMENT;
pf.wAlignment = PFA_CENTER;
SetParaFormat(pf); // Set the paragraph.
}
void CRichEditCtrlGS::SetJustify(void)
{ PARAFORMAT pf;
pf.cbSize = sizeof(PARAFORMAT);
pf.dwMask = PFM_ALIGNMENT;
pf.wAlignment = PFA_JUSTIFY;
SetParaFormat(pf); // Set the paragraph.
}
void CRichEditCtrlGS::SetBullet(void)
{ int iFormatted = IsBulleted();
PARAFORMAT pf;
pf.cbSize = sizeof(PARAFORMAT);
pf.dwMask = PFM_NUMBERING | PFM_STARTINDENT | PFM_OFFSET;
GetParaFormat(pf);
if( 1 == iFormatted )
{ pf.wNumbering = 0;
pf.dxOffset = 0;
pf.dwMask = PFM_NUMBERING | PFM_STARTINDENT | PFM_OFFSET;
}
else
{ pf.wNumbering = PFN_BULLET;
pf.dwMask = PFM_NUMBERING;
if( pf.dxOffset == 0 )
{ pf.dxOffset = 160;
pf.dwMask = PFM_NUMBERING | PFM_STARTINDENT | PFM_OFFSET;
}
}
SetParaFormat(pf);
}
// Visual Appearance
void CRichEditCtrlGS::SetWordWrap(const bool bOn/*= true*/,const int iLineWidth/* = 0*/)
{ if( bOn )
{ SetTargetDevice(NULL, iLineWidth);
}
else
{ if( 0 == iLineWidth )
{ SetTargetDevice(NULL, 1);
}
else
{ SetTargetDevice(NULL, iLineWidth);
}
}
}
// Reading and Writing
void CRichEditCtrlGS::SetRTF(const CString strText)
{ SCookieString CookieRTF;
CookieRTF.lSize = strText.GetLength();
CookieRTF.lStart = 0;
CookieRTF.pInText = &strText;
CookieRTF.pOutText = NULL;
CString strRTF = _T("");
if( strText.GetLength() > 5 )
{ strRTF = strText.Left(5);
}
// Read the text in
EDITSTREAM es;
es.dwError = 0;
es.pfnCallback = StreamInCString; // Set the callback
es.dwCookie = (DWORD)&CookieRTF; // and the informations
if( strRTF.CompareNoCase(_T("{\\rtf")) == 0 )
{ StreamIn(SF_RTF,es);
}
else
{ StreamIn(SF_TEXT,es);
}
}
void CRichEditCtrlGS::SetRTF(const CByteArray& arrRTF)
{ SCookieByteArray CookieRTF;
CookieRTF.lSize = arrRTF.GetSize();
CookieRTF.lStart = 0;
CookieRTF.pInText = &arrRTF;
CookieRTF.pOutText = NULL;
CString strRTF = _T("");
if( arrRTF.GetSize() > 5 )
{ strRTF = TCHAR(arrRTF[0]);
strRTF += TCHAR(arrRTF[1]);
strRTF += TCHAR(arrRTF[2]);
strRTF += TCHAR(arrRTF[3]);
strRTF += TCHAR(arrRTF[4]);
}
// Read the text in
EDITSTREAM es;
es.dwError = 0;
es.pfnCallback = StreamInCByteArray; // Set the callback
es.dwCookie = (DWORD)&CookieRTF; // and the informations
if( strRTF.CompareNoCase(_T("{\\rtf")) == 0 )
{ StreamIn(SF_RTF,es);
}
else
{ StreamIn(SF_TEXT,es);
}
}
void CRichEditCtrlGS::SetRTF(const UINT resID)
{ // Obtain a handle and the memory to the resource
HINSTANCE hApp = ::GetModuleHandle(0);
ASSERT(hApp);
HRSRC hResInfo = ::FindResource(hApp, MAKEINTRESOURCE(resID), TEXT("RTF"));
if( NULL == hResInfo ) return;
HGLOBAL hRes = ::LoadResource(hApp, hResInfo);
if( NULL == hRes ) return;
LPVOID pRTFText = ::LockResource(hRes);
if( NULL == pRTFText ) return;
DWORD dwRTFSize = ::SizeofResource(hApp, hResInfo);
if( 0 == dwRTFSize )
{ ::FreeResource(hRes);
return;
}
CByteArray arrbRTF;
arrbRTF.SetSize(dwRTFSize);
LPBYTE pArrRTF = arrbRTF.GetData();
::CopyMemory(pArrRTF,pRTFText,dwRTFSize);
::FreeResource(hRes);
SetRTF(arrbRTF);
}
void CRichEditCtrlGS::LoadRTF(const CString strFilename)
{
SCookieFile CookieRTF;
CFileStatus fileStatus;
CString strRTF = _T("");
CookieRTF.lStart = 0;
CookieRTF.pFilename = &strFilename;
// Close all filehandles
if( CFile::hFileNull != CookieRTF.fileInText.m_hFile )
{
CookieRTF.fileInText.Close();
}
if( CFile::hFileNull != CookieRTF.fileOutText.m_hFile )
{
CookieRTF.fileOutText.Close();
}
// Is the filename the name of a file?
if( CFile::GetStatus(strFilename, fileStatus) )
{
if( (fileStatus.m_attribute & ( CFile::volume | CFile::directory )) )
{
AfxMessageBox(AFX_IDP_INVALID_FILENAME);
return; // Filename is not the name of a file
}
}
else
{
AfxMessageBox(AFX_IDP_INVALID_FILENAME);
return;// The name is not existing
}
CookieRTF.lSize = fileStatus.m_size;
if( !CookieRTF.fileInText.Open( *CookieRTF.pFilename,CFile::modeRead | CFile::shareDenyNone | CFile::typeBinary ) )
{
AfxMessageBox(AFX_IDP_FAILED_TO_OPEN_DOC);
return; // File could not be opened
}
CookieRTF.fileInText.SeekToBegin();
LPTSTR pBuff = strRTF.GetBuffer(15);
CookieRTF.fileInText.Read(pBuff,5);
pBuff[5] = 0;
strRTF.ReleaseBuffer();
EDITSTREAM es;
es.dwError = 0;
es.pfnCallback = StreamInCFile; // Set the callback
es.dwCookie = (DWORD)&CookieRTF; // and the informations
if( strRTF.CompareNoCase(_T("{\\rtf")) == 0 )
{
StreamIn(SF_RTF,es);
}
else
{
StreamIn(SF_TEXT,es);
}
CookieRTF.fileInText.Close();
}
void CRichEditCtrlGS::GetRTF(CString& strText,const bool bAsRTF/*=true*/)
{ SCookieString CookieRTF;
strText.Empty();
CookieRTF.lSize = 0;
CookieRTF.lStart = 0;
CookieRTF.pInText = NULL;
CookieRTF.pOutText = &strText;
// Pull the text out
EDITSTREAM es;
es.dwError = 0;
es.pfnCallback = StreamOutCString; // Set the callback
es.dwCookie = (DWORD)&CookieRTF; // and the informations
if( bAsRTF )
{ StreamOut(SF_RTF,es);
}
else
{ StreamOut(SF_TEXT,es);
}
return;
}
void CRichEditCtrlGS::GetRTF(CByteArray& arrText, const bool bAsRTF/*=true*/)
{ SCookieByteArray CookieRTF;
arrText.RemoveAll();
CookieRTF.lSize = 0;
CookieRTF.lStart = 0;
CookieRTF.pInText = NULL;
CookieRTF.pOutText = &arrText;
// Pull the text out
EDITSTREAM es;
es.dwError = 0;
es.pfnCallback = StreamOutCByteArray; // Set the callback
es.dwCookie = (DWORD)&CookieRTF; // and the informations
if( bAsRTF )
{ StreamOut(SF_RTF,es);
}
else
{ StreamOut(SF_TEXT,es);
}
}
void CRichEditCtrlGS::WriteRTF(const CString strFilename, const bool bAsRTF/*=true*/ )
{ SCookieFile CookieRTF;
CFileStatus fileStatus;
CString strRTF = _T("");
CookieRTF.lStart = 0;
CookieRTF.pFilename = &strFilename;
// Close all filehandles
if( CFile::hFileNull != CookieRTF.fileInText.m_hFile )
{ CookieRTF.fileInText.Close();
}
if( CFile::hFileNull != CookieRTF.fileOutText.m_hFile )
{ CookieRTF.fileOutText.Close();
}
if( !CookieRTF.fileOutText.Open( *CookieRTF.pFilename,CFile::modeCreate | CFile::modeReadWrite | CFile::typeBinary ) )
{ AfxMessageBox(AFX_IDP_FAILED_TO_SAVE_DOC);
return; // File could not be opened for saving
}
CookieRTF.fileOutText.SeekToBegin();
EDITSTREAM es;
es.dwError = 0;
es.pfnCallback = StreamOutCFile; // Set the callback
es.dwCookie = (DWORD)&CookieRTF; // and the informations
if( bAsRTF )
{ StreamOut(SF_RTF,es);
}
else
{ StreamOut(SF_TEXT,es);
}
CookieRTF.fileOutText.Close();
}
// StreamIn StreamOut methods
DWORD CALLBACK CRichEditCtrlGS::StreamInCString(DWORD dwCookie, LPBYTE pbBuff, LONG cb, LONG *pcb)
{ SCookieString *pCookie = (SCookieString *)dwCookie;
long lMaxSize = cb; // We may only read such much
long lReadSize = cb; // We can only read such much
*pcb = 0; // Till now we have nothing read so far
if( pCookie->lStart < 0 )
{ return(0); // Nothing to do
}
if( NULL == pCookie->pInText )
{ return(0); // Nothing to do
}
if( pCookie->pInText->GetLength() < cb )
{ lMaxSize = pCookie->pInText->GetLength();
}
lReadSize = lMaxSize;
if( (pCookie->lSize - pCookie->lStart) < lReadSize )
{ lReadSize = pCookie->lSize - pCookie->lStart;
}
if( lReadSize <= 0 )
{ return (0); // Nothing to do
}
LPCTSTR pText = (LPCTSTR)(*(pCookie->pInText));
pText += pCookie->lStart;
pCookie->lStart += lReadSize;
*pcb = lReadSize;
memcpy(pbBuff,pText,lReadSize);
return 0;
}
DWORD CALLBACK CRichEditCtrlGS::StreamInCByteArray(DWORD dwCookie, LPBYTE pbBuff, LONG cb, LONG *pcb)
{ SCookieByteArray *pCookie = (SCookieByteArray *)dwCookie;
long lMaxSize = cb; // We may only read such much
long lReadSize = cb; // We can only read such much
*pcb = 0; // Till now we have nothing read so far
if( pCookie->lStart < 0 )
{ return(0); // Nothing to do
}
if( NULL == pCookie->pInText )
{ return(0); // Nothing to do
}
if( pCookie->pInText->GetSize() < cb )
{ lMaxSize = pCookie->pInText->GetSize();
}
lReadSize = lMaxSize;
if( (pCookie->lSize - pCookie->lStart) < lReadSize )
{ lReadSize = pCookie->lSize - pCookie->lStart;
}
if( lReadSize <= 0 )
{ return (0); // Nothing to do
}
LPCTSTR pText = (LPCTSTR)(pCookie->pInText->GetData());
pText += pCookie->lStart;
pCookie->lStart += lReadSize;
*pcb = lReadSize;
memcpy(pbBuff,pText,lReadSize);
return 0;
}
DWORD CALLBACK CRichEditCtrlGS::StreamInCFile(DWORD dwCookie, LPBYTE pbBuff, LONG cb, LONG *pcb)
{ SCookieFile *pCookie = (SCookieFile *)dwCookie;
UINT uiReadSize = 0;
*pcb = 0; // Till now we have nothing read so far
if( pCookie->lStart < 0 )
{ return (0); // Nothing to do
}
if( 0 == cb )
{ return(0); // Nothing to do
}
if( CFile::hFileNull == pCookie->fileInText.m_hFile )
{ return(0); // Nothing to do
}
pCookie->fileInText.Seek(pCookie->lStart,CFile::begin);
uiReadSize = pCookie->fileInText.Read(pbBuff,cb);
*pcb = uiReadSize;
pCookie->lStart += uiReadSize;
return 0;
}
DWORD CALLBACK CRichEditCtrlGS::StreamOutCString(DWORD dwCookie, LPBYTE pbBuff, LONG cb, LONG* pcb)
{ SCookieString *pCookie = (SCookieString *)dwCookie;
long lWriteSize = cb; // We can only Write such much
*pcb = 0; // Till now we have nothing written so far
if( pCookie->lStart < 0 )
{ return (0); // Nothing to do
}
if( 0 == cb )
{ return(0); // Nothing to do
}
if( NULL == pCookie->pOutText )
{ return(0); // Nothing to do
}
CString strBuffer = _T("");
strBuffer = (CString)pbBuff;
(*(pCookie->pOutText)) += strBuffer.Left(lWriteSize);
*pcb = lWriteSize;
pCookie->lStart += lWriteSize;
return 0;
}
DWORD CALLBACK CRichEditCtrlGS::StreamOutCByteArray(DWORD dwCookie, LPBYTE pbBuff, LONG cb, LONG* pcb)
{ SCookieByteArray *pCookie = (SCookieByteArray *)dwCookie;
long lWriteSize = cb; // We can only Write such much
*pcb = 0; // Till now we have nothing written so far
if( pCookie->lStart < 0 )
{ return (0); // Nothing to do
}
if( 0 == cb )
{ return(0); // Nothing to do
}
if( NULL == pCookie->pOutText )
{ return(0); // Nothing to do
}
CByteArray arrBuffer;
arrBuffer.SetSize(lWriteSize);
BYTE *pByte = arrBuffer.GetData();
memcpy(pByte,pbBuff,lWriteSize);
pCookie->pOutText->Append(arrBuffer);
*pcb = lWriteSize;
pCookie->lStart += lWriteSize;
return 0;
}
DWORD CALLBACK CRichEditCtrlGS::StreamOutCFile(DWORD dwCookie, LPBYTE pbBuff, LONG cb, LONG *pcb)
{ SCookieFile *pCookie = (SCookieFile *)dwCookie;
*pcb = 0; // Till now we have nothing written so far
if( pCookie->lStart < 0 )
{ return (0); // Nothing to do
}
if( 0 == cb )
{ return(0); // Nothing to do
}
if( CFile::hFileNull == pCookie->fileOutText.m_hFile )
{ return(0); // Nothing to do
}
try
{ pCookie->fileOutText.SeekToEnd();
pCookie->fileOutText.Write(pbBuff,cb);
}
catch (CFileException *e)
{ AfxMessageBox(AFX_IDP_FAILED_TO_SAVE_DOC);
e->Delete();
return (1); // File could not be opened for saving so stop saving
}
*pcb = cb;
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -