在 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

这个窗口是个简单版,后面还要添加“新建”、“打开”、“保存”、“撤消”、“剪切”、“复制”、“粘贴”、“删除”、“全选”、“查找”功能。

(未完待续)

查看原文 >>
相关文章