1、首先,在ScintillaWnd.h头文件中添加如下代码

HMODULE m_hSciLexerDll;

如下图:

2、在ScintillaWnd.cpp源文件的构造函数和析构函数中添加如下代码

ScintillaWnd::ScintillaWnd(){m_hSciLexerDll = NULL;m_hSciLexerDll = LoadLibrary(_T("SciLexer.dll"));if (NULL == m_hSciLexerDll){AfxMessageBox(_T("LoadLibrary SciLexer.dll failure..."));}}

这是构造函数

ScintillaWnd::~ScintillaWnd(){if (NULL != m_hWnd){DestroyWindow();}if (m_hSciLexerDll != NULL){FreeLibrary(m_hSciLexerDll);}}

这是析构函数

3、为窗口类创建Create函数

打开类向导,选择“ScintillaWnd”类,找到“虚函数”,找到“Create”函数,点击“添加函数”。

基于对话框的C++源代码编辑器制作过程(四)——窗口类代码

Create函数

添加完“Create”函数,会自动生成一些代码,这些代码不需要改动,保持原样就好。

基于对话框的C++源代码编辑器制作过程(四)——窗口类代码

4、为窗口类添加Init()函数,先在ScintillaWnd.h中添加下面的代码

virtual void Init();
基于对话框的C++源代码编辑器制作过程(四)——窗口类代码

点击图中“小灯泡”,会提示你在ScintillaWnd.cpp文件中创建Init()函数,点击创建。

在创建的Init()函数中添加如下代码

void ScintillaWnd::Init(){// clear all text stylesSendMessage(SCI_CLEARDOCUMENTSTYLE, 0, 0);// set the number of styling bits to 7 - the asp/html views need a lot of styling - default is 5// If you leave the default you will see twiggle lines instead of ASP codeSendMessage(SCI_SETSTYLEBITS, 7, 0);// set the display for indetation guides to on - this displays virtical dotted lines from the beginning of// a code block to the end of the blockSendMessage(SCI_SETINDENTATIONGUIDES, TRUE, 0);// set tabwidth to 4SendMessage(SCI_SETTABWIDTH, 4, 0);// set indention to 4SendMessage(SCI_SETINDENT, 4, 0);// set the caret blinking time to 400 millisecondsSendMessage(SCI_SETCARETPERIOD, 400, 0);// display fold marginsSetFold();// hide SelectionMarginSendMessage(SCI_SETMARGINWIDTHN, 1, 0);// set markersymbol for marker type 0 - bookmarkSendMessage(SCI_MARKERDEFINE, 0, SC_MARK_CIRCLE);// set the forground color for some stylesSendMessage(SCI_STYLESETFORE, 0, RGB(0, 0, 0));SendMessage(SCI_STYLESETFORE, 2, RGB(0, 64, 0));SendMessage(SCI_STYLESETFORE, 5, RGB(0, 0, 255));SendMessage(SCI_STYLESETFORE, 6, RGB(200, 20, 0));SendMessage(SCI_STYLESETFORE, 9, RGB(0, 0, 255));SendMessage(SCI_STYLESETFORE, 10, RGB(255, 0, 64));SendMessage(SCI_STYLESETFORE, 11, RGB(0, 0, 0));// set the backgroundcolor of brace highlightsSendMessage(SCI_STYLESETBACK, STYLE_BRACELIGHT, RGB(0, 255, 0));// set end of line mode to CRLFSendMessage(SCI_CONVERTEOLS, 2, 0);SendMessage(SCI_SETEOLMODE, 2, 0);// SendMessage(SCI_SETVIEWEOL, TRUE, 0);//显示当前行的淡黄色背景SendMessage(SCI_SETCARETLINEVISIBLE, TRUE, 0);SendMessage(SCI_SETCARETLINEBACK, RGB(255, 255, 0), 0);SendMessage(SCI_SETCARETLINEBACKALPHA, 100, 0);}

注意:要在ScintillaWnd.h头文件中包含Scintilla.h和SciLexer.h头文件

基于对话框的C++源代码编辑器制作过程(四)——窗口类代码

为窗口类添加InitScintillaEdit函数,在ScintillaWnd.h中添加下面的代码

void InitScintillaEdit(int nSize, const TCHAR* face);
基于对话框的C++源代码编辑器制作过程(四)——窗口类代码

用同样的方法,在ScintillaWnd.cpp文件中创建InitScintillaEdit函数

在创建的InitScintillaEdit函数添加如下代码

void ScintillaWnd::InitScintillaEdit(int nSize, const TCHAR * face){setCppSyntax();Init();SetDefaultColorFont(nSize, face);UpdateLineNumberWidth();}

为窗口类添加SetDefaultColorFont函数

void SetDefaultColorFont(int nSize, const TCHAR* face);

在创建的SetDefaultColorFont函数添加如下代码

void ScintillaWnd::SetDefaultColorFont(int nSize, const TCHAR * face){SendMessage(SCI_SETSELFORE, TRUE, RGB(255, 255, 255));//选中行的颜色SendMessage(SCI_SETSELBACK, TRUE, RGB(10, 36, 106));//默认文本颜色SendMessage(SCI_STYLESETFORE, STYLE_DEFAULT, RGB(0x00, 0x00, 0x00));SendMessage(SCI_STYLESETBACK, STYLE_DEFAULT, RGB(0xff, 0xff, 0xff));SendMessage(SCI_STYLESETSIZE, STYLE_DEFAULT, nSize);SendMessage(SCI_STYLESETFONT, STYLE_DEFAULT, reinterpret_cast<LPARAM>(face));}

为窗口类添加SetFold函数

void SetFold(BOOL bFold = TRUE);

在创建的SetFold函数添加如下代码

void ScintillaWnd::SetFold(BOOL bFold){if (bFold){// source folding section// tell the lexer that we want folding information - the lexer supplies "folding levels"SendMessage(SCI_SETPROPERTY, (WPARAM) "fold
查看原文 >>
相关文章