MyEditerView.cpp
资源名称:edit.rar [点击查看]
上传用户:icamtech05
上传日期:2020-11-24
资源大小:10883k
文件大小:11k
源码类别:
编辑框
开发平台:
Visual C++
- // MyEditerView.cpp : CMyEditerView 类的实现
- //
- #include "stdafx.h"
- #include "MyEditer.h"
- #include "MyEditerDoc.h"
- #include "CntrItem.h"
- #include "MyEditerView.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- // CMyEditerView
- IMPLEMENT_DYNCREATE(CMyEditerView, CRichEditView)
- BEGIN_MESSAGE_MAP(CMyEditerView, CRichEditView)
- ON_WM_DESTROY()
- // 标准打印命令
- ON_COMMAND(ID_FILE_PRINT, &CRichEditView::OnFilePrint)
- ON_COMMAND(ID_FILE_PRINT_DIRECT, &CRichEditView::OnFilePrint)
- ON_COMMAND(ID_FILE_PRINT_PREVIEW, &CRichEditView::OnFilePrintPreview)
- // ON_WM_CHAR()
- ON_WM_CREATE()
- ON_CONTROL_REFLECT(EN_CHANGE, &CMyEditerView::OnEnChange)
- ON_NOTIFY_REFLECT(EN_PROTECTED, &CMyEditerView::OnEnProtected)
- END_MESSAGE_MAP()
- // CMyEditerView 构造/析构
- CMyEditerView::CMyEditerView() :ctrlEdit(NULL)
- {
- // TODO: 在此处添加构造代码
- AddColors();
- AddKeyWords();
- m_bInForceChange = FALSE;
- m_changeType = ctUndo;
- m_crOldSel.cpMin = m_crOldSel.cpMax = 0;
- }
- CMyEditerView::~CMyEditerView()
- {
- }
- BOOL CMyEditerView::PreCreateWindow(CREATESTRUCT& cs)
- {
- // TODO: 在此处通过修改
- // CREATESTRUCT cs 来修改窗口类或样式
- return CRichEditView::PreCreateWindow(cs);
- }
- void CMyEditerView::OnInitialUpdate()
- {
- CRichEditView::OnInitialUpdate();
- // 设置打印边距(720 缇 = 1/2 英寸)
- SetMargins(CRect(720, 720, 720, 720));
- }
- // CMyEditerView 打印
- BOOL CMyEditerView::OnPreparePrinting(CPrintInfo* pInfo)
- {
- // 默认准备
- return DoPreparePrinting(pInfo);
- }
- void CMyEditerView::OnDestroy()
- {
- // 停用处于析构中的项;这在
- // 使用拆分器视图时非常重要
- COleClientItem* pActiveItem = GetDocument()->GetInPlaceActiveItem(this);
- if (pActiveItem != NULL && pActiveItem->GetActiveView() == this)
- {
- pActiveItem->Deactivate();
- ASSERT(GetDocument()->GetInPlaceActiveItem(this) == NULL);
- }
- CRichEditView::OnDestroy();
- }
- // CMyEditerView 诊断
- #ifdef _DEBUG
- void CMyEditerView::AssertValid() const
- {
- CRichEditView::AssertValid();
- }
- void CMyEditerView::Dump(CDumpContext& dc) const
- {
- CRichEditView::Dump(dc);
- }
- CMyEditerDoc* CMyEditerView::GetDocument() const // 非调试版本是内联的
- {
- ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMyEditerDoc)));
- return (CMyEditerDoc*)m_pDocument;
- }
- #endif //_DEBUG
- // CMyEditerView 消息处理程序
- //void CMyEditerView::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
- //{
- // // TODO: Add your message handler code here and/or call default
- //
- // CRichEditView::OnChar(nChar, nRepCnt, nFlags);
- //}
- void CMyEditerView::FormatText(int iStart, int iEnd)
- {
- if ( iStart >= iEnd )
- return;
- m_bInForceChange = true;
- // Start changing..
- CHARRANGE crOldSel;
- GetRichEditCtrl().GetSel(crOldSel);
- LockWindowUpdate(); // Update not allowed.
- GetRichEditCtrl().HideSelection(true, false);
- CString strBuffer;
- try
- {
- GetRichEditCtrl().SetSel(iStart, iEnd);
- GetRichEditCtrl().GetTextRange(iStart, iEnd, strBuffer);
- int nStart, nPtr;
- nStart = nPtr = 0;
- while( nPtr < strBuffer.GetLength() )
- {
- Color clrCur = m_clrNormalColor;
- TCHAR ch = strBuffer[nPtr];
- // TODO: Process comments /* .. */.
- if( ch == '/' && strBuffer[nPtr +1] == '*' )
- {
- nStart = nPtr;
- ++nPtr;
- while( nPtr < strBuffer.GetLength() &&
- ( strBuffer[ nPtr ] != '/' || strBuffer[ nPtr-1 ] != '*') )
- ++nPtr;
- clrCur = m_clrCommentColor;
- }
- // TODO: Process comments //.. .
- else if( ch == '/' && strBuffer[nPtr + 1] == '/' )
- {
- nStart = nPtr;
- while( nPtr < strBuffer.GetLength() && strBuffer[ ++nPtr ] != 'r')
- ;
- clrCur = m_clrCommentColor;
- }
- // TODO: Process keywords.
- else if( _istalpha(ch) || ch == '_' )
- {
- nStart = nPtr;
- while( nPtr < strBuffer.GetLength() && ( _istalnum(strBuffer[nPtr]) || strBuffer[nPtr] == '_' ) )
- nPtr ++;
- CString strTemp = strBuffer.Mid( nStart, nPtr - nStart );
- int iTemp;
- if( m_mpKeyword.Lookup(strTemp, iTemp) )
- clrCur = m_clrKeywordColor;
- else
- clrCur = m_clrNormalColor;
- }
- // TODO: Process strings.
- else if( ch == '"' )
- {
- nStart = nPtr++;
- while( nPtr < strBuffer.GetLength() && strBuffer[nPtr] != '"' && AddIndex(strBuffer, nPtr) )
- ;
- ++nPtr;
- clrCur = m_clrStringColor;
- }
- // TODO: Process characters.
- else if( ch == ''' )
- {
- nStart = nPtr++;
- while( nPtr < strBuffer.GetLength() && strBuffer[nPtr] != ''' && AddIndex(strBuffer, nPtr) )
- ;
- ++nPtr;
- clrCur = m_clrStringColor;
- }
- // TODO: Others.
- else
- ++ nPtr;
- ASSERT( nStart < strBuffer.GetLength() );
- SetFormat(iStart + nStart, iStart + nPtr, clrCur);
- nStart = nPtr;
- if( nPtr < strBuffer.GetLength() )
- ch = strBuffer[nPtr];
- }
- }
- catch(...)
- {}
- GetRichEditCtrl().SetSel( crOldSel );
- GetRichEditCtrl().HideSelection( false, false );
- UnlockWindowUpdate();
- // End changing..
- m_bInForceChange = false;
- }
- int CMyEditerView::OnCreate(LPCREATESTRUCT lpCreateStruct)
- {
- if (CRichEditView::OnCreate(lpCreateStruct) == -1)
- return -1;
- // TODO: Add your specialized creation code here
- Initialize();
- return 0;
- }
- void CMyEditerView::OnEnChange()
- {
- // TODO: If this is a RICHEDIT control, the control will not
- // send this notification unless you override the CRichEditView::OnInitDialog()
- // function and call CRichEditCtrl().SetEventMask()
- // with the ENM_CHANGE flag ORed into the mask.
- // TODO: Add your control notification handler code here
- if(m_bInForceChange)
- return;
- CHARRANGE crCurSel;
- GetRichEditCtrl().GetSel(crCurSel);
- if( m_changeType == ctMove && crCurSel.cpMin == crCurSel.cpMax )
- { // 如果操作是移动,并且crCurSel.cpMin == crCurSel.cpMax时, 该操作相当于粘贴操作
- m_changeType = ctPaste;
- }
- switch(m_changeType)
- {
- case ctReplSel:
- case ctPaste:
- FormatLine(m_crOldSel.cpMin, crCurSel.cpMax);
- break;
- case ctDelete:
- case ctBack:
- case ctCut:
- case ctUndo:
- FormatLine(crCurSel.cpMin, crCurSel.cpMax);
- break;
- case ctMove:
- FormatLine(crCurSel.cpMin, crCurSel.cpMax);
- if(crCurSel.cpMin > m_crOldSel.cpMin)
- FormatLine(m_crOldSel.cpMin, m_crOldSel.cpMin);
- else
- FormatLine(m_crOldSel.cpMax, m_crOldSel.cpMax);
- break;
- default:
- FormatText(0, GetTextLength());
- }
- m_changeType = ctUndo;
- }
- // 初始化每一种颜色值
- void CMyEditerView::SetColor(COLORREF clr, bool bBold, Color & sc)
- {
- sc.crClr = clr;
- sc.bBold = bBold;
- }
- // 初始化颜色值
- void CMyEditerView::AddColors(void)
- {
- SetColor(RGB(0, 128, 0), false, m_clrCommentColor);
- SetColor(RGB(0, 0, 255), false, m_clrKeywordColor);
- SetColor(RGB(0, 0, 0), false, m_clrNormalColor);
- SetColor(RGB(255, 0, 255), false, m_clrStringColor);
- SetColor(RGB(255, 0, 255), false, m_clrCharColor);
- SetColor(RGB(255, 0, 255), false, m_clrNumberColor);
- SetColor(RGB(0, 0, 0), true, m_clrConstantColor);
- }
- void CMyEditerView::OnEnProtected(NMHDR *pNMHDR, LRESULT *pResult)
- {
- ENPROTECTED *pEnProtected = reinterpret_cast<ENPROTECTED *>(pNMHDR);
- // TODO: The control will not send this notification unless you override the
- // CRichEditView::OnInitDialog() function to send the EM_SETEVENTMASK message
- // to the control with the ENM_PROTECTED flag ORed into the lParam mask.
- // TODO: Add your control notification handler code here
- // TODO: 判断消息类型,并存储在m_changeType中。
- ENPROTECTED * pEP = (ENPROTECTED *)pNMHDR;
- switch(pEP->msg)
- {
- case WM_KEYDOWN:
- switch(pEP->wParam)
- {
- case VK_DELETE:
- m_changeType = ctDelete;
- break;
- case VK_BACK:
- m_changeType = ctBack;
- break;
- default:
- m_changeType = ctUnknown;
- break;
- }
- break;
- case EM_REPLACESEL:
- case WM_CHAR:
- m_changeType = ctReplSel;
- break;
- case WM_PASTE: // 剪切 + 粘贴 = 移动
- m_changeType = (m_changeType == ctCut) ? ctMove : ctPaste;
- break;
- case WM_CUT:
- m_changeType = ctCut;
- break;
- case EM_SETCHARFORMAT: // 不处理用户设置格式
- break;
- default:
- m_changeType = ctUnknown;
- break;
- }
- if (pEP->msg != EM_SETCHARFORMAT && m_changeType != ctMove)
- m_crOldSel = pEP->chrg;
- *pResult = 0;
- }
- // 添加关键字
- void CMyEditerView::AddKeyWords(void)
- {
- // TODO; 为m_mpKeyword添加关键字
- CString strKeyword[] =
- {
- L"auto", L"break",L"case",L"char",
- L"const",L"continue",L"default",L"do",
- L"double",L"else",L"enum",L"extern",
- L"float",L"for",L"goto",L"if",
- L"int",L"long",L"register",L"return",
- L"short",L"signed",L"sizeof",L"static",
- L"struct",L"switch",L"typedef",L"union",
- L"unsigned",L"void",L"volatile",L"while"
- };
- m_mpKeyword.RemoveAll();
- for(int i = 0; i < sizeof(strKeyword) / sizeof(CString) ; i++)
- m_mpKeyword.SetAt(strKeyword[i], i);
- }
- // 为当前行设置格式
- void CMyEditerView::FormatLine(int iStart, int iEnd)
- {
- // 设置包括iStart和iEnd之间的所有行的格式。
- long nStart = GetRichEditCtrl().LineIndex(GetRichEditCtrl().LineFromChar(iStart));
- long nEnd = GetRichEditCtrl().LineIndex(GetRichEditCtrl().LineFromChar(iEnd));
- nEnd += GetRichEditCtrl().LineLength(iEnd);
- FormatText(nStart, nEnd);
- }
- // 设置从 iStart 开始到 iEnd 结束的字符格式
- void CMyEditerView::SetFormat(int iStart, int iEnd, Color clr)
- {
- if(iStart >= iEnd)
- return;
- CHARRANGE crOldSel;
- GetRichEditCtrl().GetSel(crOldSel);
- GetRichEditCtrl().SetSel(iStart, iEnd);
- DWORD dwEffect = clr.bBold ? CFE_BOLD : 0;
- CHARFORMAT cf;
- cf.cbSize = sizeof(cf);
- GetRichEditCtrl().GetSelectionCharFormat(cf);
- if ((cf.dwMask & CFM_COLOR) && cf.crTextColor == clr.crClr &&
- (cf.dwMask & CFM_BOLD) && (cf.dwEffects & CFE_BOLD) == dwEffect)
- return;
- cf.dwEffects = dwEffect;
- cf.crTextColor = clr.crClr;
- cf.dwMask = CFM_BOLD | CFM_COLOR;
- GetRichEditCtrl().SetSelectionCharFormat(cf);
- GetRichEditCtrl().SetSel(crOldSel);
- }
- // 初始化程序,将文件设为保护模式
- void CMyEditerView::Initialize(void)
- {
- PARAFORMAT2 pf;
- pf.cbSize = sizeof(PARAFORMAT2);
- pf.dwMask = PFM_TABSTOPS ;
- pf.cTabCount = MAX_TAB_STOPS;
- for( int itab = 0 ; itab < pf.cTabCount ; itab++ )
- pf.rgxTabs[itab] = (itab + 1) * 1440/5 ;
- SetParaFormat( pf );
- CHARFORMAT cfDefault;
- cfDefault.cbSize = sizeof(cfDefault);
- cfDefault.dwEffects = CFE_PROTECTED;
- cfDefault.dwMask = CFM_BOLD | CFM_FACE | CFM_SIZE | CFM_CHARSET | CFM_PROTECTED;
- cfDefault.yHeight = 200;
- cfDefault.bCharSet = 0xEE;
- GetRichEditCtrl().SetDefaultCharFormat(cfDefault);
- GetRichEditCtrl().SetEventMask(ENM_CHANGE | ENM_SELCHANGE | ENM_PROTECTED);
- }
- // 向后移动指针
- bool CMyEditerView::AddIndex(CString & strBuffer, int & Index)
- {
- if(strBuffer[Index] == 'r')
- return false;
- if(strBuffer[Index] == '\')
- {
- if(strBuffer[Index+1] == 'r' || strBuffer[Index+1] == 'b' ||
- strBuffer[Index+1] == 'n' || strBuffer[Index+1] == '\' ||
- strBuffer[Index+1] == 'v' || strBuffer[Index+1] == ''' ||
- strBuffer[Index+1] == 't' || strBuffer[Index+1] == 'a' ||
- strBuffer[Index+1] == 'f' || strBuffer[Index+1] == '"')
- {
- Index += 2;
- return true;
- }
- }
- Index ++;
- return true;
- }