📄 editview.cpp
字号:
// EditView.cpp : implementation of the CMyEditView class
//
#include "stdafx.h"
#include "Edit.h"
#include "FormatBar.h"
#include "EditDoc.h"
#include "CntrItem.h"
#include "EditView.h"
#include "ParagraphDialog.h"
#include "DateDialog.h"
#include <io.h>
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
class RichEditLoader
{
public:
RichEditLoader() : hInst(0)
{
}
~RichEditLoader()
{
if (hInst)
FreeLibrary(hInst);
}
HINSTANCE Load()
{
return LoadLibraryA("RICHED20.DLL");
}
HINSTANCE hInst;
}
Loader;
BOOL CMyEditView::ExtAfxInitRichEdit(BOOL bUnicode)
{
if (!Loader.Load())
{
AfxMessageBox(IDS_MFRM2);
AfxInitRichEdit();
}
if (bUnicode)
m_strClass = "RichEdit20W";
else
m_strClass = "RichEdit20A";
return TRUE;
}
/////////////////////////////////////////////////////////////////////////////
// CMyEditView
IMPLEMENT_DYNCREATE(CMyEditView, CRichEditView)
BEGIN_MESSAGE_MAP(CMyEditView, CRichEditView)
//{{AFX_MSG_MAP(CMyEditView)
ON_WM_DESTROY()
ON_COMMAND(ID_CHAR_STRIKEOUT, OnCharStrikeout)
ON_UPDATE_COMMAND_UI(ID_CHAR_STRIKEOUT, OnUpdateCharStrikeout)
ON_WM_CONTEXTMENU()
ON_WM_RBUTTONUP()
ON_COMMAND(ID_EDIT_REDO, OnEditRedo)
ON_UPDATE_COMMAND_UI(ID_EDIT_REDO, OnUpdateEditRedo)
ON_WM_CHAR()
ON_COMMAND(ID_CHAR_HIGHLIGHT, OnCharHighlight)
ON_COMMAND(ID_PARAGRAPH, OnParagraph)
ON_COMMAND(ID_DATE, OnDate)
ON_COMMAND(ID_SUBSCRIPT, OnSubscript)
ON_UPDATE_COMMAND_UI(ID_SUBSCRIPT, OnUpdateSubscript)
ON_COMMAND(ID_SUPERSCRIPT, OnSuperscript)
ON_UPDATE_COMMAND_UI(ID_SUPERSCRIPT, OnUpdateSuperscript)
ON_COMMAND(ID_INSERT_NUMBERING, OnInsertNumbering)
ON_UPDATE_COMMAND_UI(ID_INSERT_NUMBERING, OnUpdateInsertNumbering)
ON_COMMAND(ID_CHAR_BOLD, OnCharBold)
ON_UPDATE_COMMAND_UI(ID_CHAR_BOLD, OnUpdateCharBold)
ON_COMMAND(ID_CHAR_ITALIC, OnCharItalic)
ON_UPDATE_COMMAND_UI(ID_CHAR_ITALIC, OnUpdateCharItalic)
ON_COMMAND(ID_CHAR_UNDERLINE, OnCharUnderline)
ON_UPDATE_COMMAND_UI(ID_CHAR_UNDERLINE, OnUpdateCharUnderline)
ON_COMMAND(ID_PARA_CENTER, OnParaCenter)
ON_UPDATE_COMMAND_UI(ID_PARA_CENTER, OnUpdateParaCenter)
ON_COMMAND(ID_PARA_LEFT, OnParaLeft)
ON_UPDATE_COMMAND_UI(ID_PARA_LEFT, OnUpdateParaLeft)
ON_COMMAND(ID_PARA_RIGHT, OnParaRight)
ON_UPDATE_COMMAND_UI(ID_PARA_RIGHT, OnUpdateParaRight)
ON_COMMAND(ID_CHAR_COLOR, OnColorPick)
ON_COMMAND(ID_FILE_PRINT, OnFilePrint)
ON_COMMAND(ID_CHANGE_CASE, OnChangeCase)
ON_UPDATE_COMMAND_UI(ID_CHANGE_CASE, OnUpdateChangeCase)
//}}AFX_MSG_MAP
ON_NOTIFY(FN_GETFORMAT, IDR_FORMATBAR, OnGetCharFormat)
ON_NOTIFY(FN_SETFORMAT, IDR_FORMATBAR, OnSetCharFormat)
ON_NOTIFY(NM_RETURN, IDR_FORMATBAR, OnBarReturn)
// Standard printing commands
ON_COMMAND(ID_INSERT_BULLET, CRichEditView::OnBullet)
ON_UPDATE_COMMAND_UI(ID_INSERT_BULLET, CRichEditView::OnUpdateBullet)
ON_COMMAND(ID_FILE_PRINT_DIRECT, CRichEditView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, CRichEditView::OnFilePrintPreview)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMyEditView construction/destruction
CMyEditView::CMyEditView()
{
// TODO: add construction code here
m_nBulletIndent = 360;
}
CMyEditView::~CMyEditView()
{
}
BOOL CMyEditView::PreCreateWindow(CREATESTRUCT& cs)
{
ExtAfxInitRichEdit(FALSE);
return CCtrlView::PreCreateWindow(cs);
}
void CMyEditView::OnInitialUpdate()
{
CRichEditView::OnInitialUpdate();
CRichEditCtrl& rCtrl = GetRichEditCtrl();
CHARFORMAT cfm;
cfm.cbSize = sizeof(CHARFORMAT);
cfm.dwMask = CFM_FACE | CFM_SIZE | CFM_BOLD |
CFM_ITALIC | CFM_UNDERLINE | CFM_STRIKEOUT | CFM_PROTECTED;
cfm.dwEffects = 0;
cfm.yHeight = 240;
::lstrcpy(cfm.szFaceName, "Times New Roman"); rCtrl.SetWordCharFormat(cfm);
SetMargins(CRect(720, 720, 720, 720));
}
/////////////////////////////////////////////////////////////////////////////
// CMyEditView printing
BOOL CMyEditView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CMyEditView::OnDestroy()
{
CRichEditView::OnDestroy();
}
/////////////////////////////////////////////////////////////////////////////
// CMyEditView diagnostics
#ifdef _DEBUG
void CMyEditView::AssertValid() const
{
CRichEditView::AssertValid();
}
void CMyEditView::Dump(CDumpContext& dc) const
{
CRichEditView::Dump(dc);
}
CEditDoc* CMyEditView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CEditDoc)));
return (CEditDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CMyEditView message handlers
void CMyEditView::OnGetCharFormat(NMHDR* pNMHDR, LRESULT* pRes)
{
((CHARNMHDR*)pNMHDR)->cf = GetCharFormatSelection();
*pRes = 1;
}
void CMyEditView::OnSetCharFormat(NMHDR* pNMHDR, LRESULT* pRes)
{
SetCharFormat(((CHARNMHDR*)pNMHDR)->cf);
*pRes = 1;
}
void CMyEditView::OnBarReturn(NMHDR*, LRESULT* )
{
SetFocus();
}
void CMyEditView::OnColorPick()
{
CColorDialog dlg;
if( dlg.DoModal() == IDOK ){
CRichEditView::OnColorPick(dlg.GetColor());
}
}
void CMyEditView::OnCharStrikeout()
{
// TODO: Add your command handler code here
OnCharEffect(CFM_STRIKEOUT, CFE_STRIKEOUT);
}
void CMyEditView::OnUpdateCharStrikeout(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here
OnUpdateCharEffect(pCmdUI, CFM_STRIKEOUT, CFE_STRIKEOUT);
}
void CMyEditView::OnRButtonUp(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
ClientToScreen(&point);
OnContextMenu((CWnd* )NULL, point);
}
void CMyEditView::OnContextMenu(CWnd* pWnd, CPoint point)
{
// TODO: Add your message handler code here
if( point.x == -1 && point.y == -1 )
{
CRect rect;
GetClientRect(rect);
ClientToScreen(rect);
point = rect.TopLeft();
point.Offset(5, 5);
}
CMenu menu;
VERIFY(menu.LoadMenu(IDR_POP));
CMenu* pPopup = menu.GetSubMenu(0);
ASSERT(pPopup != NULL);
CWnd* pWndPopupOwner = this;
while(pWndPopupOwner->GetStyle() & WS_CHILD)
pWndPopupOwner = pWndPopupOwner->GetParent();
pPopup->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON,
point.x, point.y, pWndPopupOwner);
}
void CMyEditView::OnCharHighlight()
{
// TODO: Add your command handler code here
CColorDialog dlg;
if( IDOK == dlg.DoModal() )
{
CHARFORMAT2 cf;
SendMessage(EM_GETCHARFORMAT, SCF_SELECTION | SCF_WORD, (LPARAM)&cf);
cf.cbSize = sizeof CHARFORMAT2;
cf.dwMask = CFM_BACKCOLOR;
cf.crBackColor = dlg.GetColor();
SendMessage(EM_SETCHARFORMAT, SCF_SELECTION | SCF_WORD, (LPARAM)&cf);
}
}
void CMyEditView::OnPrint(CDC* pDC, CPrintInfo* pInfo)
{
// TODO: Add your command handler code here
CRichEditView::OnPrint(pDC, pInfo);
UINT nPage = pInfo->m_nCurPage;
ASSERT(nPage <= (UINT)m_aPageStart.GetSize());
UINT Hu = m_aPageStart[nPage];
if (nPage >= 2 && m_aPageStart[nPage-1] >= m_aPageStart[nPage])
{
TRACE0("End of Document\n");
pInfo->m_bContinuePrinting = FALSE;
}
}
void CMyEditView::OnEditRedo()
{
// TODO: Add your command handler code here
SendMessage(EM_REDO, 0, 0);
}
void CMyEditView::OnUpdateEditRedo(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here
BOOL bCanRedo = (BOOL)::SendMessage(m_hWnd, EM_CANREDO, 0, 0);
pCmdUI->Enable(bCanRedo);
}
void CMyEditView::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
{
// TODO: Add your message handler code here and/or call default
if (m_bFirst)
{
m_bFirst = FALSE;
m_bIsSpace = isspace(nChar);
return;
}
if (m_bIsSpace)
{
if (!isspace(nChar))
{
SendMessage(EM_STOPGROUPTYPING,0,0);
m_bIsSpace = FALSE;
}
}
else
{
if (isspace(nChar))
{
SendMessage(EM_STOPGROUPTYPING,0,0);
m_bIsSpace = TRUE;
}
}
CRichEditView::OnChar(nChar, nRepCnt, nFlags);
}
LRESULT CMyEditView::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
{
if (message == EM_FINDTEXTEX)
{
wParam += 1;
}
return CRichEditView::WindowProc(message, wParam, lParam);
}
void CMyEditView::OnGetCharFormatEx(CHARFORMAT2& cf, DWORD dwMask)
{
cf.dwMask |= dwMask;
cf.cbSize = sizeof CHARFORMAT2;
SendMessage(EM_GETCHARFORMAT, 1, (LPARAM)&cf);
}
void CMyEditView::OnCharEffect2(DWORD dwMask, DWORD dwEffect)
{
CHARFORMAT2 cf;
OnGetCharFormatEx(cf, dwMask);
if (cf.dwEffects & dwEffect)
cf.dwEffects &= ~dwEffect;
else
cf.dwEffects |= dwEffect;
OnSetCharFormatEx(cf, dwMask);
}
void CMyEditView::OnUpdateCharEffect2(CCmdUI* pCmdUI, DWORD dwMask, DWORD dwEffect)
{
CHARFORMAT2 cf;
OnGetCharFormatEx(cf, dwMask);
pCmdUI->SetCheck((cf.dwMask & dwMask) ?
((cf.dwEffects & dwEffect) ? 1 : 0) : 2);
}
void CMyEditView::OnSetCharFormatEx(CHARFORMAT2& cf, DWORD dwMask)
{
CRichEditCtrl& rCtrl = GetRichEditCtrl();
cf.cbSize = sizeof CHARFORMAT2;
cf.dwMask |= dwMask;
CHARRANGE cr;
rCtrl.GetSel(cr);
if (cr.cpMax != cr.cpMin)
SendMessage(EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&cf);
else
SendMessage(EM_SETCHARFORMAT, SCF_SELECTION | SCF_WORD, (LPARAM)&cf);
}
void CMyEditView::OnGetParaFormatEx(PARAFORMAT2& pf)
{
pf.cbSize = sizeof PARAFORMAT2;
SendMessage(EM_GETPARAFORMAT, 1, (LPARAM)&pf);
}
void CMyEditView::OnSetParaFormatEx(PARAFORMAT2& pf)
{
CRichEditCtrl& rCtrl = GetRichEditCtrl();
pf.cbSize = sizeof PARAFORMAT2;
CHARRANGE cr;
rCtrl.GetSel(cr);
if (cr.cpMax != cr.cpMin)
SendMessage(EM_SETPARAFORMAT, SCF_SELECTION, (LPARAM)&pf);
else
SendMessage(EM_SETPARAFORMAT, SCF_SELECTION | SCF_WORD, (LPARAM)&pf);
}
void CMyEditView::OnParagraph()
{
// TODO: Add your command handler code here
CParagraphDialog dlg(GetParaFormatSelection());
if (dlg.DoModal() == IDOK)
SetParaFormat(dlg.m_pf);
}
void CMyEditView::OnDate()
{
// TODO: Add your command handler code here
CDateDialog dlg;
if (dlg.DoModal() == IDOK)
GetRichEditCtrl().ReplaceSel(dlg.m_strSel);;
}
void CMyEditView::OnSubscript()
{
// TODO: Add your command handler code here
OnCharEffect2(CFM_SUBSCRIPT,CFE_SUBSCRIPT);
}
void CMyEditView::OnUpdateSubscript(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here
OnUpdateCharEffect2(pCmdUI, CFM_SUBSCRIPT,CFE_SUBSCRIPT);
}
void CMyEditView::OnSuperscript()
{
// TODO: Add your command handler code here
OnCharEffect2(CFM_SUPERSCRIPT,CFE_SUPERSCRIPT);
}
void CMyEditView::OnUpdateSuperscript(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here
OnUpdateCharEffect2(pCmdUI, CFM_SUPERSCRIPT,CFE_SUPERSCRIPT);
}
void CMyEditView::OnInsertNumbering()
{
// TODO: Add your command handler code here
PARAFORMAT2 pf;
OnGetParaFormatEx(pf);
if (pf.dwMask & PFM_NUMBERING && pf.wNumbering == 2)
{
pf.wNumbering = 0;
pf.dxOffset = 0;
pf.dxStartIndent = 0;
pf.dwMask = PFM_NUMBERING | PFM_STARTINDENT | PFM_OFFSET;
}
else
{
pf.dwMask = PFM_NUMBERING | PFM_NUMBERINGSTART | PFM_NUMBERINGSTYLE | PFM_NUMBERINGTAB;
pf.wNumbering = 2;
pf.wNumberingStart = 1;
pf.wNumberingStyle = 1;
pf.wNumberingTab = 360;
if (pf.dxOffset == 0)
{
pf.dxOffset = m_nBulletIndent;
pf.dwMask = PFM_NUMBERING | PFM_STARTINDENT | PFM_OFFSET;
}
}
OnSetParaFormatEx(pf);
}
void CMyEditView::OnUpdateInsertNumbering(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here
PARAFORMAT2 pf;
OnGetParaFormatEx(pf);
pCmdUI->SetRadio( (pf.dwMask & PFM_NUMBERING) ? ((pf.wNumbering & 2) ? 1 : 0) : 2);
}
void CMyEditView::OnChangeCase()
{
// TODO: Add your command handler code here
OnCharEffect2(CFM_ALLCAPS,CFE_ALLCAPS);
}
void CMyEditView::OnUpdateChangeCase(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here
OnUpdateCharEffect2(pCmdUI, CFM_ALLCAPS,CFE_ALLCAPS);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -