在 CCppEditorDlg 創建 ScintillaWnd 並處理簡單消息

在CppEditorDlg.h中添加如下代碼

ScintillaWnd* m_pScintillaWnd;
基於對話框的C++源代碼編輯器製作過程(五)——CCppEditorDlg

基於對話框的C++源代碼編輯器製作過程(五)——CCppEditorDlg

爲CCppEditorDlg類添加OnCreate函數,打開“類嚮導”,選擇“CCppEditorDlg類”,點擊“消息”,找到“WM_CREATE”消息,點擊“添加處理程序”。

基於對話框的C++源代碼編輯器製作過程(五)——CCppEditorDlg

在OnCreate函數中添加如下代碼

int CCppEditorDlg::OnCreate(LPCREATESTRUCT lpCreateStruct){if (CDialogEx::OnCreate(lpCreateStruct) == -1)return -1;// TODO: 在此添加您專用的創建代碼CRect rcWin;GetClientRect(&rcWin);m_pScintillaWnd = new ScintillaWnd();m_pScintillaWnd->Create(_T("Scintilla"), _T("CppCodeEditor"), WS_CHILD | WS_VISIBLE, CRect(0, 0, lpCreateStruct->cx, lpCreateStruct->cy), this, 10000);m_pScintillaWnd->InitScintillaEdit(12, _T("Courier New"));m_pScintillaWnd->ShowWindow(SW_SHOW);return 0;}

爲CCppEditorDlg類添加OnSize函數,打開“類嚮導”,選擇“CCppEditorDlg類”,點擊“消息”,找到“WM_SIZE”消息,點擊“添加處理程序”。

基於對話框的C++源代碼編輯器製作過程(五)——CCppEditorDlg

在OnSize函數中添加如下代碼

void CCppEditorDlg::OnSize(UINT nType, int cx, int cy){CDialogEx::OnSize(nType, cx, cy);// TODO: 在此處添加消息處理程序代碼CRect rcWin;GetClientRect(&rcWin);m_pScintillaWnd->MoveWindow(&rcWin);}

爲CCppEditorDlg類添加OnNotify函數,打開“類嚮導”,選擇“CCppEditorDlg類”,點擊“虛函數”,找到“OnNotify”,點擊“添加函數”。

基於對話框的C++源代碼編輯器製作過程(五)——CCppEditorDlg

在OnNotify函數中添加如下代碼

BOOL CCppEditorDlg::OnNotify(WPARAM wParam, LPARAM lParam, LRESULT* pResult){// TODO: 在此添加專用代碼和/或調用基類struct SCNotification* scn = (struct SCNotification*)lParam;LPNMHDR pnmh = (LPNMHDR)lParam;switch (pnmh->code){case SCN_MODIFIED://修改了文件case SCN_ZOOM://放大,縮小if (m_pScintillaWnd->GetMarginWidthN(0) != 0){m_pScintillaWnd->UpdateLineNumberWidth();}break;case SCN_MARGINCLICK://確定是Fold頁邊點擊事件{long n = m_pScintillaWnd->LineFromPosition(scn->position);m_pScintillaWnd->ToggleFold(n);}break;case SCN_UPDATEUI://界面更新(單擊鼠標,按下箭頭等)break;}return CDialogEx::OnNotify(wParam, lParam, pResult);}

下面我們回到ScintillaWnd類中,爲窗口類添加setCppSyntax函數

在ScintillaWnd.h中添加下面的代碼

void setCppSyntax();

在ScintillaWnd.cpp中添加下面的代碼

const char cppKeyWords[] ="and and_eq asm auto bitand bitor bool break ""case catch char class compl const const_cast continue ""default delete do double dynamic_cast else enum explicit export extern false float for ""friend goto if inline int long mutable namespace new not not_eq ""operator or or_eq private protected public ""register reinterpret_cast return short signed sizeof static static_cast struct switch ""template this throw true try typedef typeid typename union unsigned using ""virtual void volatile wchar_t while xor xor_eq ";void ScintillaWnd::setCppSyntax(){SendMessage(SCI_SETLEXER, SCLEX_CPP);SendMessage(SCI_SETKEYWORDS, 0, (LPARAM)cppKeyWords);// 下面設置各種語法元素前景色SendMessage(SCI_STYLESETFORE, SCE_C_WORD, 0x00FF0000); //關鍵字SendMessage(SCI_STYLESETFORE, SCE_C_STRING, 0x001515A3); //字符串SendMessage(SCI_STYLESETFORE, SCE_C_CHARACTER, 0x001515A3); //字符SendMessage(SCI_STYLESETFORE, SCE_C_PREPROCESSOR, 0x00808080);//預編譯開關SendMessage(SCI_STYLESETFORE, SCE_C_COMMENT, 0x00008000);//塊註釋SendMessage(SCI_STYLESETFORE, SCE_C_COMMENTLINE, 0x00008000);//行註釋SendMessage(SCI_STYLESETFORE, SCE_C_COMMENTDOC, 0x00008000);//文檔註釋(/**開頭)SendMessage(SCI_STYLESETFORE, SCE_C_NUMBER, 0x000010ff);//數字SendMessage(SCI_STYLESETFORE, SCE_C_OPERATOR, 0x0000c0f0);//操作}

到這裏就可以編譯運行了,結果如下圖

基於對話框的C++源代碼編輯器製作過程(五)——CCppEditorDlg

這個窗口是個簡單版,後面還要添加“新建”、“打開”、“保存”、“撤消”、“剪切”、“複製”、“粘貼”、“刪除”、“全選”、“查找”功能。

(未完待續)

查看原文 >>
相關文章