派生自CRichEditCtrl的类CKofXMLRichEditCtrl,代码如下:
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
|
/** *\fileKofXMLRichEditCtrl.h * *\author无幻 *\date2012-3-28 * *简单的XML文件高亮编辑器类 */ #pragmaonce #include<tom.h> #include<Richole.h> #defineXML_COLOR_TEXTRGB(0,0,0) #defineXML_COLOR_STRINGRGB(163,21,21) #defineXML_COLOR_TAGSRGB(0,255) #defineXML_COLOR_COMMENTRGB(0,128,0) #defineXML_COLOR_BACKGROUNDRGB(255,255,255) classCKofXMLRichEditCtrl:publicCRichEditCtrl { DECLARE_DYNAMIC(CKofXMLRichEditCtrl) public: CKofXMLRichEditCtrl(); virtual~CKofXMLRichEditCtrl(); protected: voidInitXmlRichEdit(); voidColorXml(); DECLARE_MESSAGE_MAP() virtualvoidPreSubclassWindow(); afx_msgvoidOnEnChange(); private: IRichEditOle*m_pRichEditOle; ITextDocument*m_pTextDocument; }; |
001 002 003 004 005 006 007 008 009 010 011 012 013 014 015 016 017 018 019 020 021 022 023 024 025 026 027 028 029 030 031 032 033 034 035 036 037 038 039 040 041 042 043 044 045 046 047 048 049 050 051 052 053 054 055 056 057 058 059 060 061 062 063 064 065 066 067 068 069 070 071 072 073 074 075 076 077 078 079 080 081 082 083 084 085 086 087 088 089 090 091 092 093 094 095 096 097 098 099 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
|
/** *\fileKofXMLRichEditCtrl.cpp * *\author无幻 *\date2012-3-28 * *简单的XML文件高亮编辑器类 */ #include"stdafx.h" #include"deelx.h" #include"KofXMLRichEditCtrl.h" IMPLEMENT_DYNAMIC(CKofXMLRichEditCtrl,CRichEditCtrl) CKofXMLRichEditCtrl::CKofXMLRichEditCtrl() { m_pRichEditOle=NULL; m_pTextDocument=NULL; } CKofXMLRichEditCtrl::~CKofXMLRichEditCtrl() { if(m_pTextDocument) { m_pTextDocument->Release(); m_pTextDocument=NULL; } if(m_pRichEditOle) { m_pRichEditOle->Release(); m_pRichEditOle=NULL; } } BEGIN_MESSAGE_MAP(CKofXMLRichEditCtrl,CRichEditCtrl) ON_CONTROL_REFLECT(EN_CHANGE,&CKofXMLRichEditCtrl::OnEnChange) END_MESSAGE_MAP() voidCKofXMLRichEditCtrl::PreSubclassWindow() { m_pRichEditOle=GetIRichEditOle(); if(m_pRichEditOle) { if(S_OK!=m_pRichEditOle->QueryInterface(__uuidof(ITextDocument),(LPVOID*)&m_pTextDocument)) { m_pTextDocument=NULL; } } InitXmlRichEdit(); CRichEditCtrl::PreSubclassWindow(); } voidCKofXMLRichEditCtrl::InitXmlRichEdit() { //关闭自动字体切换 SendMessage(EM_SETLANGOPTIONS,0); //设置文本内容变化响应事件掩码 SetEventMask(GetEventMask()|ENM_CHANGE); //设置默认字体 CHARFORMATcf; ZeroMemory(&cf,sizeof(CHARFORMAT)); cf.cbSize=sizeof(CHARFORMAT); cf.dwMask=CFM_FACE|CFM_CHARSET|CFM_COLOR; cf.bCharSet=ANSI_CHARSET; cf.dwEffects=0; cf.crTextColor=XML_COLOR_TEXT; CFileFindfFind; if(fFind.FindFile(_T("C:\\WINDOWS\\Fonts\\CONSOLA.TTF"))) { _tcscpy_s(cf.szFaceName,_T("Consolas")); } else { _tcscpy_s(cf.szFaceName,_T("CourierNew")); } fFind.Close(); SetDefaultCharFormat(cf); //设置背景色 SetBackgroundColor(FALSE,XML_COLOR_BACKGROUND); } voidCKofXMLRichEditCtrl::ColorXml() { CStringstrValue; GetWindowText(strValue); if(strValue.IsEmpty()) { return; } //SetSel会把\r\n当成一个字符位 strValue.Replace(_T("\r\n"),_T("\n")); //转成Unicode统一当成一个字符 CStringWstrValueW(strValue); CHARFORMATcf; ZeroMemory(&cf,sizeof(CHARFORMAT)); cf.cbSize=sizeof(CHARFORMAT); cf.dwMask=CFM_COLOR; cf.dwEffects=0; CStringWstrText; longlBegin,lEnd; intiHorzPos,iVertPos; GetSel(lBegin,lEnd); iHorzPos=GetScrollPos(SB_HORZ); iVertPos=GetScrollPos(SB_VERT); SetRedraw(FALSE); m_pTextDocument->Undo(tomSuspend,NULL); SetSel(0,-1); cf.crTextColor=XML_COLOR_TEXT; SetSelectionCharFormat(cf); staticCRegexpT<wchar_t>regexpTags(L"<([^>]*)>");//找寻<>标签 staticCRegexpT<wchar_t>regexpString(L"\"([^\"]*)\"");//找寻""字符串 staticCRegexpT<wchar_t>regexpComment(L"<!--((?s).*?)-->");//找寻<!---->注释 MatchResultresultTags=regexpTags.Match(strValueW); while(resultTags.IsMatched()) { strText=strValueW.Mid(resultTags.GetStart(),resultTags.GetEnd()-resultTags.GetStart()); SetSel(resultTags.GetStart(),resultTags.GetEnd()); cf.crTextColor=XML_COLOR_TAGS; SetSelectionCharFormat(cf); MatchResultresultString=regexpString.Match(strText); while(resultString.IsMatched()) { SetSel(resultTags.GetStart()+resultString.GetStart(), resultTags.GetStart()+resultString.GetEnd()); cf.crTextColor=XML_COLOR_STRING; SetSelectionCharFormat(cf); resultString=regexpString.Match(strText,resultString.GetEnd()); } resultTags=regexpTags.Match(strValueW,resultTags.GetEnd()); } MatchResultresultComment=regexpComment.Match(strValueW); while(resultComment.IsMatched()) { SetSel(resultComment.GetStart(),resultComment.GetEnd()); cf.crTextColor=XML_COLOR_COMMENT; SetSelectionCharFormat(cf); resultComment=regexpComment.Match(strValueW,resultComment.GetEnd()); } SetSel(lBegin,lEnd); SendMessage(WM_HSCROLL,MAKELONG(SB_THUMBPOSITION,iHorzPos),0); SendMessage(WM_VSCROLL,iVertPos),0); SetRedraw(TRUE); RedrawWindow(); m_pTextDocument->Undo(tomResume,NULL); } voidCKofXMLRichEditCtrl::OnEnChange() { if(m_pTextDocument) { ColorXml(); } } |
1.打开VC2008,新建基于对话框工程,名称: MyXmlViewer;
2.在对话框上放置一个RichEdit 2.0控件,ID号改为: IDC_RICHEDIT_XML,设置其Multiline属性、Horizontal Scroll属性、Vertical Scroll属性、Want Return属性;
3.在应用程序类实现文件,添加以下代码:
1 |
|
AfxInitRichEdit2(); |
4.包含"kofxmlricheditctrl.h"头文件,然后关联IDC_RICHEDIT_XML的变量即可,如下:
01 02 03 04 05 06 07 08 09 10 11 12 13 14 |
|
#include"kofxmlricheditctrl.h" classCMyXmlViewerDlg:publicCDialog { //... public: CKofXMLRichEditCtrlm_RichEdit; }; voidCMyXmlViewerDlg::DoDataExchange(CDataExchange*pDX) { CDialog::DoDataExchange(pDX); DDX_Control(pDX,IDC_RICHEDIT_XML,m_RichEdit); } |
5.效果如下所示:
原文链接:https://www.f2er.com/regex/362337.html