📄 richeditpage.cpp
字号:
// CFTPTREEDlg::OnSize
if ((cx > 0) && (cy > 0))
{
CRect rect;
if (m_rtf.m_hWnd != NULL)
{
m_rtf.GetWindowRect(rect);
ScreenToClient(rect);
m_rtf.MoveWindow(rect.left, rect.top,
cx-(2*rect.left), cy-rect.top-rect.left);
}
// end sample
if ( m_toolBar.m_hWnd !=NULL)
{
m_toolBar.GetWindowRect(rect);
ScreenToClient(rect);
m_toolBar.MoveWindow(rect.left, rect.top,cx,rect.bottom);
}
}
}
void CRicheditPage::OnUpdateEditCut(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here
ASSERT(pCmdUI->m_nID == ID_EDIT_CUT);
WORD wSel= m_rtf.GetSelectionType ();
if ( wSel & SEL_TEXT )
pCmdUI->Enable ();
else //if ( m_rtf.GetSelectionType () == SEL_EMPTY)
pCmdUI->Enable (FALSE);
}
void CRicheditPage::OnUpdateEditCopy(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here
ASSERT(pCmdUI->m_nID == ID_EDIT_COPY);
WORD wSel= m_rtf.GetSelectionType ();
if ( wSel & SEL_TEXT )
pCmdUI->Enable ();
else if ( m_rtf.GetSelectionType () == SEL_EMPTY)
pCmdUI->Enable (FALSE);
}
// this code is taken from Vadim Z.'s article RTF based Tooltips
// www.codeguru.com
//----------------------------------------------------------------------------
// Function CMyRichEditCtrl::Print
// @func print the rich edit control content
// @parm bool | bShowPrintDialog | show the dialog box CPrintDialog
//----------------------------------------------------------------------------
// @todo
//----------------------------------------------------------------------------
void CRicheditPage::Print(BOOL bShowPrintDialog)
{
CPrintDialog printDialog(FALSE);
if (bShowPrintDialog)
{
int r = printDialog.DoModal();
if (r == IDCANCEL)
return; // User pressed cancel, don't print.
}
else
{
printDialog.GetDefaults();
}
HDC hPrinterDC = printDialog.GetPrinterDC();
// This code basically taken from MS KB article Q129860
FORMATRANGE fr;
int nHorizRes = GetDeviceCaps(hPrinterDC, HORZRES);
int nVertRes = GetDeviceCaps(hPrinterDC, VERTRES);
int nLogPixelsX = GetDeviceCaps(hPrinterDC, LOGPIXELSX);
int nLogPixelsY = GetDeviceCaps(hPrinterDC, LOGPIXELSY);
LONG lTextLength; // Length of document.
LONG lTextPrinted; // Amount of document printed.
// Ensure the printer DC is in MM_TEXT mode.
SetMapMode ( hPrinterDC, MM_TEXT );
// Rendering to the same DC we are measuring.
ZeroMemory(&fr, sizeof(fr));
fr.hdc = fr.hdcTarget = hPrinterDC;
// Set up the page.
fr.rcPage.left = fr.rcPage.top = 0;
fr.rcPage.right = (nHorizRes/nLogPixelsX) * 1440;
fr.rcPage.bottom = (nVertRes/nLogPixelsY) * 1440;
// Set up 0" margins all around.
fr.rc.left = fr.rcPage.left ;//+ 1440; // 1440 TWIPS = 1 inch.
fr.rc.top = fr.rcPage.top ;//+ 1440;
fr.rc.right = fr.rcPage.right ;//- 1440;
fr.rc.bottom = fr.rcPage.bottom ;//- 1440;
// Default the range of text to print as the entire document.
fr.chrg.cpMin = 0;
fr.chrg.cpMax = -1;
m_rtf.FormatRange(&fr,TRUE);
// Set up the print job (standard printing stuff here).
DOCINFO di;
ZeroMemory(&di, sizeof(di));
di.cbSize = sizeof(DOCINFO);
di.lpszDocName = m_strFilename;
// Do not print to file.
di.lpszOutput = NULL;
// Start the document.
StartDoc(hPrinterDC, &di);
// Find out real size of document in characters.
lTextLength = m_rtf.GetTextLength();
do
{
// Start the page.
StartPage(hPrinterDC);
// Print as much text as can fit on a page. The return value is
// the index of the first character on the next page. Using TRUE
// for the wParam parameter causes the text to be printed.
lTextPrinted =m_rtf.FormatRange(&fr,TRUE);
m_rtf.DisplayBand(&fr.rc );
// Print last page.
EndPage(hPrinterDC);
// If there is more text to print, adjust the range of characters
// to start printing at the first character of the next page.
if (lTextPrinted < lTextLength)
{
fr.chrg.cpMin = lTextPrinted;
fr.chrg.cpMax = -1;
}
}
while (lTextPrinted < lTextLength);
// Tell the control to release cached information.
m_rtf.FormatRange(NULL,FALSE);
EndDoc (hPrinterDC);
///////
DeleteDC(hPrinterDC);
}
// for updating dialog box controls
// this 2 functions are taken from Visual C++ for dummies Quick
// Reference book --- Dialog Command Enablers --- page 185 and 186
LRESULT CRicheditPage::OnKickIdle(WPARAM w, LPARAM l)
{
AfxGetApp()->OnIdle(-1);
OnIdleUpdateCmdUI(w,l);
return 0L;
}
LRESULT CRicheditPage::OnIdleUpdateCmdUI(WPARAM w, LPARAM l)
{
UpdateDialogControls((CCmdTarget*)this,TRUE);
return 0L;
}
// added on 2 sep 2003
void CRicheditPage::OnUpdateBold(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here
ASSERT(pCmdUI->m_nID == ID_BOLD);
// get the sel type
WORD wSel= m_rtf.GetSelectionType ();
// if sel is bold then show the Bold button pressed
if ( (wSel==SEL_MULTICHAR) && m_rtf.SelectionIsBold() )
pCmdUI->SetCheck(m_rtf.bold);
// if I have clicked in the window and immediately after the caret
// is a bold character, then show the Bold button pressed
else if ( m_bClick)
{
if ( m_rtf.SelectionIsBold() )
pCmdUI->SetCheck(m_rtf.bold);
else
pCmdUI->SetCheck(0);
}
else if ( m_rtf.GetSelectionType () == SEL_EMPTY)
{
pCmdUI->SetCheck(0);
}
}
// 2 sep 2003
void CRicheditPage::OnUpdateItalic(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here
ASSERT(pCmdUI->m_nID == ID_ITALIC);
// get the sel type
WORD wSel= m_rtf.GetSelectionType ();
// if sel is bold then show the Bold button pressed
if ( (wSel==SEL_MULTICHAR) && m_rtf.SelectionIsItalic() )
pCmdUI->SetCheck(1);
// if I have clicked in the window and immediately after the caret
// is a bold character, then show the Bold button pressed
else if ( m_bClick)
{
if ( m_rtf.SelectionIsItalic() )
pCmdUI->SetCheck(1);
else
pCmdUI->SetCheck(0);
}
else if ( m_rtf.GetSelectionType () == SEL_EMPTY)
{
pCmdUI->SetCheck(0);
}
}
// 2 sep 2003
void CRicheditPage::OnUpdateUnderline(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here
ASSERT(pCmdUI->m_nID == ID_UNDERLINE);
WORD wSel= m_rtf.GetSelectionType ();
if ( wSel & SEL_TEXT && m_rtf.SelectionIsUnderlined() )
pCmdUI->SetCheck(1 );
else if ( m_bClick)
{
if ( m_rtf.SelectionIsUnderlined() )
pCmdUI->SetCheck(1);
else
pCmdUI->SetCheck(0);
}
else if ( m_rtf.GetSelectionType () == SEL_EMPTY)
{
pCmdUI->SetCheck(0);
}
}
// 2 sep 2003
void CRicheditPage::OnUpdateEditPaste(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here
COleDataObject oleTarget;
oleTarget.AttachClipboard ();
pCmdUI->Enable(oleTarget.IsDataAvailable(CF_TEXT));
}
void CRicheditPage::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
// if I have clicked in the window and immediately after the caret
// is a bold,italic or underlined character, then show the Bold,
// Italic or Underlined button pressed
m_bClick=TRUE;
// get the caret position
point=m_rtf.GetCaretPos ();
// if there is no text in the window, do not select
// anything
if ( m_rtf.GetTextLength()==0 )
m_rtf.SetSel(0 , 0);
else
// select 1 character and verify if the character is
// bold, italic or underlined
m_rtf.SetSel(point.x , point.x+1);
m_rtf.ShowCaret();
// hide the selection. It is not importand for the user to
// see it
m_rtf.HideSelection(TRUE,FALSE);
CDialog::OnLButtonDown(nFlags, point);
}
void CRicheditPage::OnUpdateEditDelete(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here
ASSERT(pCmdUI->m_nID == ID_EDIT_DELETE);
WORD wSel= m_rtf.GetSelectionType ();
if ( wSel & SEL_TEXT )
pCmdUI->Enable ();
else if ( m_rtf.GetSelectionType () == SEL_EMPTY)
pCmdUI->Enable (FALSE);
}
void CRicheditPage::OnUpdateEditUndo(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here
if (m_rtf.CanUndo())
pCmdUI->Enable() ;
else pCmdUI->Enable(FALSE) ;
}
void CRicheditPage::OnUpdateParagraphBulleted(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here
ASSERT(pCmdUI->m_nID == ID_PARAGRAPH_BULLETED);
WORD wSel= m_rtf.GetSelectionType ();
if ( wSel & SEL_TEXT && m_rtf.ParagraphIsBulleted())
pCmdUI->SetCheck(1 );
else if ( m_bClick)
{
if ( m_rtf.ParagraphIsBulleted() )
pCmdUI->SetCheck(1);
else
pCmdUI->SetCheck(0);
}
else if ( m_rtf.GetSelectionType () == SEL_EMPTY)
{
pCmdUI->SetCheck(0);
}
}
void CRicheditPage::OnUpdateParagraphCenter(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here
ASSERT(pCmdUI->m_nID == ID_PARAGRAPH_CENTER);
WORD wSel= m_rtf.GetSelectionType ();
if ( wSel & SEL_TEXT && m_rtf.ParagraphIsCentered())
pCmdUI->SetCheck(1 );
else if ( m_bClick)
{
if ( m_rtf.ParagraphIsCentered() )
pCmdUI->SetCheck(1);
else
pCmdUI->SetCheck(0);
}
else if ( m_rtf.GetSelectionType () == SEL_EMPTY)
{
pCmdUI->SetCheck(0);
}
}
void CRicheditPage::OnUpdateParagraphLeft(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here
ASSERT(pCmdUI->m_nID == ID_PARAGRAPH_LEFT);
WORD wSel= m_rtf.GetSelectionType ();
if ( wSel & SEL_TEXT && m_rtf.ParagraphIsLeft())
pCmdUI->SetCheck(1 );
else if ( m_bClick)
{
if ( m_rtf.ParagraphIsLeft() )
pCmdUI->SetCheck(1);
else
pCmdUI->SetCheck(0);
}
else if ( m_rtf.GetSelectionType () == SEL_EMPTY)
{
pCmdUI->SetCheck(0);
}
}
void CRicheditPage::OnUpdateParagraphRight(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here
ASSERT(pCmdUI->m_nID == ID_PARAGRAPH_RIGHT);
WORD wSel= m_rtf.GetSelectionType ();
if ( wSel & SEL_TEXT && m_rtf.ParagraphIsRight())
pCmdUI->SetCheck(1 );
else if ( m_bClick)
{
if ( m_rtf.ParagraphIsRight() )
pCmdUI->SetCheck(1);
else
pCmdUI->SetCheck(0);
}
else if ( m_rtf.GetSelectionType () == SEL_EMPTY)
{
pCmdUI->SetCheck(0);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -