Scintilla is a free source code editing component. Here we will record its usage in MFC.
Local Environment: Windows XP, Visual Studio 2008 SP1
1. DownloadScintillaSource code version 3.24 (http://www.scintilla.org/ScintillaDownload.html ).
2. Open vs2008, click "Tools" → "Visual Studio 2008 command prompt" in the menu bar, and run the CD command".. \ Scintilla \ Win32"Directory, use the following command to compile:
3. After compilation, you can".. \ Scintilla \ bin"Directory to seeScilexer. dllAndScintilla. dll.
4. Download the MFC encapsulation class (http://www.naughter.com/scintilla.html) from the naughter website ).
5. Create an MFC multi-document application namedTestscintilla.
6. Copy scintillactrl. H, scintillactrl. cpp, scintilladocview. H, and scintilladocview. cpp to the project directory and add them to the project.
7. Refer to the scintillademo project to create the idd_scintilla_finddlgord and idd_scintilla_replacedlgord dialog boxes.
8"Stdafx. h"Add the following code to the file:
1 2 3 4 |
|
# Define sci_namespace // use scintilla via a namespace # Include <platform. h> # Include <scintilla. h> # Include <scilexer. h> |
ReplaceCtestscintillaviewThe base class is scintilla: cscintillaview, replaceCtestscintilladocThe base class is scintilla: cscintilladoc. Add the following variables to the app class:
InInitinstanceFunction, add the following code:
1 2 3 4 5 6 |
|
M_hscidll = loadlibrary (_ T ("scilexer. dll ")); If (m_hscidll = NULL) { Afxmessagebox (_ T ("scilexer DLL is not installed .")); Return false; } |
Heavy LoadExitinstanceFunction, add the following code:
1 2 3 4 |
|
If (m_hscidll) { Freelibrary (m_hscidll ); } |
9. Compile and setScilexer. dllPut it in the program directory and run the program. The editor is displayed, as shown in:
10. OverloadCtestscintillaviewOfOninitialupdateThe Code is as follows:
1 2 3 4 5 6 7 8 9 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 40 41 |
|
Const tchar cppkeywords [] = _ T ("and and_eq ASM auto bitand bitor bool break ") _ T ("case catch char class compl const const_cast continue ") _ T ("Default Delete do double dynamic_cast else Enum explicit export extern false float ") _ T ("friend goto if inline int long mutable namespace new not not_eq ") _ T ("operator or or_eq private protected public ") _ T ("register reinterpret_cast return short signed sizeof static static_cast struct switch ") _ T ("template this throw true try typedef typeid typename Union unsigned using ") _ T ("virtual void volatile wchar_t while XOR xor_eq ");Void ctestscintillaview: oninitialupdate () { Cscintillaview: oninitialupdate (); Cscintillactrl & rctrl = getctrl (); // Setup the lexer Rctrl. setlexer (sclex_cpp ); Rctrl. setkeywords (0, cppkeywords ); // Setup styles Rctrl. stylesetfore (style_default, RGB (0, 0, 0 )); Rctrl. stylesetback (style_default, RGB (0xff, 0xff, 0xff )); Rctrl. stylesetsize (style_default, 11 ); Rctrl. stylesetfont (style_default, "verdana "); Rctrl. styleclearall (); Rctrl. stylesetfore (sce_c_default, RGB (0, 0, 0 )); Rctrl. stylesetfore (sce_c_comment, RGB (0, 0x80, 0 )); Rctrl. stylesetfore (sce_c_commentline, RGB (0, 0x80, 0 )); Rctrl. stylesetfore (sce_c_commentdoc, RGB (0, 0x80, 0 )); Rctrl. stylesetfore (sce_c_commentlinedoc, RGB (0, 0x80, 0 )); Rctrl. stylesetfore (sce_c_commentdockeyword, RGB (0, 0x80, 0 )); Rctrl. stylesetfore (sce_c_commentdockeyworderror, RGB (0, 0x80, 0 )); Rctrl. stylesetfore (sce_c_number, RGB (0, 0x80, 0x80 )); Rctrl. stylesetfore (sce_c_word, RGB (0, 0, 0x80 )); Rctrl. stylesetbold (sce_c_word, 1 ); Rctrl. stylesetfore (sce_c_string, RGB (0x80, 0, 0x80 )); Rctrl. stylesetfore (sce_c_identifier, RGB (0, 0, 0 )); Rctrl. stylesetfore (sce_c_preprocessor, RGB (0x80, 0, 0 )); Rctrl. stylesetfore (sce_c_operator, RGB (0x80, 0x80, 0 )); } |
Set the C ++ syntax parser, set the highlighted keywords, set the text style, and compile and run again. The effect is shown in:
Sample Code download: http://download.csdn.net/detail/akof1314/5069136
The static compilation procedure is as follows:
1. Create a static library project named:Scintillalib.
2. SetScintillaPut the source code folder in the project directory, and add files under the lexers, lexlib, SRC, and Win32 directories to the project.
3. Set project properties, C/C ++ → General → additional include directories to. \ scintilla \ include;. \ scintilla \ lexlib;. \ scintilla \ SRC
C/C ++ → Preprocessor definitions fill in static_build; sci_lexer
4. Comment outCheckd2d. cxxFile Content.
5. The compilation is complete.
6. Use the test project aboveCtestscintillaappOfInitinstanceFunction, add the following code:
1 |
|
Scintilla_registerclasses (AfxGetInstanceHandle ()); |
InExitinstanceFunction, add the following code:
1 |
|
Scintilla_releaseresources (); |
7. Set project properties, linker → input → additional dependencies, and enter scintillalib. Lib imm32.lib.
8. Compile and run the program.
Sample Code download: http://download.csdn.net/detail/akof1314/5069143
Exclude unnecessary language Parser:
1. Delete".. \ Scintilla \ lexers"Corresponding to the language parser not required in the directoryLex *. cxxFile, for example, onlyLexcpp. cxxFile.
2. Open".. \ Scintilla \ SRC \ catalogue. cxx"File, find the following location
1 |
|
// ++ Autogenerated -- run src/lexgen. py to regenerate |
Delete link_lexer (...);
1 2 |
|
Link_lexer (lmcpp ); Link_lexer (lmcppnocase ); |
Open".. \ Scintilla \ Win32 \ scintilla. Mak"File, find the following location
1 2 |
|
# ++ Autogenerated -- run src/lexgen. py to regenerate # ** Lexobjs =\\ n \ (\ t $ (dir_o) \\\ *. OBJ \\ n \) |
Delete $ (dir_o) \ Lex ***. OBJ \ To make it available
1 2 3 4 5 6 |
|
# ++ Autogenerated -- run src/lexgen. py to regenerate # ** Lexobjs =\\ n \ (\ t $ (dir_o) \\\ *. OBJ \\ n \) Lexobjs = \ $ (Dir_o) \ lexcpp. OBJ \# -- Autogenerated -- end of automatically generated Section |
Then find the following location:
1 2 |
|
# ++ Autogenerated -- run src/lexgen. py to regenerate # ** \ N \ ($ (dir_o) \ *. OBJ: .. \ lexers \ *. cxx $ (lex_headers) \ n \) |
Delete $ (dir_o) \ Lex ***. OBJ: .. \ lexers \ Lex ***. cxx $ (lex_headers) to make it available
1 2 3 4 5 6 7 |
|
# ++ Autogenerated -- run src/lexgen. py to regenerate # ** \ N \ ($ (dir_o) \ *. OBJ: .. \ lexers \ *. cxx $ (lex_headers) \ n \)$ (Dir_o) \ lexcpp. OBJ: .. \ lexers \ lexcpp. cxx $ (lex_headers) # -- Autogenerated -- end of automatically generated Section |
3. Save the changes, open vs2008, and click "Tools" → "Visual Studio 2008 command prompt" in the menu bar. Run the CD command".. \ Scintilla \ Win32"Directory, use the following command to compile:
4. After compilation,Scilexer. dllIt is 348kb, and 649kb before being reduced.
References:
1. scintilla documentation http://www.scintilla.org/ScintillaDoc.html
2. MFC classes to encapsulate the scintilla Edit Control
Http://www.naughter.com/scintilla.html
3. simple use of scintilla in MFC
Http://www.qingfengju.com/article.asp? Id = 14
4. Use scintilla to enable the program to support syntax highlighting and compilation
Http://hi.baidu.com/kxw102/item/b7e701a569d685de5af19138
5. scintilla open source Library User Guide http://www.cnblogs.com/superanyi/archive/2011/04/07/2008632.html