The RichEdit control has been widely used on the Internet.
Go to the topic:
A simple method to highlight keywords in RichEdit.
To truly highlight the keyword of RichEdit, You need to rewrite the entire RichEdit control, which requires a lot of work.
Since you don't want to make too much effort to implement it, the effect will be somewhat different.
Here, the method is to input and receive information in RichEdit, and compare the information one by one with the saved keywords.
First, create RichEdit:
RichEdit = CreateWindowEx(WS_EX_CLIENTEDGE,TEXT("RichEdit20A"),NULL,ES_MULTILINE | WS_CHILD |WS_VISIBLE |WS_VSCROLL |WS_HSCROLL | ES_WANTRETURN,170,80,rcClient.right-180,rcClient.bottom/2-60,hWnd,0,hInstance,NULL);
Do not forget to add the RichEdit header file # include "RichEdit. h" and hInst = LoadLibrary (TEXT ("riched#dll "));
To dynamically change the content in RichEdit, a new window callback function is required:
Char buffer [1024*10]; // set a buffer to store the received characters lresult callback editproc (hwnd, uint message, wparam, lparam) {HDC; DWORD firstchar; rect; textrange txtrange; char keybuffer [1024]; hrgn holdrgn; rect realrect; lresult nret; Switch (Message) {Case wm_paint: {nret = callwindowproc (wndproc) oldproc, hwnd, message, wparam, lparam); HDC = getdc (hwnd); sendmessage (hwnd, em_getrect, 0, (lpara M) & rect); lresult NPOs = sendmessage (hwnd, em_charfrompos, 0, (lparam) & rect); lresult nline = sendmessage (hwnd, em_linefromchar, NPOs, 0 ); long nindex = sendmessage (hwnd, em_lineindex, nline, 0); txtrange. chrg. CPMin = nindex; firstchar = nindex; long nnextpos = sendmessage (hwnd, em_charfrompos, 0, (lparam) & rect. right); txtrange. chrg. cpmax = nnextpos; realrect. left = rect. left; realrect. top = rect. top; realrect. right = Rect. right; realrect. bottom = rect. bottom; hrgn = createrectrgn (realrect. left, realrect. top, realrect. right, realrect. bottom); holdrgn = (hrgn) SelectObject (HDC, hrgn); txtrange. lpstrtext = buffer; sendmessage (hwnd, em_gettextrange, 0, (lparam) & txtrange); int I = 0, J; while (buffer [I]! = '\ 0') // match the keyword {While (buffer [I]! = '\ 0' & (buffer [I] = ''| buffer [I] =' \ R' | buffer [I] = '\ t' | buffer [I] = '\ n' | buffer [I] =' ('| buffer [I] = ') ') I ++; If (buffer [I] =' \ 0') break; For (j = 0; buffer [I]! = '\ 0' & buffer [I]! = ''& Buffer [I]! = '\ R' & buffer [I]! = '\ T' & buffer [I]! = '\ N' & buffer [I]! = '(' & Buffer [I]! = ')'; I ++, J ++) keybuffer [J] = buffer [I]; keybuffer [J] = '\ 0'; If (check (keybuffer) = 1) // compare the existing keywords one by one {settextcolor (HDC, RGB (255,); int nwidth = sendmessage (RichEdit, em_posfromchar, nindex + I-j, 0); rect. left = loword (nwidth); rect. top = hiword (nwidth); drawtext (HDC, keybuffer,-1, & rect, 0) ;}} SelectObject (HDC, holdrgn); deleteobject (hrgn); releasedc (hwnd, HDC);} break; default: Return callwindowproc (wndproc) oldproc, hwnd, message, wparam, lparam);} return nret ;}
During initialization, you can perform the following call to highlight the basic keywords:
Oldproc = setwindowlong (RichEdit, gwl_wndproc, (long) editproc); // oldproc is a global long variable
Note:
The keyword is set by yourself. In my above example, a function such as check (char * Str) matches the existing keyword.