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
查看原文 >>
相關文章