[转]在MFC中的文档视图中对视图使用RichEdit2--来自CodeGuru
The following is an example of rich edit2.0 based on the default SD rich edit project.
Please email me for source code.
1.revise PreCreateWindows
BOOL CRich20DocViewView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
// return CRichEditView::PreCreateWindow(cs);
m_strClass="RichEdit20A";
BOOL nR=CRichEditView::PreCreateWindow(cs);
LoadLibraryA("RICHED20.DLL");
return nR;
}
2. revise OnDestory
void CRich20DocViewView::OnDestroy()
{
// Deactivate the item on destruction; this is important
// when a splitter view is being used.
// CRichEditView::OnDestroy();
COleClientItem* pActiveItem = GetDocument()->GetInPlaceActiveItem(this);
if (pActiveItem != NULL && pActiveItem->GetActiveView() == this)
{
pActiveItem->Deactivate();
ASSERT(GetDocument()->GetInPlaceActiveItem(this) == NULL);
}
CRichEditView::OnDestroy();
}
3. Enable auto URL detect in OnInitialUpdate()
long lENM=GetRichEditCtrl().GetEventMask();
lENM|=ENM_LINK;
GetRichEditCtrl().SetEventMask(lENM);
BOOL bEnable=1;
::SendMessage(m_hWnd,EM_AUTOURLDETECT,bEnable,0);
4. URL left-button_dwon open with notify message
ON_NOTIFY_REFLECT_EX(EN_LINK, OnLink )
afx_msg void OnLink( NMHDR* in_pNotifyHeader, LRESULT* out_pResult );
void CRich20DocViewView::OnLink( NMHDR* in_pNotifyHeader, LRESULT* out_pResult )
{
ENLINK * pLink=(ENLINK*) in_pNotifyHeader;
if(pLink->msg==WM_LBUTTONDOWN)
{
GetRichEditCtrl().SetSel(pLink->chrg);
CString str=GetRichEditCtrl().GetSelText();
ShellExecute( this->GetSafeHwnd(), _T( "open" ), str, NULL, NULL, SW_SHOWNORMAL ) ;
}
}
5. To print correctly, revise OnPrint. Without the following, there might be a non-stop print with blank pages)
void CRich20DocViewView::OnPrint(CDC* pDC, CPrintInfo* pInfo)
{
// TODO: Add your specialized code here and/or call the base class
ASSERT_VALID(this);
ASSERT_VALID(pDC);
ASSERT(pInfo != NULL);
ASSERT(pInfo->m_bContinuePrinting);
UINT nPage = pInfo->m_nCurPage;
ASSERT(nPage <= (UINT)m_aPageStart.GetSize());
long nIndex = (long) m_aPageStart[nPage-1];
// print as much as possible in the current page.
nIndex = PrintPage(pDC, nIndex, 0xFFFFFFFF);
//nIndex got above hasn't take "return" into account
//The following codes add nIndex when there are any new lines
int nLength=GetTextLength();
CString strRich;
GetRichEditCtrl().GetWindowText(strRich);
int nS=0;
while(nS!=-1)
{
nS=strRich.Find("\r\n",nS);
if(nS!=-1)
{
nLength--;
nS++;
}
}
//END adding nIndex
//other error that might cause non-stop print
if(m_nIndex==nIndex)
nLength-=10000;
m_nIndex=nIndex;
//end other erro
if (nIndex >= nLength)//GetTextLength()
{
TRACE0("End of Document\n");
pInfo->SetMaxPage(nPage);
}
// update pagination information for page just printed
if (nPage == (UINT)m_aPageStart.GetSize())
{
if (nIndex < nLength)//GetTextLength()
m_aPageStart.Add(nIndex);
}
else
{
ASSERT(nPage+1 <= (UINT)m_aPageStart.GetSize());
ASSERT(nIndex == (long)m_aPageStart[nPage+1-1]);
}
// CRichEditView::OnPrint(pDC, pInfo);
}