win32窗体中使用Scintilla窗体

第一步:建立win32程序

第二步:添加头文件,导入lib库

LoadLibrary(_T("SciLexer.dll"))

这样在后面就可以使用类名 Scintilla 来创建窗体。

第三步:创建 Scintilla 窗体的代码

在 BOOL InitInstance(HINSTANCE hInstance, int nCmdShow) 中添加:

hwndScintilla = CreateWindowEx(0,L"Scintilla",L"", WS_CHILD | WS_VISIBLE | WS_TABSTOP | WS_CLIPCHILDREN,
0,0,CLIENT_WIDTH,CLIENT_HEIGHT,hWnd,(HMENU)SCINT_ID, hInstance,NULL);

注意要放在主窗体之后,hWnd 是主窗体句柄。

第四步:实现自动补全代码的功能

在 WndProc 中处理 WM_NOTIFY 消息:

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    int wmId, wmEvent;
    PAINTSTRUCT ps;
    HDC hdc;
    NMHDR *lpnmhdr; 
    SCNotification* notify = (SCNotification*)lParam;
    switch (message)
    {
    case WM_NOTIFY:
        lpnmhdr = (LPNMHDR) lParam;
        if(lpnmhdr->hwndFrom==hwndScintilla)
        {
            switch(lpnmhdr->code)//notify->nmhdr.code  notify->ch
            {
            case SCN_CHARADDED:
                {
                    static const char* pCallTipNextWord = NULL;//下一个高亮位置
                    static const char* pCallTipCurDesc = NULL;//当前提示的函数信息
                    char word[1000]; //保存当前光标下的单词(函数名)
                    TextRange tr;    //用于SCI_GETTEXTRANGE命令
                    int pos = SendMessage(hwndScintilla,SCI_GETCURRENTPOS,0,0); //取得当前位置(括号的位置)
                    int startpos = SendMessage(hwndScintilla,SCI_WORDSTARTPOSITION,pos-1,0);//当前单词起始位置
                    int endpos = SendMessage(hwndScintilla,SCI_WORDENDPOSITION,pos-1,0);//当前单词终止位置
                    tr.chrg.cpMin = startpos;  //设定单词区间,取出单词
                    tr.chrg.cpMax = endpos;
                    tr.lpstrText = word;
                    SendMessage(hwndScintilla,SCI_GETTEXTRANGE,0, sptr_t(&tr));
                    if(strcmp(word,"Create") == 0) 
                    { 
                        SendMessage(hwndScintilla,SCI_AUTOCSHOW,6,//已经输入了6位字符 
                        sptr_t( "CreateBitmap ""CreateDC ""CreateHandle ""CreateWindow ""CreateWindowEx")); 
                    }
                }
            break;
        default:    
           break;   
        }
    }
    break;default:
        return DefWindowProc(hWnd, message, wParam, lParam);
 }
 return 0;
}

第五步:关闭窗口前获取 Scintilla 窗口的内容

在 WndProc 中处理 WM_DESTROY 消息:

case WM_DESTROY:
    int ret_num;
    HWND hwnd;
    WCHAR buff[1024];//Scintilla 窗体的内容
    ret_num = GetDlgItemText(hWnd,SCINT_ID,buff,1024);
    PostQuitMessage(0);
break;

也可以使用 SCI_SETTEXT 消息来获取。

效果如下:

阿里云网盘下载:https://www.aliyundrive.com/s/sJwWCqGa1V7

原文地址:https://www.cnblogs.com/hosseini/p/15123813.html